Merge "Modified the PrintJobSettingsFragment"
This commit is contained in:
@@ -19,13 +19,16 @@
|
|||||||
android:title="@string/print_print_job">
|
android:title="@string/print_print_job">
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
android:key="print_job_preference">
|
android:key="print_job_preference"
|
||||||
|
android:title="@string/print_print_job"
|
||||||
|
settings:controller="com.android.settings.print.PrintJobPreferenceController">
|
||||||
</Preference>
|
</Preference>
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
android:key="print_job_message_preference"
|
android:key="print_job_message_preference"
|
||||||
android:layout="@layout/print_job_summary"
|
android:layout="@layout/print_job_summary"
|
||||||
android:selectable="false">
|
android:selectable="false"
|
||||||
|
settings:controller="com.android.settings.print.PrintJobMessagePreferenceController">
|
||||||
</Preference>
|
</Preference>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
||||||
|
* except in compliance with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the specific language governing
|
||||||
|
* permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.print;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.print.PrintJob;
|
||||||
|
import android.print.PrintJobInfo;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
public class PrintJobMessagePreferenceController extends PrintJobPreferenceControllerBase {
|
||||||
|
|
||||||
|
public PrintJobMessagePreferenceController(Context context, String key) {
|
||||||
|
super(context, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void updateUi() {
|
||||||
|
final PrintJob printJob = getPrintJob();
|
||||||
|
|
||||||
|
if (printJob == null) {
|
||||||
|
mFragment.finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printJob.isCancelled() || printJob.isCompleted()) {
|
||||||
|
mFragment.finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final PrintJobInfo info = printJob.getInfo();
|
||||||
|
final CharSequence status = info.getStatus(mContext.getPackageManager());
|
||||||
|
mPreference.setVisible(!TextUtils.isEmpty(status));
|
||||||
|
mPreference.setSummary(status);
|
||||||
|
}
|
||||||
|
}
|
115
src/com/android/settings/print/PrintJobPreferenceController.java
Normal file
115
src/com/android/settings/print/PrintJobPreferenceController.java
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
||||||
|
* except in compliance with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the specific language governing
|
||||||
|
* permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.print;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.TypedArray;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.print.PrintJob;
|
||||||
|
import android.print.PrintJobInfo;
|
||||||
|
import android.text.format.DateUtils;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
|
||||||
|
public class PrintJobPreferenceController extends PrintJobPreferenceControllerBase {
|
||||||
|
|
||||||
|
public PrintJobPreferenceController(Context context, String key) {
|
||||||
|
super(context, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void updateUi() {
|
||||||
|
final PrintJob printJob = getPrintJob();
|
||||||
|
|
||||||
|
if (printJob == null) {
|
||||||
|
mFragment.finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printJob.isCancelled() || printJob.isCompleted()) {
|
||||||
|
mFragment.finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintJobInfo info = printJob.getInfo();
|
||||||
|
|
||||||
|
switch (info.getState()) {
|
||||||
|
case PrintJobInfo.STATE_CREATED: {
|
||||||
|
mPreference.setTitle(mContext.getString(
|
||||||
|
R.string.print_configuring_state_title_template, info.getLabel()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PrintJobInfo.STATE_QUEUED:
|
||||||
|
case PrintJobInfo.STATE_STARTED: {
|
||||||
|
if (!printJob.getInfo().isCancelling()) {
|
||||||
|
mPreference.setTitle(mContext.getString(
|
||||||
|
R.string.print_printing_state_title_template, info.getLabel()));
|
||||||
|
} else {
|
||||||
|
mPreference.setTitle(mContext.getString(
|
||||||
|
R.string.print_cancelling_state_title_template, info.getLabel()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PrintJobInfo.STATE_FAILED: {
|
||||||
|
mPreference.setTitle(mContext.getString(
|
||||||
|
R.string.print_failed_state_title_template, info.getLabel()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PrintJobInfo.STATE_BLOCKED: {
|
||||||
|
if (!printJob.getInfo().isCancelling()) {
|
||||||
|
mPreference.setTitle(mContext.getString(
|
||||||
|
R.string.print_blocked_state_title_template, info.getLabel()));
|
||||||
|
} else {
|
||||||
|
mPreference.setTitle(mContext.getString(
|
||||||
|
R.string.print_cancelling_state_title_template, info.getLabel()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
mPreference.setSummary(mContext.getString(R.string.print_job_summary,
|
||||||
|
info.getPrinterName(), DateUtils.formatSameDayTime(
|
||||||
|
info.getCreationTime(), info.getCreationTime(), DateFormat.SHORT,
|
||||||
|
DateFormat.SHORT)));
|
||||||
|
|
||||||
|
TypedArray a = mContext.obtainStyledAttributes(new int[]{
|
||||||
|
android.R.attr.colorControlNormal});
|
||||||
|
int tintColor = a.getColor(0, 0);
|
||||||
|
a.recycle();
|
||||||
|
|
||||||
|
switch (info.getState()) {
|
||||||
|
case PrintJobInfo.STATE_QUEUED:
|
||||||
|
case PrintJobInfo.STATE_STARTED: {
|
||||||
|
Drawable icon = mContext.getDrawable(com.android.internal.R.drawable.ic_print);
|
||||||
|
icon.setTint(tintColor);
|
||||||
|
mPreference.setIcon(icon);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PrintJobInfo.STATE_FAILED:
|
||||||
|
case PrintJobInfo.STATE_BLOCKED: {
|
||||||
|
Drawable icon = mContext.getDrawable(
|
||||||
|
com.android.internal.R.drawable.ic_print_error);
|
||||||
|
icon.setTint(tintColor);
|
||||||
|
mPreference.setIcon(icon);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
||||||
|
* except in compliance with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the specific language governing
|
||||||
|
* permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.print;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.print.PrintJob;
|
||||||
|
import android.print.PrintJobId;
|
||||||
|
import android.print.PrintManager;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
|
|
||||||
|
public abstract class PrintJobPreferenceControllerBase extends BasePreferenceController implements
|
||||||
|
LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener {
|
||||||
|
private static final String TAG = "PrintJobPrefCtrlBase";
|
||||||
|
|
||||||
|
private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
|
||||||
|
|
||||||
|
private final PrintManager mPrintManager;
|
||||||
|
protected Preference mPreference;
|
||||||
|
protected PrintJobSettingsFragment mFragment;
|
||||||
|
protected PrintJobId mPrintJobId;
|
||||||
|
|
||||||
|
public PrintJobPreferenceControllerBase(Context context, String key) {
|
||||||
|
super(context, key);
|
||||||
|
mPrintManager = ((PrintManager) mContext.getSystemService(
|
||||||
|
Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(mContext.getUserId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
mPrintManager.addPrintJobStateChangeListener(this);
|
||||||
|
updateUi();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop() {
|
||||||
|
mPrintManager.removePrintJobStateChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return AVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPrintJobStateChanged(PrintJobId printJobId) {
|
||||||
|
updateUi();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
super.displayPreference(screen);
|
||||||
|
mPreference = screen.findPreference(getPreferenceKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init(PrintJobSettingsFragment fragment) {
|
||||||
|
mFragment = fragment;
|
||||||
|
processArguments();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected PrintJob getPrintJob() {
|
||||||
|
return mPrintManager.getPrintJob(mPrintJobId);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void updateUi();
|
||||||
|
|
||||||
|
private void processArguments() {
|
||||||
|
String printJobId = mFragment.getArguments().getString(EXTRA_PRINT_JOB_ID);
|
||||||
|
if (printJobId == null) {
|
||||||
|
printJobId = mFragment.getActivity().getIntent().getStringExtra(EXTRA_PRINT_JOB_ID);
|
||||||
|
|
||||||
|
if (printJobId == null) {
|
||||||
|
Log.w(TAG, EXTRA_PRINT_JOB_ID + " not set");
|
||||||
|
mFragment.finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mPrintJobId = PrintJobId.unflattenFromString(printJobId);
|
||||||
|
}
|
||||||
|
}
|
@@ -17,114 +17,59 @@
|
|||||||
package com.android.settings.print;
|
package com.android.settings.print;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.TypedArray;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.print.PrintJob;
|
import android.print.PrintJob;
|
||||||
import android.print.PrintJobId;
|
|
||||||
import android.print.PrintJobInfo;
|
|
||||||
import android.print.PrintManager;
|
|
||||||
import android.print.PrintManager.PrintJobStateChangeListener;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.text.format.DateUtils;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fragment for management of a print job.
|
* Fragment for management of a print job.
|
||||||
*/
|
*/
|
||||||
public class PrintJobSettingsFragment extends SettingsPreferenceFragment {
|
public class PrintJobSettingsFragment extends DashboardFragment {
|
||||||
private static final String LOG_TAG = PrintJobSettingsFragment.class.getSimpleName();
|
private static final String TAG = "PrintJobSettingsFragment";
|
||||||
|
|
||||||
private static final int MENU_ITEM_ID_CANCEL = 1;
|
private static final int MENU_ITEM_ID_CANCEL = 1;
|
||||||
private static final int MENU_ITEM_ID_RESTART = 2;
|
private static final int MENU_ITEM_ID_RESTART = 2;
|
||||||
|
|
||||||
private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
|
@Override
|
||||||
|
protected int getPreferenceScreenResId() {
|
||||||
|
return R.xml.print_job_settings;
|
||||||
|
}
|
||||||
|
|
||||||
private static final String PRINT_JOB_PREFERENCE = "print_job_preference";
|
@Override
|
||||||
private static final String PRINT_JOB_MESSAGE_PREFERENCE = "print_job_message_preference";
|
protected String getLogTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
private final PrintJobStateChangeListener mPrintJobStateChangeListener =
|
@Override
|
||||||
new PrintJobStateChangeListener() {
|
public void onAttach(Context context) {
|
||||||
@Override
|
super.onAttach(context);
|
||||||
public void onPrintJobStateChanged(PrintJobId printJobId) {
|
use(PrintJobPreferenceController.class).init(this);
|
||||||
updateUi();
|
use(PrintJobMessagePreferenceController.class).init(this);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
private PrintManager mPrintManager;
|
|
||||||
|
|
||||||
private Preference mPrintJobPreference;
|
|
||||||
private Preference mMessagePreference;
|
|
||||||
|
|
||||||
private PrintJobId mPrintJobId;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getMetricsCategory() {
|
public int getMetricsCategory() {
|
||||||
return MetricsEvent.PRINT_JOB_SETTINGS;
|
return MetricsEvent.PRINT_JOB_SETTINGS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
||||||
Bundle savedInstanceState) {
|
|
||||||
View view = super.onCreateView(inflater, container, savedInstanceState);
|
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.print_job_settings);
|
|
||||||
mPrintJobPreference = findPreference(PRINT_JOB_PREFERENCE);
|
|
||||||
mMessagePreference = findPreference(PRINT_JOB_MESSAGE_PREFERENCE);
|
|
||||||
|
|
||||||
mPrintManager = ((PrintManager) getActivity().getSystemService(
|
|
||||||
Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(
|
|
||||||
getActivity().getUserId());
|
|
||||||
|
|
||||||
getActivity().getActionBar().setTitle(R.string.print_print_job);
|
|
||||||
|
|
||||||
processArguments();
|
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
getListView().setEnabled(false);
|
getListView().setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
mPrintManager.addPrintJobStateChangeListener(
|
|
||||||
mPrintJobStateChangeListener);
|
|
||||||
updateUi();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop() {
|
|
||||||
super.onStop();
|
|
||||||
mPrintManager.removePrintJobStateChangeListener(
|
|
||||||
mPrintJobStateChangeListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
super.onCreateOptionsMenu(menu, inflater);
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
|
|
||||||
PrintJob printJob = getPrintJob();
|
final PrintJob printJob = use(PrintJobPreferenceController.class).getPrintJob();
|
||||||
if (printJob == null) {
|
if (printJob == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -144,7 +89,7 @@ public class PrintJobSettingsFragment extends SettingsPreferenceFragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
PrintJob printJob = getPrintJob();
|
final PrintJob printJob = use(PrintJobPreferenceController.class).getPrintJob();
|
||||||
|
|
||||||
if (printJob != null) {
|
if (printJob != null) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
@@ -164,113 +109,4 @@ public class PrintJobSettingsFragment extends SettingsPreferenceFragment {
|
|||||||
|
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processArguments() {
|
|
||||||
String printJobId = getArguments().getString(EXTRA_PRINT_JOB_ID);
|
|
||||||
if (printJobId == null) {
|
|
||||||
printJobId = getIntent().getStringExtra(EXTRA_PRINT_JOB_ID);
|
|
||||||
|
|
||||||
if (printJobId == null) {
|
|
||||||
Log.w(LOG_TAG, EXTRA_PRINT_JOB_ID + " not set");
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mPrintJobId = PrintJobId.unflattenFromString(printJobId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private PrintJob getPrintJob() {
|
|
||||||
return mPrintManager.getPrintJob(mPrintJobId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateUi() {
|
|
||||||
PrintJob printJob = getPrintJob();
|
|
||||||
|
|
||||||
if (printJob == null) {
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (printJob.isCancelled() || printJob.isCompleted()) {
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrintJobInfo info = printJob.getInfo();
|
|
||||||
|
|
||||||
switch (info.getState()) {
|
|
||||||
case PrintJobInfo.STATE_CREATED: {
|
|
||||||
mPrintJobPreference.setTitle(getString(
|
|
||||||
R.string.print_configuring_state_title_template, info.getLabel()));
|
|
||||||
} break;
|
|
||||||
case PrintJobInfo.STATE_QUEUED:
|
|
||||||
case PrintJobInfo.STATE_STARTED: {
|
|
||||||
if (!printJob.getInfo().isCancelling()) {
|
|
||||||
mPrintJobPreference.setTitle(getString(
|
|
||||||
R.string.print_printing_state_title_template, info.getLabel()));
|
|
||||||
} else {
|
|
||||||
mPrintJobPreference.setTitle(getString(
|
|
||||||
R.string.print_cancelling_state_title_template, info.getLabel()));
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
|
|
||||||
case PrintJobInfo.STATE_FAILED: {
|
|
||||||
mPrintJobPreference.setTitle(getString(
|
|
||||||
R.string.print_failed_state_title_template, info.getLabel()));
|
|
||||||
} break;
|
|
||||||
|
|
||||||
case PrintJobInfo.STATE_BLOCKED: {
|
|
||||||
if (!printJob.getInfo().isCancelling()) {
|
|
||||||
mPrintJobPreference.setTitle(getString(
|
|
||||||
R.string.print_blocked_state_title_template, info.getLabel()));
|
|
||||||
} else {
|
|
||||||
mPrintJobPreference.setTitle(getString(
|
|
||||||
R.string.print_cancelling_state_title_template, info.getLabel()));
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
}
|
|
||||||
|
|
||||||
mPrintJobPreference.setSummary(getString(R.string.print_job_summary,
|
|
||||||
info.getPrinterName(), DateUtils.formatSameDayTime(
|
|
||||||
info.getCreationTime(), info.getCreationTime(), DateFormat.SHORT,
|
|
||||||
DateFormat.SHORT)));
|
|
||||||
|
|
||||||
TypedArray a = getActivity().obtainStyledAttributes(new int[]{
|
|
||||||
android.R.attr.colorControlNormal});
|
|
||||||
int tintColor = a.getColor(0, 0);
|
|
||||||
a.recycle();
|
|
||||||
|
|
||||||
switch (info.getState()) {
|
|
||||||
case PrintJobInfo.STATE_QUEUED:
|
|
||||||
case PrintJobInfo.STATE_STARTED: {
|
|
||||||
Drawable icon = getActivity().getDrawable(com.android.internal.R.drawable.ic_print);
|
|
||||||
icon.setTint(tintColor);
|
|
||||||
mPrintJobPreference.setIcon(icon);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PrintJobInfo.STATE_FAILED:
|
|
||||||
case PrintJobInfo.STATE_BLOCKED: {
|
|
||||||
Drawable icon = getActivity().getDrawable(
|
|
||||||
com.android.internal.R.drawable.ic_print_error);
|
|
||||||
icon.setTint(tintColor);
|
|
||||||
mPrintJobPreference.setIcon(icon);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CharSequence status = info.getStatus(getPackageManager());
|
|
||||||
if (!TextUtils.isEmpty(status)) {
|
|
||||||
if (getPreferenceScreen().findPreference(PRINT_JOB_MESSAGE_PREFERENCE) == null) {
|
|
||||||
getPreferenceScreen().addPreference(mMessagePreference);
|
|
||||||
}
|
|
||||||
mMessagePreference.setSummary(status);
|
|
||||||
} else {
|
|
||||||
getPreferenceScreen().removePreference(mMessagePreference);
|
|
||||||
}
|
|
||||||
|
|
||||||
getActivity().invalidateOptionsMenu();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.print;
|
||||||
|
|
||||||
|
import static androidx.lifecycle.Lifecycle.Event.ON_START;
|
||||||
|
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Matchers.anyObject;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.print.PrintJob;
|
||||||
|
import android.print.PrintJobInfo;
|
||||||
|
import android.print.PrintManager;
|
||||||
|
|
||||||
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
public class PrintJobMessagePreferenceControllerTest {
|
||||||
|
private static final String PREF_KEY = "print_job_message_preference";
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PrintManager mPrintManager;
|
||||||
|
@Mock
|
||||||
|
private PrintJob mPrintJob;
|
||||||
|
@Mock
|
||||||
|
private PrintJobInfo mPrintJobInfo;
|
||||||
|
@Mock
|
||||||
|
private PreferenceScreen mScreen;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private PrintJobMessagePreferenceController mController;
|
||||||
|
private Preference mPreference;
|
||||||
|
private LifecycleOwner mLifecycleOwner;
|
||||||
|
private Lifecycle mLifecycle;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mPreference = new Preference(mContext);
|
||||||
|
when(mContext.getSystemService(Context.PRINT_SERVICE)).thenReturn(mPrintManager);
|
||||||
|
when(mPrintManager.getGlobalPrintManagerForUser(anyInt())).thenReturn(mPrintManager);
|
||||||
|
when(mPrintManager.getPrintJob(anyObject())).thenReturn(mPrintJob);
|
||||||
|
when(mPrintJob.getInfo()).thenReturn(mPrintJobInfo);
|
||||||
|
mController = new PrintJobMessagePreferenceController(mContext, PREF_KEY);
|
||||||
|
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
mLifecycleOwner = () -> mLifecycle;
|
||||||
|
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||||
|
mLifecycle.addObserver(mController);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onStartStop_shouldRegisterPrintStateListener() {
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_START);
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_STOP);
|
||||||
|
|
||||||
|
verify(mPrintManager).addPrintJobStateChangeListener(mController);
|
||||||
|
verify(mPrintManager).removePrintJobStateChangeListener(mController);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_visiblePreference() {
|
||||||
|
when(mPrintJobInfo.getStatus(anyObject())).thenReturn("TestPrint");
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_START);
|
||||||
|
|
||||||
|
assertThat(mPreference.isVisible()).isTrue();
|
||||||
|
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_STOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_invisiblePreference() {
|
||||||
|
when(mPrintJobInfo.getStatus(anyObject())).thenReturn(null);
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_START);
|
||||||
|
|
||||||
|
assertThat(mPreference.isVisible()).isFalse();
|
||||||
|
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_STOP);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,189 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.print;
|
||||||
|
|
||||||
|
import static androidx.lifecycle.Lifecycle.Event.ON_START;
|
||||||
|
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Matchers.anyObject;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.print.PrintJob;
|
||||||
|
import android.print.PrintJobInfo;
|
||||||
|
import android.print.PrintManager;
|
||||||
|
|
||||||
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
public class PrintJobPreferenceControllerTest {
|
||||||
|
private static final String PREF_KEY = "print_job_preference";
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PrintManager mPrintManager;
|
||||||
|
@Mock
|
||||||
|
private PrintJob mPrintJob;
|
||||||
|
@Mock
|
||||||
|
private PrintJobInfo mPrintJobInfo;
|
||||||
|
@Mock
|
||||||
|
private PreferenceScreen mScreen;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private LifecycleOwner mLifecycleOwner;
|
||||||
|
private Lifecycle mLifecycle;
|
||||||
|
private PrintJobPreferenceController mController;
|
||||||
|
private Preference mPreference;
|
||||||
|
private String mTestLabel;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mPreference = new Preference(mContext);
|
||||||
|
mTestLabel = "PrintTest";
|
||||||
|
when(mContext.getSystemService(Context.PRINT_SERVICE)).thenReturn(mPrintManager);
|
||||||
|
when(mPrintManager.getGlobalPrintManagerForUser(anyInt())).thenReturn(mPrintManager);
|
||||||
|
when(mPrintManager.getPrintJob(anyObject())).thenReturn(mPrintJob);
|
||||||
|
when(mPrintJob.getInfo()).thenReturn(mPrintJobInfo);
|
||||||
|
mController = new PrintJobPreferenceController(mContext, PREF_KEY);
|
||||||
|
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
|
when(mPrintJobInfo.getLabel()).thenReturn(mTestLabel);
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
mLifecycleOwner = () -> mLifecycle;
|
||||||
|
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||||
|
mLifecycle.addObserver(mController);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onStartStop_shouldRegisterPrintStateListener() {
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_START);
|
||||||
|
mLifecycle.handleLifecycleEvent(ON_STOP);
|
||||||
|
|
||||||
|
verify(mPrintManager).addPrintJobStateChangeListener(mController);
|
||||||
|
verify(mPrintManager).removePrintJobStateChangeListener(mController);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_CREATED() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_CREATED);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_configuring_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_QUEUED() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_QUEUED);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_printing_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_STARTED() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_STARTED);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_printing_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_QUEUED_and_jobInfo_CANCELLING() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_QUEUED);
|
||||||
|
when(mPrintJobInfo.isCancelling()).thenReturn(true);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_cancelling_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_STARTED_and_jobInfo_CANCELLING() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_STARTED);
|
||||||
|
when(mPrintJobInfo.isCancelling()).thenReturn(true);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_cancelling_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_FAILED() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_FAILED);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_failed_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_BLOCKED() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_BLOCKED);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_blocked_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUi_jobState_STATE_BLOCKED_and_jobInfo_CANCELLING() {
|
||||||
|
when(mPrintJobInfo.getState()).thenReturn(PrintJobInfo.STATE_BLOCKED);
|
||||||
|
when(mPrintJobInfo.isCancelling()).thenReturn(true);
|
||||||
|
|
||||||
|
mController.onStart();
|
||||||
|
String title = mContext.getString(
|
||||||
|
R.string.print_cancelling_state_title_template, mTestLabel);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(title);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user