diff options
81 files changed, 3429 insertions, 316 deletions
diff --git a/Android.mk b/Android.mk index 024b2fdcaaf4..1469c2cdf93e 100644 --- a/Android.mk +++ b/Android.mk @@ -251,10 +251,13 @@ LOCAL_SRC_FILES += \ core/java/android/print/IPrintDocumentAdapterObserver.aidl \ core/java/android/print/IPrintJobStateChangeListener.aidl \ core/java/android/print/IPrintServicesChangeListener.aidl \ + core/java/android/printservice/recommendation/IRecommendationsChangeListener.aidl \ core/java/android/print/IPrintManager.aidl \ core/java/android/print/IPrintSpooler.aidl \ core/java/android/print/IPrintSpoolerCallbacks.aidl \ core/java/android/print/IPrintSpoolerClient.aidl \ + core/java/android/printservice/recommendation/IRecommendationServiceCallbacks.aidl \ + core/java/android/printservice/recommendation/IRecommendationService.aidl \ core/java/android/print/IWriteResultCallback.aidl \ core/java/android/printservice/IPrintService.aidl \ core/java/android/printservice/IPrintServiceClient.aidl \ @@ -565,6 +568,7 @@ aidl_files := \ frameworks/base/core/java/android/print/PrintJobInfo.aidl \ frameworks/base/core/java/android/print/PrinterInfo.aidl \ frameworks/base/core/java/android/print/PrintJobId.aidl \ + frameworks/base/core/java/android/printservice/recommendation/RecommendationInfo.aidl \ frameworks/base/core/java/android/hardware/usb/UsbDevice.aidl \ frameworks/base/core/java/android/hardware/usb/UsbInterface.aidl \ frameworks/base/core/java/android/hardware/usb/UsbEndpoint.aidl \ diff --git a/api/current.txt b/api/current.txt index 5ceb75974ba6..6a1bb02a57c1 100644 --- a/api/current.txt +++ b/api/current.txt @@ -3425,7 +3425,7 @@ package android.app { method public boolean dispatchTouchEvent(android.view.MotionEvent); method public boolean dispatchTrackballEvent(android.view.MotionEvent); method public void dump(java.lang.String, java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); - method public void enterPictureInPicture(); + method public void enterPictureInPictureMode(); method public android.view.View findViewById(int); method public void finish(); method public void finishActivity(int); @@ -3465,15 +3465,16 @@ package android.app { method public android.view.Window getWindow(); method public android.view.WindowManager getWindowManager(); method public boolean hasWindowFocus(); - method public boolean inMultiWindow(); - method public boolean inPictureInPicture(); method public void invalidateOptionsMenu(); method public boolean isChangingConfigurations(); method public final boolean isChild(); method public boolean isDestroyed(); method public boolean isFinishing(); method public boolean isImmersive(); + method public boolean isInMultiWindowMode(); + method public boolean isInPictureInPictureMode(); method public boolean isLocalVoiceInteractionSupported(); + method public boolean isOverlayWithDecorCaptionEnabled(); method public boolean isTaskRoot(); method public boolean isVoiceInteraction(); method public boolean isVoiceInteractionRoot(); @@ -3520,7 +3521,7 @@ package android.app { method public void onLowMemory(); method public boolean onMenuItemSelected(int, android.view.MenuItem); method public boolean onMenuOpened(int, android.view.Menu); - method public void onMultiWindowChanged(boolean); + method public void onMultiWindowModeChanged(boolean); method public boolean onNavigateUp(); method public boolean onNavigateUpFromChild(android.app.Activity); method protected void onNewIntent(android.content.Intent); @@ -3528,7 +3529,7 @@ package android.app { method public void onOptionsMenuClosed(android.view.Menu); method public void onPanelClosed(int, android.view.Menu); method protected void onPause(); - method public void onPictureInPictureChanged(boolean); + method public void onPictureInPictureModeChanged(boolean); method protected void onPostCreate(android.os.Bundle); method public void onPostCreate(android.os.Bundle, android.os.PersistableBundle); method protected void onPostResume(); @@ -3566,7 +3567,6 @@ package android.app { method public android.view.ActionMode onWindowStartingActionMode(android.view.ActionMode.Callback, int); method public void openContextMenu(android.view.View); method public void openOptionsMenu(); - method public void overlayWithDecorCaption(boolean); method public void overridePendingTransition(int, int); method public void postponeEnterTransition(); method public void recreate(); @@ -3595,6 +3595,7 @@ package android.app { method public void setImmersive(boolean); method public void setIntent(android.content.Intent); method public final void setMediaController(android.media.session.MediaController); + method public void setOverlayWithDecorCaptionEnabled(boolean); method public final deprecated void setProgress(int); method public final deprecated void setProgressBarIndeterminate(boolean); method public final deprecated void setProgressBarIndeterminateVisibility(boolean); @@ -4437,11 +4438,11 @@ package android.app { method public void onInflate(android.content.Context, android.util.AttributeSet, android.os.Bundle); method public deprecated void onInflate(android.app.Activity, android.util.AttributeSet, android.os.Bundle); method public void onLowMemory(); - method public void onMultiWindowChanged(boolean); + method public void onMultiWindowModeChanged(boolean); method public boolean onOptionsItemSelected(android.view.MenuItem); method public void onOptionsMenuClosed(android.view.Menu); method public void onPause(); - method public void onPictureInPictureChanged(boolean); + method public void onPictureInPictureModeChanged(boolean); method public void onPrepareOptionsMenu(android.view.Menu); method public void onRequestPermissionsResult(int, java.lang.String[], int[]); method public void onResume(); @@ -4522,11 +4523,11 @@ package android.app { method public void dispatchDestroy(); method public void dispatchDestroyView(); method public void dispatchLowMemory(); - method public void dispatchMultiWindowChanged(boolean); + method public void dispatchMultiWindowModeChanged(boolean); method public boolean dispatchOptionsItemSelected(android.view.MenuItem); method public void dispatchOptionsMenuClosed(android.view.Menu); method public void dispatchPause(); - method public void dispatchPictureInPictureChanged(boolean); + method public void dispatchPictureInPictureModeChanged(boolean); method public boolean dispatchPrepareOptionsMenu(android.view.Menu); method public void dispatchResume(); method public void dispatchStart(); @@ -29129,6 +29130,7 @@ package android.os { field public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1; // 0x1 field public static final deprecated int SCREEN_BRIGHT_WAKE_LOCK = 10; // 0xa field public static final deprecated int SCREEN_DIM_WAKE_LOCK = 6; // 0x6 + field public static final int SUSTAINED_PERFORMANCE_WAKE_LOCK = 256; // 0x100 } public final class PowerManager.WakeLock { diff --git a/api/system-current.txt b/api/system-current.txt index bd77046c15d3..6bf0717b32c5 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -42,6 +42,7 @@ package android { field public static final java.lang.String BIND_MIDI_DEVICE_SERVICE = "android.permission.BIND_MIDI_DEVICE_SERVICE"; field public static final java.lang.String BIND_NFC_SERVICE = "android.permission.BIND_NFC_SERVICE"; field public static final java.lang.String BIND_NOTIFICATION_LISTENER_SERVICE = "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"; + field public static final java.lang.String BIND_PRINT_RECOMMENDATION_SERVICE = "android.permission.BIND_PRINT_RECOMMENDATION_SERVICE"; field public static final java.lang.String BIND_PRINT_SERVICE = "android.permission.BIND_PRINT_SERVICE"; field public static final java.lang.String BIND_QUICK_SETTINGS_TILE = "android.permission.BIND_QUICK_SETTINGS_TILE"; field public static final java.lang.String BIND_REMOTEVIEWS = "android.permission.BIND_REMOTEVIEWS"; @@ -3540,7 +3541,7 @@ package android.app { method public boolean dispatchTouchEvent(android.view.MotionEvent); method public boolean dispatchTrackballEvent(android.view.MotionEvent); method public void dump(java.lang.String, java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); - method public void enterPictureInPicture(); + method public void enterPictureInPictureMode(); method public android.view.View findViewById(int); method public void finish(); method public void finishActivity(int); @@ -3580,8 +3581,6 @@ package android.app { method public android.view.Window getWindow(); method public android.view.WindowManager getWindowManager(); method public boolean hasWindowFocus(); - method public boolean inMultiWindow(); - method public boolean inPictureInPicture(); method public void invalidateOptionsMenu(); method public boolean isBackgroundVisibleBehind(); method public boolean isChangingConfigurations(); @@ -3589,7 +3588,10 @@ package android.app { method public boolean isDestroyed(); method public boolean isFinishing(); method public boolean isImmersive(); + method public boolean isInMultiWindowMode(); + method public boolean isInPictureInPictureMode(); method public boolean isLocalVoiceInteractionSupported(); + method public boolean isOverlayWithDecorCaptionEnabled(); method public boolean isTaskRoot(); method public boolean isVoiceInteraction(); method public boolean isVoiceInteractionRoot(); @@ -3637,7 +3639,7 @@ package android.app { method public void onLowMemory(); method public boolean onMenuItemSelected(int, android.view.MenuItem); method public boolean onMenuOpened(int, android.view.Menu); - method public void onMultiWindowChanged(boolean); + method public void onMultiWindowModeChanged(boolean); method public boolean onNavigateUp(); method public boolean onNavigateUpFromChild(android.app.Activity); method protected void onNewIntent(android.content.Intent); @@ -3645,7 +3647,7 @@ package android.app { method public void onOptionsMenuClosed(android.view.Menu); method public void onPanelClosed(int, android.view.Menu); method protected void onPause(); - method public void onPictureInPictureChanged(boolean); + method public void onPictureInPictureModeChanged(boolean); method protected void onPostCreate(android.os.Bundle); method public void onPostCreate(android.os.Bundle, android.os.PersistableBundle); method protected void onPostResume(); @@ -3683,7 +3685,6 @@ package android.app { method public android.view.ActionMode onWindowStartingActionMode(android.view.ActionMode.Callback, int); method public void openContextMenu(android.view.View); method public void openOptionsMenu(); - method public void overlayWithDecorCaption(boolean); method public void overridePendingTransition(int, int); method public void postponeEnterTransition(); method public void recreate(); @@ -3712,6 +3713,7 @@ package android.app { method public void setImmersive(boolean); method public void setIntent(android.content.Intent); method public final void setMediaController(android.media.session.MediaController); + method public void setOverlayWithDecorCaptionEnabled(boolean); method public final deprecated void setProgress(int); method public final deprecated void setProgressBarIndeterminate(boolean); method public final deprecated void setProgressBarIndeterminateVisibility(boolean); @@ -4569,11 +4571,11 @@ package android.app { method public void onInflate(android.content.Context, android.util.AttributeSet, android.os.Bundle); method public deprecated void onInflate(android.app.Activity, android.util.AttributeSet, android.os.Bundle); method public void onLowMemory(); - method public void onMultiWindowChanged(boolean); + method public void onMultiWindowModeChanged(boolean); method public boolean onOptionsItemSelected(android.view.MenuItem); method public void onOptionsMenuClosed(android.view.Menu); method public void onPause(); - method public void onPictureInPictureChanged(boolean); + method public void onPictureInPictureModeChanged(boolean); method public void onPrepareOptionsMenu(android.view.Menu); method public void onRequestPermissionsResult(int, java.lang.String[], int[]); method public void onResume(); @@ -4654,11 +4656,11 @@ package android.app { method public void dispatchDestroy(); method public void dispatchDestroyView(); method public void dispatchLowMemory(); - method public void dispatchMultiWindowChanged(boolean); + method public void dispatchMultiWindowModeChanged(boolean); method public boolean dispatchOptionsItemSelected(android.view.MenuItem); method public void dispatchOptionsMenuClosed(android.view.Menu); method public void dispatchPause(); - method public void dispatchPictureInPictureChanged(boolean); + method public void dispatchPictureInPictureModeChanged(boolean); method public boolean dispatchPrepareOptionsMenu(android.view.Menu); method public void dispatchResume(); method public void dispatchStart(); @@ -31376,6 +31378,7 @@ package android.os { field public static final deprecated int SCREEN_BRIGHT_WAKE_LOCK = 10; // 0xa field public static final deprecated int SCREEN_DIM_WAKE_LOCK = 6; // 0x6 field public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3; // 0x3 + field public static final int SUSTAINED_PERFORMANCE_WAKE_LOCK = 256; // 0x100 field public static final int USER_ACTIVITY_EVENT_BUTTON = 1; // 0x1 field public static final int USER_ACTIVITY_EVENT_OTHER = 0; // 0x0 field public static final int USER_ACTIVITY_EVENT_TOUCH = 2; // 0x2 @@ -32700,6 +32703,30 @@ package android.printservice { } +package android.printservice.recommendation { + + public final class RecommendationInfo implements android.os.Parcelable { + ctor public RecommendationInfo(java.lang.CharSequence, java.lang.CharSequence, int, boolean); + method public int describeContents(); + method public java.lang.CharSequence getName(); + method public int getNumDiscoveredPrinters(); + method public java.lang.CharSequence getPackageName(); + method public boolean recommendsMultiVendorService(); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator<android.printservice.recommendation.RecommendationInfo> CREATOR; + } + + public abstract class RecommendationService extends android.app.Service { + ctor public RecommendationService(); + method public final android.os.IBinder onBind(android.content.Intent); + method public abstract void onConnected(); + method public abstract void onDisconnected(); + method public final boolean onUnbind(android.content.Intent); + method public final void updateRecommendations(java.util.List<android.printservice.recommendation.RecommendationInfo>); + } + +} + package android.provider { public final class AlarmClock { diff --git a/api/test-current.txt b/api/test-current.txt index 8a49756d9f6b..3d03d5ba84fb 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -3425,7 +3425,7 @@ package android.app { method public boolean dispatchTouchEvent(android.view.MotionEvent); method public boolean dispatchTrackballEvent(android.view.MotionEvent); method public void dump(java.lang.String, java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); - method public void enterPictureInPicture(); + method public void enterPictureInPictureMode(); method public android.view.View findViewById(int); method public void finish(); method public void finishActivity(int); @@ -3465,15 +3465,16 @@ package android.app { method public android.view.Window getWindow(); method public android.view.WindowManager getWindowManager(); method public boolean hasWindowFocus(); - method public boolean inMultiWindow(); - method public boolean inPictureInPicture(); method public void invalidateOptionsMenu(); method public boolean isChangingConfigurations(); method public final boolean isChild(); method public boolean isDestroyed(); method public boolean isFinishing(); method public boolean isImmersive(); + method public boolean isInMultiWindowMode(); + method public boolean isInPictureInPictureMode(); method public boolean isLocalVoiceInteractionSupported(); + method public boolean isOverlayWithDecorCaptionEnabled(); method public boolean isTaskRoot(); method public boolean isVoiceInteraction(); method public boolean isVoiceInteractionRoot(); @@ -3520,7 +3521,7 @@ package android.app { method public void onLowMemory(); method public boolean onMenuItemSelected(int, android.view.MenuItem); method public boolean onMenuOpened(int, android.view.Menu); - method public void onMultiWindowChanged(boolean); + method public void onMultiWindowModeChanged(boolean); method public boolean onNavigateUp(); method public boolean onNavigateUpFromChild(android.app.Activity); method protected void onNewIntent(android.content.Intent); @@ -3528,7 +3529,7 @@ package android.app { method public void onOptionsMenuClosed(android.view.Menu); method public void onPanelClosed(int, android.view.Menu); method protected void onPause(); - method public void onPictureInPictureChanged(boolean); + method public void onPictureInPictureModeChanged(boolean); method protected void onPostCreate(android.os.Bundle); method public void onPostCreate(android.os.Bundle, android.os.PersistableBundle); method protected void onPostResume(); @@ -3566,7 +3567,6 @@ package android.app { method public android.view.ActionMode onWindowStartingActionMode(android.view.ActionMode.Callback, int); method public void openContextMenu(android.view.View); method public void openOptionsMenu(); - method public void overlayWithDecorCaption(boolean); method public void overridePendingTransition(int, int); method public void postponeEnterTransition(); method public void recreate(); @@ -3595,6 +3595,7 @@ package android.app { method public void setImmersive(boolean); method public void setIntent(android.content.Intent); method public final void setMediaController(android.media.session.MediaController); + method public void setOverlayWithDecorCaptionEnabled(boolean); method public final deprecated void setProgress(int); method public final deprecated void setProgressBarIndeterminate(boolean); method public final deprecated void setProgressBarIndeterminateVisibility(boolean); @@ -4437,11 +4438,11 @@ package android.app { method public void onInflate(android.content.Context, android.util.AttributeSet, android.os.Bundle); method public deprecated void onInflate(android.app.Activity, android.util.AttributeSet, android.os.Bundle); method public void onLowMemory(); - method public void onMultiWindowChanged(boolean); + method public void onMultiWindowModeChanged(boolean); method public boolean onOptionsItemSelected(android.view.MenuItem); method public void onOptionsMenuClosed(android.view.Menu); method public void onPause(); - method public void onPictureInPictureChanged(boolean); + method public void onPictureInPictureModeChanged(boolean); method public void onPrepareOptionsMenu(android.view.Menu); method public void onRequestPermissionsResult(int, java.lang.String[], int[]); method public void onResume(); @@ -4522,11 +4523,11 @@ package android.app { method public void dispatchDestroy(); method public void dispatchDestroyView(); method public void dispatchLowMemory(); - method public void dispatchMultiWindowChanged(boolean); + method public void dispatchMultiWindowModeChanged(boolean); method public boolean dispatchOptionsItemSelected(android.view.MenuItem); method public void dispatchOptionsMenuClosed(android.view.Menu); method public void dispatchPause(); - method public void dispatchPictureInPictureChanged(boolean); + method public void dispatchPictureInPictureModeChanged(boolean); method public boolean dispatchPrepareOptionsMenu(android.view.Menu); method public void dispatchResume(); method public void dispatchStart(); @@ -29194,6 +29195,7 @@ package android.os { field public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1; // 0x1 field public static final deprecated int SCREEN_BRIGHT_WAKE_LOCK = 10; // 0xa field public static final deprecated int SCREEN_DIM_WAKE_LOCK = 6; // 0x6 + field public static final int SUSTAINED_PERFORMANCE_WAKE_LOCK = 256; // 0x100 } public final class PowerManager.WakeLock { diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 0d387e660f8a..0410a6e804cd 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -51,12 +51,7 @@ import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; -import android.graphics.drawable.InsetDrawable; -import android.graphics.drawable.LayerDrawable; -import android.graphics.drawable.ShapeDrawable; import android.hardware.input.InputManager; import android.media.AudioManager; import android.media.session.MediaController; @@ -121,7 +116,6 @@ import android.widget.Toolbar; import com.android.internal.app.IVoiceInteractor; import com.android.internal.app.ToolbarActionBar; import com.android.internal.app.WindowDecorActionBar; -import com.android.internal.policy.DecorView; import com.android.internal.policy.PhoneWindow; import java.io.FileDescriptor; @@ -1855,15 +1849,15 @@ public class Activity extends ContextThemeWrapper * visa-versa. * @see android.R.attr#resizeableActivity * - * @param inMultiWindow True if the activity is in multi-window mode. + * @param isInMultiWindowMode True if the activity is in multi-window mode. */ @CallSuper - public void onMultiWindowChanged(boolean inMultiWindow) { + public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { if (DEBUG_LIFECYCLE) Slog.v(TAG, - "onMultiWindowChanged " + this + ": " + inMultiWindow); - mFragments.dispatchMultiWindowChanged(inMultiWindow); + "onMultiWindowModeChanged " + this + ": " + isInMultiWindowMode); + mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode); if (mWindow != null) { - mWindow.onMultiWindowChanged(); + mWindow.onMultiWindowModeChanged(); } } @@ -1873,9 +1867,9 @@ public class Activity extends ContextThemeWrapper * * @return True if the activity is in multi-window mode. */ - public boolean inMultiWindow() { + public boolean isInMultiWindowMode() { try { - return ActivityManagerNative.getDefault().inMultiWindow(mToken); + return ActivityManagerNative.getDefault().isInMultiWindowMode(mToken); } catch (RemoteException e) { } return false; @@ -1885,13 +1879,13 @@ public class Activity extends ContextThemeWrapper * Called by the system when the activity changes to and from picture-in-picture mode. * @see android.R.attr#supportsPictureInPicture * - * @param inPictureInPicture True if the activity is in picture-in-picture mode. + * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode. */ @CallSuper - public void onPictureInPictureChanged(boolean inPictureInPicture) { + public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { if (DEBUG_LIFECYCLE) Slog.v(TAG, - "onPictureInPictureChanged " + this + ": " + inPictureInPicture); - mFragments.dispatchPictureInPictureChanged(inPictureInPicture); + "onPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode); + mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); } /** @@ -1900,9 +1894,9 @@ public class Activity extends ContextThemeWrapper * * @return True if the activity is in picture-in-picture mode. */ - public boolean inPictureInPicture() { + public boolean isInPictureInPictureMode() { try { - return ActivityManagerNative.getDefault().inPictureInPicture(mToken); + return ActivityManagerNative.getDefault().isInPictureInPictureMode(mToken); } catch (RemoteException e) { } return false; @@ -1912,9 +1906,9 @@ public class Activity extends ContextThemeWrapper * Puts the activity in picture-in-picture mode. * @see android.R.attr#supportsPictureInPicture */ - public void enterPictureInPicture() { + public void enterPictureInPictureMode() { try { - ActivityManagerNative.getDefault().enterPictureInPicture(mToken); + ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken); } catch (RemoteException e) { } } @@ -6915,14 +6909,25 @@ public class Activity extends ContextThemeWrapper } /** + * Check whether the caption on freeform windows is displayed directly on the content. + * + * @return True if caption is displayed on content, false if it pushes the content down. + * + * @see {@link #setOverlayWithDecorCaptionEnabled(boolean)} + */ + public boolean isOverlayWithDecorCaptionEnabled() { + return mWindow.isOverlayWithDecorCaptionEnabled(); + } + + /** * Set whether the caption should displayed directly on the content rather than push it down. * * This affects only freeform windows since they display the caption and only the main * window of the activity. The caption is used to drag the window around and also shows * maximize and close action buttons. */ - public void overlayWithDecorCaption(boolean overlay) { - mWindow.setOverlayDecorCaption(overlay); + public void setOverlayWithDecorCaptionEnabled(boolean enabled) { + mWindow.setOverlayWithDecorCaptionEnabled(enabled); } /** diff --git a/core/java/android/app/ActivityManagerNative.java b/core/java/android/app/ActivityManagerNative.java index 4bf48a338956..336218522c77 100644 --- a/core/java/android/app/ActivityManagerNative.java +++ b/core/java/android/app/ActivityManagerNative.java @@ -2890,7 +2890,7 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM case IN_MULTI_WINDOW_TRANSACTION: { data.enforceInterface(IActivityManager.descriptor); final IBinder token = data.readStrongBinder(); - final boolean inMultiWindow = inMultiWindow(token); + final boolean inMultiWindow = isInMultiWindowMode(token); reply.writeNoException(); reply.writeInt(inMultiWindow ? 1 : 0); return true; @@ -2898,7 +2898,7 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM case IN_PICTURE_IN_PICTURE_TRANSACTION: { data.enforceInterface(IActivityManager.descriptor); final IBinder token = data.readStrongBinder(); - final boolean inPip = inPictureInPicture(token); + final boolean inPip = isInPictureInPictureMode(token); reply.writeNoException(); reply.writeInt(inPip ? 1 : 0); return true; @@ -2906,7 +2906,7 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM case ENTER_PICTURE_IN_PICTURE_TRANSACTION: { data.enforceInterface(IActivityManager.descriptor); final IBinder token = data.readStrongBinder(); - enterPictureInPicture(token); + enterPictureInPictureMode(token); reply.writeNoException(); return true; } @@ -6837,7 +6837,7 @@ class ActivityManagerProxy implements IActivityManager } @Override - public boolean inMultiWindow(IBinder token) throws RemoteException { + public boolean isInMultiWindowMode(IBinder token) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); @@ -6851,7 +6851,7 @@ class ActivityManagerProxy implements IActivityManager } @Override - public boolean inPictureInPicture(IBinder token) throws RemoteException { + public boolean isInPictureInPictureMode(IBinder token) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); @@ -6865,7 +6865,7 @@ class ActivityManagerProxy implements IActivityManager } @Override - public void enterPictureInPicture(IBinder token) throws RemoteException { + public void enterPictureInPictureMode(IBinder token) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 9cee81551670..5b94696c1461 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -1284,15 +1284,15 @@ public final class ActivityThread { } @Override - public void scheduleMultiWindowChanged(IBinder token, boolean inMultiWindow) + public void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode) throws RemoteException { - sendMessage(H.MULTI_WINDOW_CHANGED, token, inMultiWindow ? 1 : 0); + sendMessage(H.MULTI_WINDOW_MODE_CHANGED, token, isInMultiWindowMode ? 1 : 0); } @Override - public void schedulePictureInPictureChanged(IBinder token, boolean inPip) + public void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode) throws RemoteException { - sendMessage(H.PICTURE_IN_PICTURE_CHANGED, token, inPip ? 1 : 0); + sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, token, isInPipMode ? 1 : 0); } @Override @@ -1364,8 +1364,8 @@ public final class ActivityThread { public static final int ENTER_ANIMATION_COMPLETE = 149; public static final int START_BINDER_TRACKING = 150; public static final int STOP_BINDER_TRACKING_AND_DUMP = 151; - public static final int MULTI_WINDOW_CHANGED = 152; - public static final int PICTURE_IN_PICTURE_CHANGED = 153; + public static final int MULTI_WINDOW_MODE_CHANGED = 152; + public static final int PICTURE_IN_PICTURE_MODE_CHANGED = 153; public static final int LOCAL_VOICE_INTERACTION_STARTED = 154; String codeToString(int code) { @@ -1420,8 +1420,8 @@ public final class ActivityThread { case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND"; case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED"; case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE"; - case MULTI_WINDOW_CHANGED: return "MULTI_WINDOW_CHANGED"; - case PICTURE_IN_PICTURE_CHANGED: return "PICTURE_IN_PICTURE_CHANGED"; + case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED"; + case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED"; case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED"; } } @@ -1666,11 +1666,11 @@ public final class ActivityThread { case STOP_BINDER_TRACKING_AND_DUMP: handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj); break; - case MULTI_WINDOW_CHANGED: - handleMultiWindowChanged((IBinder) msg.obj, msg.arg1 == 1); + case MULTI_WINDOW_MODE_CHANGED: + handleMultiWindowModeChanged((IBinder) msg.obj, msg.arg1 == 1); break; - case PICTURE_IN_PICTURE_CHANGED: - handlePictureInPictureChanged((IBinder) msg.obj, msg.arg1 == 1); + case PICTURE_IN_PICTURE_MODE_CHANGED: + handlePictureInPictureModeChanged((IBinder) msg.obj, msg.arg1 == 1); break; case LOCAL_VOICE_INTERACTION_STARTED: handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1, @@ -2924,17 +2924,17 @@ public final class ActivityThread { } } - private void handleMultiWindowChanged(IBinder token, boolean inMultiWindow) { + private void handleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode) { final ActivityClientRecord r = mActivities.get(token); if (r != null) { - r.activity.onMultiWindowChanged(inMultiWindow); + r.activity.onMultiWindowModeChanged(isInMultiWindowMode); } } - private void handlePictureInPictureChanged(IBinder token, boolean inPip) { + private void handlePictureInPictureModeChanged(IBinder token, boolean isInPipMode) { final ActivityClientRecord r = mActivities.get(token); if (r != null) { - r.activity.onPictureInPictureChanged(inPip); + r.activity.onPictureInPictureModeChanged(isInPipMode); } } diff --git a/core/java/android/app/ApplicationThreadNative.java b/core/java/android/app/ApplicationThreadNative.java index 744ddf704161..ea86dd0558d6 100644 --- a/core/java/android/app/ApplicationThreadNative.java +++ b/core/java/android/app/ApplicationThreadNative.java @@ -736,7 +736,7 @@ public abstract class ApplicationThreadNative extends Binder data.enforceInterface(IApplicationThread.descriptor); final IBinder b = data.readStrongBinder(); final boolean inMultiWindow = data.readInt() != 0; - scheduleMultiWindowChanged(b, inMultiWindow); + scheduleMultiWindowModeChanged(b, inMultiWindow); return true; } @@ -745,7 +745,7 @@ public abstract class ApplicationThreadNative extends Binder data.enforceInterface(IApplicationThread.descriptor); final IBinder b = data.readStrongBinder(); final boolean inPip = data.readInt() != 0; - schedulePictureInPictureChanged(b, inPip); + schedulePictureInPictureModeChanged(b, inPip); return true; } @@ -1498,24 +1498,24 @@ class ApplicationThreadProxy implements IApplicationThread { } @Override - public final void scheduleMultiWindowChanged( - IBinder token, boolean inMultiWindow) throws RemoteException { + public final void scheduleMultiWindowModeChanged( + IBinder token, boolean isInMultiWindowMode) throws RemoteException { Parcel data = Parcel.obtain(); data.writeInterfaceToken(IApplicationThread.descriptor); data.writeStrongBinder(token); - data.writeInt(inMultiWindow ? 1 : 0); + data.writeInt(isInMultiWindowMode ? 1 : 0); mRemote.transact(SCHEDULE_MULTI_WINDOW_CHANGED_TRANSACTION, data, null, IBinder.FLAG_ONEWAY); data.recycle(); } @Override - public final void schedulePictureInPictureChanged(IBinder token, boolean inPip) + public final void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode) throws RemoteException { Parcel data = Parcel.obtain(); data.writeInterfaceToken(IApplicationThread.descriptor); data.writeStrongBinder(token); - data.writeInt(inPip ? 1 : 0); + data.writeInt(isInPipMode ? 1 : 0); mRemote.transact(SCHEDULE_PICTURE_IN_PICTURE_CHANGED_TRANSACTION, data, null, IBinder.FLAG_ONEWAY); data.recycle(); diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java index f7a4557f8760..bb3c7191f4e3 100644 --- a/core/java/android/app/Fragment.java +++ b/core/java/android/app/Fragment.java @@ -1582,21 +1582,21 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene /** * Called when the Fragment's activity changes from fullscreen mode to multi-window mode and - * visa-versa. This is generally tied to {@link Activity#onMultiWindowChanged} of the containing - * Activity. + * visa-versa. This is generally tied to {@link Activity#onMultiWindowModeChanged} of the + * containing Activity. * - * @param inMultiWindow True if the activity is in multi-window mode. + * @param isInMultiWindowMode True if the activity is in multi-window mode. */ - public void onMultiWindowChanged(boolean inMultiWindow) { + public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { } /** * Called by the system when the activity changes to and from picture-in-picture mode. This is - * generally tied to {@link Activity#onPictureInPictureChanged} of the containing Activity. + * generally tied to {@link Activity#onPictureInPictureModeChanged} of the containing Activity. * - * @param inPictureInPicture True if the activity is in picture-in-picture mode. + * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode. */ - public void onPictureInPictureChanged(boolean inPictureInPicture) { + public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { } public void onConfigurationChanged(Configuration newConfig) { @@ -2334,17 +2334,17 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene } } - void performMultiWindowChanged(boolean inMultiWindow) { - onMultiWindowChanged(inMultiWindow); + void performMultiWindowModeChanged(boolean isInMultiWindowMode) { + onMultiWindowModeChanged(isInMultiWindowMode); if (mChildFragmentManager != null) { - mChildFragmentManager.dispatchMultiWindowChanged(inMultiWindow); + mChildFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode); } } - void performPictureInPictureChanged(boolean inPictureInPicture) { - onPictureInPictureChanged(inPictureInPicture); + void performPictureInPictureModeChanged(boolean isInPictureInPictureMode) { + onPictureInPictureModeChanged(isInPictureInPictureMode); if (mChildFragmentManager != null) { - mChildFragmentManager.dispatchPictureInPictureChanged(inPictureInPicture); + mChildFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); } } diff --git a/core/java/android/app/FragmentController.java b/core/java/android/app/FragmentController.java index 57b0ff134818..b3d2df5424bb 100644 --- a/core/java/android/app/FragmentController.java +++ b/core/java/android/app/FragmentController.java @@ -247,10 +247,10 @@ public class FragmentController { * the activity changed. * <p>Call when the multi-window mode of the activity changed. * - * @see Fragment#onMultiWindowChanged + * @see Fragment#onMultiWindowModeChanged */ - public void dispatchMultiWindowChanged(boolean inMultiWindow) { - mHost.mFragmentManager.dispatchMultiWindowChanged(inMultiWindow); + public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { + mHost.mFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode); } /** @@ -258,10 +258,10 @@ public class FragmentController { * mode of the activity changed. * <p>Call when the picture-in-picture mode of the activity changed. * - * @see Fragment#onPictureInPictureChanged + * @see Fragment#onPictureInPictureModeChanged */ - public void dispatchPictureInPictureChanged(boolean inPictureInPicture) { - mHost.mFragmentManager.dispatchPictureInPictureChanged(inPictureInPicture); + public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { + mHost.mFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); } /** diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java index 2852baf9c21b..8369f17c877f 100644 --- a/core/java/android/app/FragmentManager.java +++ b/core/java/android/app/FragmentManager.java @@ -2069,26 +2069,26 @@ final class FragmentManagerImpl extends FragmentManager implements LayoutInflate mParent = null; } - public void dispatchMultiWindowChanged(boolean inMultiWindow) { + public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { if (mAdded == null) { return; } for (int i = mAdded.size() - 1; i >= 0; --i) { final Fragment f = mAdded.get(i); if (f != null) { - f.performMultiWindowChanged(inMultiWindow); + f.performMultiWindowModeChanged(isInMultiWindowMode); } } } - public void dispatchPictureInPictureChanged(boolean inPictureInPicture) { + public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { if (mAdded == null) { return; } for (int i = mAdded.size() - 1; i >= 0; --i) { final Fragment f = mAdded.get(i); if (f != null) { - f.performPictureInPictureChanged(inPictureInPicture); + f.performPictureInPictureModeChanged(isInPictureInPictureMode); } } } diff --git a/core/java/android/app/IActivityManager.java b/core/java/android/app/IActivityManager.java index 417c0679e233..8ee6fd0a9472 100644 --- a/core/java/android/app/IActivityManager.java +++ b/core/java/android/app/IActivityManager.java @@ -623,11 +623,11 @@ public interface IActivityManager extends IInterface { public int getAppStartMode(int uid, String packageName) throws RemoteException; - public boolean inMultiWindow(IBinder token) throws RemoteException; + public boolean isInMultiWindowMode(IBinder token) throws RemoteException; - public boolean inPictureInPicture(IBinder token) throws RemoteException; + public boolean isInPictureInPictureMode(IBinder token) throws RemoteException; - public void enterPictureInPicture(IBinder token) throws RemoteException; + public void enterPictureInPictureMode(IBinder token) throws RemoteException; public int setVrMode(IBinder token, boolean enabled, ComponentName packageName) throws RemoteException; diff --git a/core/java/android/app/IApplicationThread.java b/core/java/android/app/IApplicationThread.java index 628bde0891ef..a3b263876725 100644 --- a/core/java/android/app/IApplicationThread.java +++ b/core/java/android/app/IApplicationThread.java @@ -158,8 +158,8 @@ public interface IApplicationThread extends IInterface { void notifyCleartextNetwork(byte[] firstPacket) throws RemoteException; void startBinderTracking() throws RemoteException; void stopBinderTrackingAndDump(FileDescriptor fd) throws RemoteException; - void scheduleMultiWindowChanged(IBinder token, boolean multiWindowMode) throws RemoteException; - void schedulePictureInPictureChanged(IBinder token, boolean multiWindowMode) throws RemoteException; + void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode) throws RemoteException; + void schedulePictureInPictureModeChanged(IBinder token, boolean isInPictureInPictureMode) throws RemoteException; void scheduleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor voiceInteractor) throws RemoteException; String descriptor = "android.app.IApplicationThread"; diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 36c82e509217..6b4771ce1939 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -4889,15 +4889,30 @@ public class DevicePolicyManager { * @throws SecurityException if {@code admin} is not a device or profile owner. */ public Bundle getUserRestrictions(@NonNull ComponentName admin) { - return getUserRestrictions(admin, myUserId()); + Bundle ret = null; + if (mService != null) { + try { + ret = mService.getUserRestrictions(admin); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + return ret == null ? new Bundle() : ret; } - /** @hide per-user version */ - public Bundle getUserRestrictions(@NonNull ComponentName admin, int userHandle) { + /** + * Called by the system to get the user restrictions for a user. + * + * @param admin Which {@link DeviceAdminReceiver} this request is associated with. + * @param userHandle user id the admin is running as. + * + * @hide + */ + public Bundle getUserRestrictionsForUser(@NonNull ComponentName admin, int userHandle) { Bundle ret = null; if (mService != null) { try { - ret = mService.getUserRestrictions(admin, userHandle); + ret = mService.getUserRestrictionsForUser(admin, userHandle); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } diff --git a/core/java/android/app/admin/IDevicePolicyManager.aidl b/core/java/android/app/admin/IDevicePolicyManager.aidl index 8be52d8d9d4f..c3d5ed92a912 100644 --- a/core/java/android/app/admin/IDevicePolicyManager.aidl +++ b/core/java/android/app/admin/IDevicePolicyManager.aidl @@ -169,7 +169,8 @@ interface IDevicePolicyManager { ComponentName getRestrictionsProvider(int userHandle); void setUserRestriction(in ComponentName who, in String key, boolean enable); - Bundle getUserRestrictions(in ComponentName who, int userId); + Bundle getUserRestrictions(in ComponentName who); + Bundle getUserRestrictionsForUser(in ComponentName who, int userId); void addCrossProfileIntentFilter(in ComponentName admin, in IntentFilter filter, int flags); void clearCrossProfileIntentFilters(in ComponentName admin); diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java index 8bc903b96bcf..a0a16f1a8930 100644 --- a/core/java/android/os/PowerManager.java +++ b/core/java/android/os/PowerManager.java @@ -224,8 +224,6 @@ public final class PowerManager { * This is used by Gaming and VR applications to ensure the device provides * will provide consistent performance over a large amount of time. * </p> - * - * {@hide} */ public static final int SUSTAINED_PERFORMANCE_WAKE_LOCK = 0x00000100; diff --git a/core/java/android/os/storage/StorageVolume.java b/core/java/android/os/storage/StorageVolume.java index c028e150b217..7b0d2a479702 100644 --- a/core/java/android/os/storage/StorageVolume.java +++ b/core/java/android/os/storage/StorageVolume.java @@ -315,27 +315,34 @@ public final class StorageVolume implements Parcelable { * To gain access to descendants (child, grandchild, etc) documents, use * {@link DocumentsContract#buildDocumentUriUsingTree(Uri, String)}, or * {@link DocumentsContract#buildChildDocumentsUriUsingTree(Uri, String)} with the returned URI. - * - * <b>If your application only needs to store internal data, consider using + * <p> + * If your application only needs to store internal data, consider using * {@link Context#getExternalFilesDirs(String) Context.getExternalFilesDirs}, - * {@link Context#getExternalCacheDirs()}, or - * {@link Context#getExternalMediaDirs()}, which require no permissions to read or write. - * - * <strong>NOTE: </strong>requesting access to the entire volume is not recommended and it will - * result in a stronger message displayed to the user, which may cause the user to reject - * the request. - * - * @param directoryName must be one of - * {@link Environment#DIRECTORY_MUSIC}, {@link Environment#DIRECTORY_PODCASTS}, - * {@link Environment#DIRECTORY_RINGTONES}, {@link Environment#DIRECTORY_ALARMS}, - * {@link Environment#DIRECTORY_NOTIFICATIONS}, {@link Environment#DIRECTORY_PICTURES}, - * {@link Environment#DIRECTORY_MOVIES}, {@link Environment#DIRECTORY_DOWNLOADS}, - * {@link Environment#DIRECTORY_DCIM}, or {@link Environment#DIRECTORY_DOCUMENTS}, or - * {code null} to request access to the entire volume. + * {@link Context#getExternalCacheDirs()}, or {@link Context#getExternalMediaDirs()}, which + * require no permissions to read or write. + * <p> + * Access to the entire volume is only available for non-primary volumes (for the primary + * volume, apps can use the {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} and + * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permissions) and should be used + * with caution, since users are more likely to deny access when asked for entire volume access + * rather than specific directories. * + * @param directoryName must be one of {@link Environment#DIRECTORY_MUSIC}, + * {@link Environment#DIRECTORY_PODCASTS}, {@link Environment#DIRECTORY_RINGTONES}, + * {@link Environment#DIRECTORY_ALARMS}, {@link Environment#DIRECTORY_NOTIFICATIONS}, + * {@link Environment#DIRECTORY_PICTURES}, {@link Environment#DIRECTORY_MOVIES}, + * {@link Environment#DIRECTORY_DOWNLOADS}, {@link Environment#DIRECTORY_DCIM}, or + * {@link Environment#DIRECTORY_DOCUMENTS}, or {code null} to request access to the + * entire volume. + * @return intent to request access, or {@code null} if the requested directory is invalid for + * that volume. * @see DocumentsContract */ - public Intent createAccessIntent(String directoryName) { + public @Nullable Intent createAccessIntent(String directoryName) { + if ((isPrimary() && directoryName == null) || + (directoryName != null && !Environment.isStandardDirectory(directoryName))) { + return null; + } final Intent intent = new Intent(ACTION_OPEN_EXTERNAL_DIRECTORY); intent.putExtra(EXTRA_STORAGE_VOLUME, this); intent.putExtra(EXTRA_DIRECTORY_NAME, directoryName); diff --git a/core/java/android/print/IPrintManager.aidl b/core/java/android/print/IPrintManager.aidl index 5eb8cc2f37a4..d7c267b5ca63 100644 --- a/core/java/android/print/IPrintManager.aidl +++ b/core/java/android/print/IPrintManager.aidl @@ -24,9 +24,11 @@ import android.print.IPrintDocumentAdapter; import android.print.PrintJobId; import android.print.IPrintJobStateChangeListener; import android.print.IPrintServicesChangeListener; +import android.printservice.recommendation.IRecommendationsChangeListener; import android.print.PrinterId; import android.print.PrintJobInfo; import android.print.PrintAttributes; +import android.printservice.recommendation.RecommendationInfo; import android.printservice.PrintServiceInfo; /** @@ -73,7 +75,6 @@ interface IPrintManager { * Get the print services. * * @param selectionFlags flags selecting which services to get - * @param selectedService if not null, the id of the print service to get * @param userId the id of the user requesting the services * * @return the list of selected print services. @@ -89,6 +90,37 @@ interface IPrintManager { */ void setPrintServiceEnabled(in ComponentName service, boolean isEnabled, int userId); + /** + * Listen for changes to the print service recommendations. + * + * @param listener the listener to add + * @param userId the id of the user listening + * + * @see android.print.PrintManager#getPrintServiceRecommendations + */ + void addPrintServiceRecommendationsChangeListener(in IRecommendationsChangeListener listener, + int userId); + + /** + * Stop listening for changes to the print service recommendations. + * + * @param listener the listener to remove + * @param userId the id of the user requesting the removal + * + * @see android.print.PrintManager#getPrintServiceRecommendations + */ + void removePrintServiceRecommendationsChangeListener(in IRecommendationsChangeListener listener, + int userId); + + /** + * Get the print service recommendations. + * + * @param userId the id of the user requesting the recommendations + * + * @return the list of selected print services. + */ + List<RecommendationInfo> getPrintServiceRecommendations(int userId); + void createPrinterDiscoverySession(in IPrinterDiscoveryObserver observer, int userId); void startPrinterDiscovery(in IPrinterDiscoveryObserver observer, in List<PrinterId> priorityList, int userId); diff --git a/core/java/android/print/PrintManager.java b/core/java/android/print/PrintManager.java index 25fc968fec5a..71f0bd615206 100644 --- a/core/java/android/print/PrintManager.java +++ b/core/java/android/print/PrintManager.java @@ -36,12 +36,15 @@ import android.os.RemoteException; import android.print.PrintDocumentAdapter.LayoutResultCallback; import android.print.PrintDocumentAdapter.WriteResultCallback; import android.printservice.PrintServiceInfo; +import android.printservice.recommendation.IRecommendationsChangeListener; +import android.printservice.recommendation.RecommendationInfo; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Log; import com.android.internal.os.SomeArgs; +import com.android.internal.util.Preconditions; import libcore.io.IoUtils; import java.lang.ref.WeakReference; @@ -113,6 +116,7 @@ public final class PrintManager { private static final int MSG_NOTIFY_PRINT_JOB_STATE_CHANGED = 1; private static final int MSG_NOTIFY_PRINT_SERVICES_CHANGED = 2; + private static final int MSG_NOTIFY_PRINT_SERVICE_RECOMMENDATIONS_CHANGED = 3; /** * Package name of print spooler. @@ -202,6 +206,9 @@ public final class PrintManager { mPrintJobStateChangeListeners; private Map<PrintServicesChangeListener, PrintServicesChangeListenerWrapper> mPrintServicesChangeListeners; + private Map<PrintServiceRecommendationsChangeListener, + PrintServiceRecommendationsChangeListenerWrapper> + mPrintServiceRecommendationsChangeListeners; /** @hide */ public interface PrintJobStateChangeListener { @@ -223,6 +230,15 @@ public final class PrintManager { public void onPrintServicesChanged(); } + /** @hide */ + public interface PrintServiceRecommendationsChangeListener { + + /** + * Callback notifying that the print service recommendations changed. + */ + void onPrintServiceRecommendationsChanged(); + } + /** * Creates a new instance. * @@ -260,7 +276,14 @@ public final class PrintManager { listener.onPrintServicesChanged(); } } break; - + case MSG_NOTIFY_PRINT_SERVICE_RECOMMENDATIONS_CHANGED: { + PrintServiceRecommendationsChangeListenerWrapper wrapper = + (PrintServiceRecommendationsChangeListenerWrapper) message.obj; + PrintServiceRecommendationsChangeListener listener = wrapper.getListener(); + if (listener != null) { + listener.onPrintServiceRecommendationsChanged(); + } + } break; } } }; @@ -539,13 +562,14 @@ public final class PrintManager { * @see android.print.PrintManager#getPrintServices */ void addPrintServicesChangeListener(@NonNull PrintServicesChangeListener listener) { + Preconditions.checkNotNull(listener); + if (mService == null) { Log.w(LOG_TAG, "Feature android.software.print not available"); return; } if (mPrintServicesChangeListeners == null) { - mPrintServicesChangeListeners = new ArrayMap<PrintServicesChangeListener, - PrintServicesChangeListenerWrapper>(); + mPrintServicesChangeListeners = new ArrayMap<>(); } PrintServicesChangeListenerWrapper wrappedListener = new PrintServicesChangeListenerWrapper(listener, mHandler); @@ -565,6 +589,8 @@ public final class PrintManager { * @see android.print.PrintManager#getPrintServices */ void removePrintServicesChangeListener(@NonNull PrintServicesChangeListener listener) { + Preconditions.checkNotNull(listener); + if (mService == null) { Log.w(LOG_TAG, "Feature android.software.print not available"); return; @@ -588,7 +614,6 @@ public final class PrintManager { } } - /** * Gets the list of print services, but does not register for updates. The user has to register * for updates by itself, or use {@link PrintServicesLoader}. @@ -596,7 +621,7 @@ public final class PrintManager { * @param selectionFlags flags selecting which services to get. Either * {@link #ENABLED_SERVICES},{@link #DISABLED_SERVICES}, or both. * - * @return The enabled service list or an empty list. + * @return The print service list or an empty list. * * @see #addPrintServicesChangeListener(PrintServicesChangeListener) * @see #removePrintServicesChangeListener(PrintServicesChangeListener) @@ -604,6 +629,8 @@ public final class PrintManager { * @hide */ public @NonNull List<PrintServiceInfo> getPrintServices(int selectionFlags) { + Preconditions.checkFlagsArgument(selectionFlags, ALL_SERVICES); + try { List<PrintServiceInfo> services = mService.getPrintServices(selectionFlags, mUserId); if (services != null) { @@ -616,6 +643,92 @@ public final class PrintManager { } /** + * Listen for changes to the print service recommendations. + * + * @param listener the listener to add + * + * @see android.print.PrintManager#getPrintServiceRecommendations + */ + void addPrintServiceRecommendationsChangeListener( + @NonNull PrintServiceRecommendationsChangeListener listener) { + Preconditions.checkNotNull(listener); + + if (mService == null) { + Log.w(LOG_TAG, "Feature android.software.print not available"); + return; + } + if (mPrintServiceRecommendationsChangeListeners == null) { + mPrintServiceRecommendationsChangeListeners = new ArrayMap<>(); + } + PrintServiceRecommendationsChangeListenerWrapper wrappedListener = + new PrintServiceRecommendationsChangeListenerWrapper(listener, mHandler); + try { + mService.addPrintServiceRecommendationsChangeListener(wrappedListener, mUserId); + mPrintServiceRecommendationsChangeListeners.put(listener, wrappedListener); + } catch (RemoteException re) { + throw re.rethrowFromSystemServer(); + } + } + + /** + * Stop listening for changes to the print service recommendations. + * + * @param listener the listener to remove + * + * @see android.print.PrintManager#getPrintServiceRecommendations + */ + void removePrintServiceRecommendationsChangeListener( + @NonNull PrintServiceRecommendationsChangeListener listener) { + Preconditions.checkNotNull(listener); + + if (mService == null) { + Log.w(LOG_TAG, "Feature android.software.print not available"); + return; + } + if (mPrintServiceRecommendationsChangeListeners == null) { + return; + } + PrintServiceRecommendationsChangeListenerWrapper wrappedListener = + mPrintServiceRecommendationsChangeListeners.remove(listener); + if (wrappedListener == null) { + return; + } + if (mPrintServiceRecommendationsChangeListeners.isEmpty()) { + mPrintServiceRecommendationsChangeListeners = null; + } + wrappedListener.destroy(); + try { + mService.removePrintServiceRecommendationsChangeListener(wrappedListener, mUserId); + } catch (RemoteException re) { + throw re.rethrowFromSystemServer(); + } + } + + /** + * Gets the list of print service recommendations, but does not register for updates. The user + * has to register for updates by itself, or use {@link PrintServiceRecommendationsLoader}. + * + * @return The print service recommendations list or an empty list. + * + * @see #addPrintServiceRecommendationsChangeListener + * @see #removePrintServiceRecommendationsChangeListener + * + * @hide + */ + public @NonNull List<RecommendationInfo> getPrintServiceRecommendations() { + try { + List<RecommendationInfo> recommendations = + mService.getPrintServiceRecommendations(mUserId); + if (recommendations != null) { + return recommendations; + } + } catch (RemoteException re) { + throw re.rethrowFromSystemServer(); + } + return Collections.emptyList(); + } + + /** * @hide */ public PrinterDiscoverySession createPrinterDiscoverySession() { @@ -1242,4 +1355,37 @@ public final class PrintManager { return mWeakListener.get(); } } + + /** + * @hide + */ + public static final class PrintServiceRecommendationsChangeListenerWrapper extends + IRecommendationsChangeListener.Stub { + private final WeakReference<PrintServiceRecommendationsChangeListener> mWeakListener; + private final WeakReference<Handler> mWeakHandler; + + public PrintServiceRecommendationsChangeListenerWrapper( + PrintServiceRecommendationsChangeListener listener, Handler handler) { + mWeakListener = new WeakReference<>(listener); + mWeakHandler = new WeakReference<>(handler); + } + + @Override + public void onRecommendationsChanged() { + Handler handler = mWeakHandler.get(); + PrintServiceRecommendationsChangeListener listener = mWeakListener.get(); + if (handler != null && listener != null) { + handler.obtainMessage(MSG_NOTIFY_PRINT_SERVICE_RECOMMENDATIONS_CHANGED, + this).sendToTarget(); + } + } + + public void destroy() { + mWeakListener.clear(); + } + + public PrintServiceRecommendationsChangeListener getListener() { + return mWeakListener.get(); + } + } } diff --git a/core/java/android/print/PrintServiceRecommendationsLoader.java b/core/java/android/print/PrintServiceRecommendationsLoader.java new file mode 100644 index 000000000000..bb5d065c6430 --- /dev/null +++ b/core/java/android/print/PrintServiceRecommendationsLoader.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2016 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 android.print; + +import android.annotation.NonNull; +import android.content.Context; +import android.content.Loader; +import android.os.Handler; +import android.os.Message; +import android.printservice.recommendation.RecommendationInfo; +import com.android.internal.util.Preconditions; + +import java.util.List; + +/** + * Loader for the list of print service recommendations. + * + * @hide + */ +public class PrintServiceRecommendationsLoader extends Loader<List<RecommendationInfo>> { + /** The print manager to be used by this object */ + private final @NonNull PrintManager mPrintManager; + + /** Handler to sequentialize the delivery of the results to the main thread */ + private final Handler mHandler; + + /** Listens for updates to the data from the platform */ + private PrintManager.PrintServiceRecommendationsChangeListener mListener; + + /** + * Create a new PrintServicesLoader. + * + * @param printManager The print manager supplying the data + * @param context Context of the using object + */ + public PrintServiceRecommendationsLoader(@NonNull PrintManager printManager, + @NonNull Context context) { + super(Preconditions.checkNotNull(context)); + mHandler = new MyHandler(); + mPrintManager = Preconditions.checkNotNull(printManager); + } + + @Override + protected void onForceLoad() { + queueNewResult(); + } + + /** + * Read the print service recommendations and queue it to be delivered on the main thread. + */ + private void queueNewResult() { + Message m = mHandler.obtainMessage(0); + m.obj = mPrintManager.getPrintServiceRecommendations(); + mHandler.sendMessage(m); + } + + @Override + protected void onStartLoading() { + mListener = new PrintManager.PrintServiceRecommendationsChangeListener() { + @Override + public void onPrintServiceRecommendationsChanged() { + queueNewResult(); + } + }; + + mPrintManager.addPrintServiceRecommendationsChangeListener(mListener); + + // Immediately deliver a result + deliverResult(mPrintManager.getPrintServiceRecommendations()); + } + + @Override + protected void onStopLoading() { + if (mListener != null) { + mPrintManager.removePrintServiceRecommendationsChangeListener(mListener); + mListener = null; + } + + if (mHandler != null) { + mHandler.removeMessages(0); + } + } + + @Override + protected void onReset() { + onStopLoading(); + } + + /** + * Handler to sequentialize all the updates to the main thread. + */ + private class MyHandler extends Handler { + /** + * Create a new handler on the main thread. + */ + public MyHandler() { + super(getContext().getMainLooper()); + } + + @Override + public void handleMessage(Message msg) { + if (isStarted()) { + deliverResult((List<RecommendationInfo>) msg.obj); + } + } + } +} diff --git a/core/java/android/print/PrintServicesLoader.java b/core/java/android/print/PrintServicesLoader.java index ed411141d1fb..60d7d666c2c9 100644 --- a/core/java/android/print/PrintServicesLoader.java +++ b/core/java/android/print/PrintServicesLoader.java @@ -22,6 +22,7 @@ import android.content.Loader; import android.os.Handler; import android.os.Message; import android.printservice.PrintServiceInfo; +import com.android.internal.util.Preconditions; import java.util.List; @@ -46,13 +47,16 @@ public class PrintServicesLoader extends Loader<List<PrintServiceInfo>> { /** * Create a new PrintServicesLoader. * + * @param printManager The print manager supplying the data + * @param context Context of the using object * @param selectionFlags What type of services to load. */ public PrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context, int selectionFlags) { - super(context); - mPrintManager = printManager; - mSelectionFlags = selectionFlags; + super(Preconditions.checkNotNull(context)); + mPrintManager = Preconditions.checkNotNull(printManager); + mSelectionFlags = Preconditions.checkFlagsArgument(selectionFlags, + PrintManager.ALL_SERVICES); } @Override diff --git a/core/java/android/printservice/recommendation/IRecommendationService.aidl b/core/java/android/printservice/recommendation/IRecommendationService.aidl new file mode 100644 index 000000000000..ce9ea6fd9fcb --- /dev/null +++ b/core/java/android/printservice/recommendation/IRecommendationService.aidl @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2016 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 android.printservice.recommendation; + +import android.printservice.recommendation.IRecommendationServiceCallbacks; + +/** + * Interface for communication with the print service recommendation service. + * + * @see android.print.IPrintServiceRecommendationServiceCallbacks + * + * @hide + */ +oneway interface IRecommendationService { + void registerCallbacks(in IRecommendationServiceCallbacks callbacks); +} diff --git a/core/java/android/printservice/recommendation/IRecommendationServiceCallbacks.aidl b/core/java/android/printservice/recommendation/IRecommendationServiceCallbacks.aidl new file mode 100644 index 000000000000..95286544eed0 --- /dev/null +++ b/core/java/android/printservice/recommendation/IRecommendationServiceCallbacks.aidl @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 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 android.printservice.recommendation; + +import android.printservice.recommendation.RecommendationInfo; + +/** + * Callbacks for communication with the print service recommendation service. + * + * @see android.print.IPrintServiceRecommendationService + * + * @hide + */ +oneway interface IRecommendationServiceCallbacks { + /** + * Update the print service recommendations. + * + * @param recommendations the new print service recommendations + */ + void onRecommendationsUpdated(in List<RecommendationInfo> recommendations); +} diff --git a/core/java/android/printservice/recommendation/IRecommendationsChangeListener.aidl b/core/java/android/printservice/recommendation/IRecommendationsChangeListener.aidl new file mode 100644 index 000000000000..8ca5c69e8180 --- /dev/null +++ b/core/java/android/printservice/recommendation/IRecommendationsChangeListener.aidl @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2016 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 android.printservice.recommendation; + +/** + * Interface for observing changes of the print service recommendations. + * + * @hide + */ +oneway interface IRecommendationsChangeListener { + void onRecommendationsChanged(); +} diff --git a/core/java/android/printservice/recommendation/RecommendationInfo.aidl b/core/java/android/printservice/recommendation/RecommendationInfo.aidl new file mode 100644 index 000000000000..f21d0bf3f584 --- /dev/null +++ b/core/java/android/printservice/recommendation/RecommendationInfo.aidl @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2016, 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 android.printservice.recommendation; + +/** + * @hide + */ +parcelable RecommendationInfo; diff --git a/core/java/android/printservice/recommendation/RecommendationInfo.java b/core/java/android/printservice/recommendation/RecommendationInfo.java new file mode 100644 index 000000000000..65d534e45e1c --- /dev/null +++ b/core/java/android/printservice/recommendation/RecommendationInfo.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2016 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 android.printservice.recommendation; + +import android.annotation.IntRange; +import android.annotation.NonNull; +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; +import android.printservice.PrintService; +import com.android.internal.util.Preconditions; + +/** + * A recommendation to install a {@link PrintService print service}. + * + * @hide + */ +@SystemApi +public final class RecommendationInfo implements Parcelable { + /** Package name of the print service. */ + private @NonNull final CharSequence mPackageName; + + /** Display name of the print service. */ + private @NonNull final CharSequence mName; + + /** Number of printers the print service would discover if installed. */ + private @IntRange(from = 0) final int mNumDiscoveredPrinters; + + /** If the service detects printer from multiple vendors. */ + private final boolean mRecommendsMultiVendorService; + + /** + * Create a new recommendation. + * + * @param packageName Package name of the print service + * @param name Display name of the print service + * @param numDiscoveredPrinters Number of printers the print service would discover if + * installed + * @param recommendsMultiVendorService If the service detects printer from multiple vendor + */ + public RecommendationInfo(@NonNull CharSequence packageName, @NonNull CharSequence name, + @IntRange(from = 0) int numDiscoveredPrinters, boolean recommendsMultiVendorService) { + mPackageName = Preconditions.checkStringNotEmpty(packageName); + mName = Preconditions.checkStringNotEmpty(name); + mNumDiscoveredPrinters = Preconditions.checkArgumentNonnegative(numDiscoveredPrinters); + mRecommendsMultiVendorService = recommendsMultiVendorService; + } + + /** + * Create a new recommendation from a parcel. + * + * @param parcel The parcel containing the data + * + * @see #CREATOR + */ + private RecommendationInfo(@NonNull Parcel parcel) { + this(parcel.readCharSequence(), parcel.readCharSequence(), parcel.readInt(), + parcel.readByte() != 0); + } + + /** + * @return The package name the recommendations recommends. + */ + public CharSequence getPackageName() { + return mPackageName; + } + + /** + * @return Whether the recommended print service detects printers of more than one vendor. + */ + public boolean recommendsMultiVendorService() { + return mRecommendsMultiVendorService; + } + + /** + * @return The number of printer the print service would detect. + */ + public int getNumDiscoveredPrinters() { + return mNumDiscoveredPrinters; + } + + /** + * @return The name of the recommended print service. + */ + public CharSequence getName() { + return mName; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeCharSequence(mPackageName); + dest.writeCharSequence(mName); + dest.writeInt(mNumDiscoveredPrinters); + dest.writeByte((byte) (mRecommendsMultiVendorService ? 1 : 0)); + } + + /** + * Utility class used to create new print service recommendation objects from parcels. + * + * @see #RecommendationInfo(Parcel) + */ + public static final Creator<RecommendationInfo> CREATOR = + new Creator<RecommendationInfo>() { + @Override + public RecommendationInfo createFromParcel(Parcel in) { + return new RecommendationInfo(in); + } + + @Override + public RecommendationInfo[] newArray(int size) { + return new RecommendationInfo[size]; + } + }; +} diff --git a/core/java/android/printservice/recommendation/RecommendationService.java b/core/java/android/printservice/recommendation/RecommendationService.java new file mode 100644 index 000000000000..b7ea51271043 --- /dev/null +++ b/core/java/android/printservice/recommendation/RecommendationService.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2016 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 android.printservice.recommendation; + +import android.annotation.Nullable; +import android.annotation.SystemApi; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Handler; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; +import android.os.RemoteException; +import android.util.Log; +import com.android.internal.annotations.GuardedBy; + +import java.util.List; + +/** + * Base class for the print service recommendation services. + * + * @hide + */ +@SystemApi +public abstract class RecommendationService extends Service { + private static final String LOG_TAG = "PrintServiceRecS"; + + /** Used to push onConnect and onDisconnect on the main thread */ + private Handler mHandler; + + /** + * The {@link Intent} action that must be declared as handled by a service in its manifest for + * the system to recognize it as a print service recommendation service. + * + * @hide + */ + public static final String SERVICE_INTERFACE = + "android.printservice.recommendation.RecommendationService"; + + /** Registered callbacks, only modified on main thread */ + private IRecommendationServiceCallbacks mCallbacks; + + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(base); + + mHandler = new MyHandler(); + } + + /** + * Update the print service recommendations. + * + * @param recommendations The new set of recommendations + */ + public final void updateRecommendations(@Nullable List<RecommendationInfo> recommendations) { + mHandler.obtainMessage(MyHandler.MSG_UPDATE, recommendations).sendToTarget(); + } + + @Override + public final IBinder onBind(Intent intent) { + return new IRecommendationService.Stub() { + @Override + public void registerCallbacks(IRecommendationServiceCallbacks callbacks) { + // The callbacks come in order of the caller on oneway calls. Hence while the caller + // cannot know at what time the connection is made, he can know the ordering of + // connection and disconnection. + // + // Similar he cannot know when the disconnection is processed, hence he has to + // handle callbacks after calling disconnect. + if (callbacks != null) { + mHandler.obtainMessage(MyHandler.MSG_CONNECT, callbacks).sendToTarget(); + } else { + mHandler.obtainMessage(MyHandler.MSG_DISCONNECT).sendToTarget(); + } + } + }; + } + + /** + * Called when the client connects to the recommendation service. + */ + public abstract void onConnected(); + + /** + * Called when the client disconnects from the recommendation service. + */ + public abstract void onDisconnected(); + + private class MyHandler extends Handler { + static final int MSG_CONNECT = 1; + static final int MSG_DISCONNECT = 2; + static final int MSG_UPDATE = 3; + + MyHandler() { + super(Looper.getMainLooper()); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_CONNECT: + mCallbacks = (IRecommendationServiceCallbacks) msg.obj; + onConnected(); + break; + case MSG_DISCONNECT: + onDisconnected(); + mCallbacks = null; + break; + case MSG_UPDATE: + // Note that there might be a connection change in progress. In this case the + // message is handled as before the change. This is acceptable as the caller of + // the connection change has not guarantee when the connection change binder + // transaction is actually processed. + try { + mCallbacks.onRecommendationsUpdated((List<RecommendationInfo>) msg.obj); + } catch (RemoteException | NullPointerException e) { + Log.e(LOG_TAG, "Could not update recommended services", e); + } + break; + } + } + } +} diff --git a/core/java/android/view/Window.java b/core/java/android/view/Window.java index 36ee3e67ef79..2f3f0bfa9612 100644 --- a/core/java/android/view/Window.java +++ b/core/java/android/view/Window.java @@ -298,7 +298,7 @@ public abstract class Window { private boolean mDestroyed; - private boolean mOverlayWithDecorCaption = false; + private boolean mOverlayWithDecorCaptionEnabled = false; // The current window attributes. private final WindowManager.LayoutParams mWindowAttributes = @@ -2139,13 +2139,13 @@ public abstract class Window { * down. This affects only freeform windows since they display the caption. * @hide */ - public void setOverlayDecorCaption(boolean overlayCaption) { - mOverlayWithDecorCaption = overlayCaption; + public void setOverlayWithDecorCaptionEnabled(boolean enabled) { + mOverlayWithDecorCaptionEnabled = enabled; } /** @hide */ - public boolean getOverlayDecorCaption() { - return mOverlayWithDecorCaption; + public boolean isOverlayWithDecorCaptionEnabled() { + return mOverlayWithDecorCaptionEnabled; } /** @hide */ @@ -2181,7 +2181,7 @@ public abstract class Window { * Called when the activity changes from fullscreen mode to multi-window mode and visa-versa. * @hide */ - public abstract void onMultiWindowChanged(); + public abstract void onMultiWindowModeChanged(); /** * Called when the activity just relaunched. diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java index c1392fe07235..23b0df2db110 100644 --- a/core/java/android/view/WindowManagerPolicy.java +++ b/core/java/android/view/WindowManagerPolicy.java @@ -415,7 +415,7 @@ public interface WindowManagerPolicy { * Returns true if the window is current in multi-windowing mode. i.e. it shares the * screen with other application windows. */ - public boolean inMultiWindowMode(); + public boolean isInMultiWindowMode(); } /** diff --git a/core/java/com/android/internal/app/LocaleHelper.java b/core/java/com/android/internal/app/LocaleHelper.java index d8d6e56ac69a..36db5d742b4a 100644 --- a/core/java/com/android/internal/app/LocaleHelper.java +++ b/core/java/com/android/internal/app/LocaleHelper.java @@ -181,6 +181,7 @@ public class LocaleHelper { public static final class LocaleInfoComparator implements Comparator<LocaleStore.LocaleInfo> { private final Collator mCollator; private final boolean mCountryMode; + private static final String PREFIX_ARABIC = "\u0627\u0644"; // ALEF-LAM, ال /** * Constructor. @@ -192,6 +193,20 @@ public class LocaleHelper { mCountryMode = countryMode; } + /* + * The Arabic collation should ignore Alef-Lam at the beginning (b/26277596) + * + * We look at the label's locale, not the current system locale. + * This is because the name of the Arabic language itself is in Arabic, + * and starts with Alef-Lam, no matter what the system locale is. + */ + private String removePrefixForCompare(Locale locale, String str) { + if ("ar".equals(locale.getLanguage()) && str.startsWith(PREFIX_ARABIC)) { + return str.substring(PREFIX_ARABIC.length()); + } + return str; + } + /** * Compares its two arguments for order. * @@ -206,7 +221,9 @@ public class LocaleHelper { // and "all others" (== 0) if (mCountryMode || (lhs.isSuggested() == rhs.isSuggested())) { // They are in the same "bucket" (suggested / others), so we compare the text - return mCollator.compare(lhs.getLabel(mCountryMode), rhs.getLabel(mCountryMode)); + return mCollator.compare( + removePrefixForCompare(lhs.getLocale(), lhs.getLabel(mCountryMode)), + removePrefixForCompare(rhs.getLocale(), rhs.getLabel(mCountryMode))); } else { // One locale is suggested and one is not, so we put them in different "buckets" return lhs.isSuggested() ? -1 : 1; diff --git a/core/java/com/android/internal/policy/PhoneWindow.java b/core/java/com/android/internal/policy/PhoneWindow.java index 794a6d678936..d80b63acb90f 100644 --- a/core/java/com/android/internal/policy/PhoneWindow.java +++ b/core/java/com/android/internal/policy/PhoneWindow.java @@ -680,7 +680,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { } @Override - public void onMultiWindowChanged() { + public void onMultiWindowModeChanged() { if (mDecor != null) { mDecor.onConfigurationChanged(getContext().getResources().getConfiguration()); } diff --git a/core/java/com/android/internal/util/Preconditions.java b/core/java/com/android/internal/util/Preconditions.java index c46851e1c757..7c8524671716 100644 --- a/core/java/com/android/internal/util/Preconditions.java +++ b/core/java/com/android/internal/util/Preconditions.java @@ -56,7 +56,7 @@ public class Preconditions { * @return the string reference that was validated * @throws IllegalArgumentException if {@code string} is empty */ - public static @NonNull String checkStringNotEmpty(final String string) { + public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string) { if (TextUtils.isEmpty(string)) { throw new IllegalArgumentException(); } @@ -73,7 +73,7 @@ public class Preconditions { * @return the string reference that was validated * @throws IllegalArgumentException if {@code string} is empty */ - public static @NonNull String checkStringNotEmpty(final String string, + public static @NonNull <T extends CharSequence> T checkStringNotEmpty(final T string, final Object errorMessage) { if (TextUtils.isEmpty(string)) { throw new IllegalArgumentException(String.valueOf(errorMessage)); @@ -141,13 +141,17 @@ public class Preconditions { /** * Check the requested flags, throwing if any requested flags are outside * the allowed set. + * + * @return the validated requested flags. */ - public static void checkFlagsArgument(final int requestedFlags, final int allowedFlags) { + public static int checkFlagsArgument(final int requestedFlags, final int allowedFlags) { if ((requestedFlags & allowedFlags) != requestedFlags) { throw new IllegalArgumentException("Requested flags 0x" + Integer.toHexString(requestedFlags) + ", but only 0x" + Integer.toHexString(allowedFlags) + " are allowed"); } + + return requestedFlags; } /** @@ -170,6 +174,22 @@ public class Preconditions { /** * Ensures that that the argument numeric value is non-negative. * + * @param value a numeric int value + * + * @return the validated numeric value + * @throws IllegalArgumentException if {@code value} was negative + */ + public static @IntRange(from = 0) int checkArgumentNonnegative(final int value) { + if (value < 0) { + throw new IllegalArgumentException(); + } + + return value; + } + + /** + * Ensures that that the argument numeric value is non-negative. + * * @param value a numeric long value * @param errorMessage the exception message to use if the check fails * @return the validated numeric value diff --git a/core/java/com/android/internal/widget/DecorCaptionView.java b/core/java/com/android/internal/widget/DecorCaptionView.java index 409a17f9c4db..e59d7ba57ea1 100644 --- a/core/java/com/android/internal/widget/DecorCaptionView.java +++ b/core/java/com/android/internal/widget/DecorCaptionView.java @@ -136,7 +136,7 @@ public class DecorCaptionView extends ViewGroup implements View.OnTouchListener, public void setPhoneWindow(PhoneWindow owner, boolean show) { mOwner = owner; mShow = show; - mOverlayWithAppContent = owner.getOverlayDecorCaption(); + mOverlayWithAppContent = owner.isOverlayWithDecorCaptionEnabled(); if (mOverlayWithAppContent) { // The caption is covering the content, so we make its background transparent to make // the content visible. diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 8abb7e2f22b5..b0fcc2897445 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -2172,6 +2172,15 @@ <permission android:name="android.permission.BIND_PRINT_SERVICE" android:protectionLevel="signature" /> + <!-- Must be required by a {@link android.printservice.recommendation.RecommendationService}, + to ensure that only the system can bind to it. + @hide + @SystemApi + <p>Protection level: signature + --> + <permission android:name="android.permission.BIND_PRINT_RECOMMENDATION_SERVICE" + android:protectionLevel="signature" /> + <!-- Must be required by a {@link android.nfc.cardemulation.HostApduService} or {@link android.nfc.cardemulation.OffHostApduService} to ensure that only the system can bind to it. diff --git a/core/tests/coretests/src/android/print/IPrintManagerParametersTest.java b/core/tests/coretests/src/android/print/IPrintManagerParametersTest.java index ec8cd71accbe..43a61e301c8f 100644 --- a/core/tests/coretests/src/android/print/IPrintManagerParametersTest.java +++ b/core/tests/coretests/src/android/print/IPrintManagerParametersTest.java @@ -31,6 +31,7 @@ import android.print.PrintAttributes.Margins; import android.print.PrintAttributes.MediaSize; import android.print.PrintAttributes.Resolution; import android.printservice.PrintServiceInfo; +import android.printservice.recommendation.IRecommendationsChangeListener; import android.print.mockservice.MockPrintService; import android.print.mockservice.PrintServiceCallbacks; @@ -181,6 +182,17 @@ public class IPrintManagerParametersTest extends BasePrintTest { new Handler(Looper.getMainLooper())); } + /** + * Create a IPrintServiceRecommendationsChangeListener object. + * + * @return the object + * @throws Exception if the object could not be created. + */ + private IRecommendationsChangeListener + createMockIPrintServiceRecommendationsChangeListener() throws Exception { + return new PrintManager.PrintServiceRecommendationsChangeListenerWrapper(null, + new Handler(Looper.getMainLooper())); + } /** * Create a IPrinterDiscoveryObserver object. @@ -559,6 +571,61 @@ public class IPrintManagerParametersTest extends BasePrintTest { } /** + * test IPrintManager.addPrintServiceRecommendationsChangeListener + */ + @MediumTest + public void testAddPrintServiceRecommendationsChangeListener() throws Exception { + final IRecommendationsChangeListener listener = + createMockIPrintServiceRecommendationsChangeListener(); + + mIPrintManager.addPrintServiceRecommendationsChangeListener(listener, mUserId); + + assertException(new Invokable() { + @Override + public void run() throws Exception { + mIPrintManager.addPrintServiceRecommendationsChangeListener(null, mUserId); + } + }, NullPointerException.class); + + // Cannot test bad user Id as these tests are allowed to call across users + } + + /** + * test IPrintManager.removePrintServicesChangeListener + */ + @MediumTest + public void testRemovePrintServiceRecommendationsChangeListener() throws Exception { + final IRecommendationsChangeListener listener = + createMockIPrintServiceRecommendationsChangeListener(); + + mIPrintManager.addPrintServiceRecommendationsChangeListener(listener, mUserId); + mIPrintManager.removePrintServiceRecommendationsChangeListener(listener, mUserId); + + // Removing unknown listeners is a no-op + mIPrintManager.removePrintServiceRecommendationsChangeListener(listener, mUserId); + + mIPrintManager.addPrintServiceRecommendationsChangeListener(listener, mUserId); + assertException(new Invokable() { + @Override + public void run() throws Exception { + mIPrintManager.removePrintServiceRecommendationsChangeListener(null, mUserId); + } + }, NullPointerException.class); + + // Cannot test bad user Id as these tests are allowed to call across users + } + + /** + * test IPrintManager.getPrintServiceRecommendations + */ + @MediumTest + public void testGetPrintServiceRecommendations() throws Exception { + mIPrintManager.getPrintServiceRecommendations(mUserId); + + // Cannot test bad user Id as these tests are allowed to call across users + } + + /** * test IPrintManager.createPrinterDiscoverySession */ @MediumTest diff --git a/packages/DocumentsUI/res/menu/activity.xml b/packages/DocumentsUI/res/menu/activity.xml index 95c6f1f357dd..85e7a7ae004c 100644 --- a/packages/DocumentsUI/res/menu/activity.xml +++ b/packages/DocumentsUI/res/menu/activity.xml @@ -86,6 +86,10 @@ android:showAsAction="never" android:visible="false" /> <item + android:id="@+id/menu_advanced" + android:showAsAction="never" + android:visible="false" /> + <item android:id="@+id/menu_settings" android:title="@string/menu_settings" android:showAsAction="never" diff --git a/packages/DocumentsUI/res/values/config.xml b/packages/DocumentsUI/res/values/config.xml index 765211d70fa8..6590bbeda892 100644 --- a/packages/DocumentsUI/res/values/config.xml +++ b/packages/DocumentsUI/res/values/config.xml @@ -24,8 +24,9 @@ <!-- Indicates if the home directory should be hidden in the roots list, that is presented in the drawer/left side panel ) --> <bool name="home_root_hidden">true</bool> - <!-- Indicates if the advanced roots like internal storage should be hidden in the roots list) --> - <bool name="advanced_roots_hidden">true</bool> + <!-- Indicates if the advanced roots like internal storage should be shown in the roots list. + When enabled there is no menu option to toggle internal storage visibility. --> + <bool name="advanced_roots_shown">false</bool> <!-- Indicates if search view is taking the whole toolbar space --> <bool name="full_bar_search_view">true</bool> </resources> diff --git a/packages/DocumentsUI/res/values/strings.xml b/packages/DocumentsUI/res/values/strings.xml index b26ee9778d33..fb557caf84f2 100644 --- a/packages/DocumentsUI/res/values/strings.xml +++ b/packages/DocumentsUI/res/values/strings.xml @@ -204,6 +204,9 @@ <string name="open_external_dialog_request">Grant <xliff:g id="appName" example="System Settings"><b>^1</b></xliff:g> access to <xliff:g id="directory" example="Pictures"><i>^2</i></xliff:g> directory on <xliff:g id="storage" example="SD Card"><i>^3</i></xliff:g>?</string> + <!-- Text in an alert dialog asking user to grant app access to a given directory in the internal storage --> + <string name="open_external_dialog_request_primary_volume">Grant <xliff:g id="appName" example="System Settings"><b>^1</b></xliff:g> + access to <xliff:g id="directory" example="Pictures"><i>^2</i></xliff:g> directory?</string> <!-- Text in an alert dialog asking user to grant app access to all data in an external storage volume --> <string name="open_external_dialog_root_request">Grant <xliff:g id="appName" example="System Settings"><b>^1</b></xliff:g> access to your data, including photos and videos, on <xliff:g id="storage" example="SD Card"><i>^2</i></xliff:g>?</string> diff --git a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java index 69315f7b84eb..2d051e467ec3 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java +++ b/packages/DocumentsUI/src/com/android/documentsui/BaseActivity.java @@ -18,6 +18,11 @@ package com.android.documentsui; import static com.android.documentsui.Shared.DEBUG; import static com.android.documentsui.Shared.EXTRA_BENCHMARK; +import static com.android.documentsui.State.ACTION_CREATE; +import static com.android.documentsui.State.ACTION_OPEN; +import static com.android.documentsui.State.ACTION_OPEN_TREE; +import static com.android.documentsui.State.ACTION_GET_CONTENT; +import static com.android.documentsui.State.ACTION_PICK_COPY_DESTINATION; import static com.android.documentsui.State.MODE_GRID; import android.app.Activity; @@ -165,6 +170,7 @@ public abstract class BaseActivity extends Activity final MenuItem sortSize = menu.findItem(R.id.menu_sort_size); final MenuItem grid = menu.findItem(R.id.menu_grid); final MenuItem list = menu.findItem(R.id.menu_list); + final MenuItem advanced = menu.findItem(R.id.menu_advanced); final MenuItem fileSize = menu.findItem(R.id.menu_file_size); // Search uses backend ranking; no sorting, recents doesn't support sort. @@ -176,6 +182,9 @@ public abstract class BaseActivity extends Activity grid.setVisible(mState.derivedMode != State.MODE_GRID); list.setVisible(mState.derivedMode != State.MODE_LIST); + advanced.setVisible(mState.showAdvancedOption); + advanced.setTitle(mState.showAdvancedOption && mState.showAdvanced + ? R.string.menu_advanced_hide : R.string.menu_advanced_show); fileSize.setTitle(LocalPreferences.getDisplayFileSize(this) ? R.string.menu_file_size_hide : R.string.menu_file_size_show); @@ -195,25 +204,30 @@ public abstract class BaseActivity extends Activity return state; } - State state = createSharedState(); - includeState(state); - if (DEBUG) Log.d(mTag, "Created new state object: " + state); - return state; - } - - private State createSharedState() { State state = new State(); final Intent intent = getIntent(); state.localOnly = intent.getBooleanExtra(Intent.EXTRA_LOCAL_ONLY, false); - state.forceSize = intent.getBooleanExtra(DocumentsContract.EXTRA_SHOW_FILESIZE, false); state.showSize = state.forceSize || LocalPreferences.getDisplayFileSize(this); - state.initAcceptMimes(intent); state.excludedAuthorities = getExcludedAuthorities(); + includeState(state); + + // Advanced roots are shown by deafult without menu option if forced by config or intent. + state.showAdvanced = getResources().getBoolean(R.bool.advanced_roots_shown) + || intent.getBooleanExtra(DocumentsContract.EXTRA_SHOW_ADVANCED, false); + // Menu option is shown for whitelisted intents if advanced roots are not shown by default. + state.showAdvancedOption = !state.showAdvanced && + (state.action == ACTION_OPEN || + state.action == ACTION_CREATE || + state.action == ACTION_OPEN_TREE || + state.action == ACTION_GET_CONTENT); + + if (DEBUG) Log.d(mTag, "Created new state object: " + state); + return state; } @@ -287,6 +301,10 @@ public abstract class BaseActivity extends Activity } return true; + case R.id.menu_advanced: + setDisplayAdvancedDevices(!mState.showAdvanced); + return true; + case R.id.menu_file_size: setDisplayFileSize(!LocalPreferences.getDisplayFileSize(this)); return true; @@ -452,6 +470,12 @@ public abstract class BaseActivity extends Activity ? DocumentsContract.buildRootUri("com.android.providers.downloads.documents", "downloads") : DocumentsContract.buildHomeUri(); + } + + void setDisplayAdvancedDevices(boolean display) { + mState.showAdvanced = display; + RootsFragment.get(getFragmentManager()).onDisplayStateChanged(); + invalidateOptionsMenu(); } void setDisplayFileSize(boolean display) { diff --git a/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java b/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java index 3e04e2a5a218..68c0c2a27f8b 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java +++ b/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java @@ -235,7 +235,7 @@ public class FilesActivity extends BaseActivity { // With new multi-window mode we have to pick how we are launched. // By default we'd be launched in-place above the existing app. // By setting launch-to-side ActivityManager will open us to side. - if (inMultiWindow()) { + if (isInMultiWindowMode()) { intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT); } diff --git a/packages/DocumentsUI/src/com/android/documentsui/OpenExternalDirectoryActivity.java b/packages/DocumentsUI/src/com/android/documentsui/OpenExternalDirectoryActivity.java index 2fe2756e9169..854be0bee826 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/OpenExternalDirectoryActivity.java +++ b/packages/DocumentsUI/src/com/android/documentsui/OpenExternalDirectoryActivity.java @@ -86,6 +86,7 @@ public class OpenExternalDirectoryActivity extends Activity { private static final String EXTRA_VOLUME_LABEL = "com.android.documentsui.VOLUME_LABEL"; private static final String EXTRA_VOLUME_UUID = "com.android.documentsui.VOLUME_UUID"; private static final String EXTRA_IS_ROOT = "com.android.documentsui.IS_ROOT"; + private static final String EXTRA_IS_PRIMARY = "com.android.documentsui.IS_PRIMARY"; // Special directory name representing the full volume static final String DIRECTORY_ROOT = "ROOT_DIRECTORY"; @@ -157,6 +158,13 @@ public class OpenExternalDirectoryActivity extends Activity { Log.d(TAG, "showFragment() for volume " + storageVolume.dump() + ", directory " + directoryName + ", and user " + userId); final boolean isRoot = directoryName.equals(DIRECTORY_ROOT); + final boolean isPrimary = storageVolume.isPrimary(); + + if (isRoot && isPrimary) { + if (DEBUG) Log.d(TAG, "root access requested on primary volume"); + return false; + } + final File volumeRoot = storageVolume.getPathFile(); File file; try { @@ -235,6 +243,7 @@ public class OpenExternalDirectoryActivity extends Activity { args.putString(EXTRA_VOLUME_UUID, volumeUuid); args.putString(EXTRA_APP_LABEL, appLabel); args.putBoolean(EXTRA_IS_ROOT, isRoot); + args.putBoolean(EXTRA_IS_PRIMARY, isPrimary); final FragmentManager fm = activity.getFragmentManager(); final FragmentTransaction ft = fm.beginTransaction(); @@ -352,6 +361,7 @@ public class OpenExternalDirectoryActivity extends Activity { private String mVolumeLabel; private String mAppLabel; private boolean mIsRoot; + private boolean mIsPrimary; private CheckBox mDontAskAgain; private OpenExternalDirectoryActivity mActivity; private AlertDialog mDialog; @@ -367,6 +377,7 @@ public class OpenExternalDirectoryActivity extends Activity { mVolumeLabel = args.getString(EXTRA_VOLUME_LABEL); mAppLabel = args.getString(EXTRA_APP_LABEL); mIsRoot = args.getBoolean(EXTRA_IS_ROOT); + mIsPrimary= args.getBoolean(EXTRA_IS_PRIMARY); } mActivity = (OpenExternalDirectoryActivity) getActivity(); } @@ -435,7 +446,9 @@ public class OpenExternalDirectoryActivity extends Activity { message = TextUtils.expandTemplate(getText( R.string.open_external_dialog_root_request), mAppLabel, mVolumeLabel); } else { - message = TextUtils.expandTemplate(getText(R.string.open_external_dialog_request), + message = TextUtils.expandTemplate( + getText(mIsPrimary ? R.string.open_external_dialog_request_primary_volume + : R.string.open_external_dialog_request), mAppLabel, directory, mVolumeLabel); } final TextView messageField = (TextView) view.findViewById(R.id.message); diff --git a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java index c035d9239489..09fadc98a7e0 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java +++ b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java @@ -477,6 +477,11 @@ public class RootsCache { continue; } + if (!state.showAdvanced && root.isAdvanced()) { + if (DEBUG) Log.d(TAG, "Excluding root because: unwanted advanced device."); + continue; + } + if (state.localOnly && !root.isLocalOnly()) { if (DEBUG) Log.d(TAG, "Excluding root because: unwanted non-local device."); continue; diff --git a/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java b/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java index 8b4f40ef38be..5f665c0e4dd5 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java +++ b/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java @@ -321,9 +321,6 @@ public class RootsFragment extends Fragment { if (root.isHome() && Shared.isHomeRootHidden(context)) { continue; - } else if (root.isAdvanced() - && Shared.areAdvancedRootsHidden(context, state)) { - continue; } else if (root.isLibrary()) { if (DEBUG) Log.d(TAG, "Adding " + root + " as library."); libraries.add(item); diff --git a/packages/DocumentsUI/src/com/android/documentsui/Shared.java b/packages/DocumentsUI/src/com/android/documentsui/Shared.java index 655359a70c24..2c60d4a3212f 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/Shared.java +++ b/packages/DocumentsUI/src/com/android/documentsui/Shared.java @@ -181,12 +181,4 @@ public final class Shared { return context.getResources().getBoolean(R.bool.home_root_hidden); } - /* - * Indicates if the advanced roots should be hidden. - */ - public static boolean areAdvancedRootsHidden(Context context, State state) { - return context.getResources().getBoolean(R.bool.advanced_roots_hidden) - && state.action != ACTION_OPEN_TREE; - } - } diff --git a/packages/DocumentsUI/src/com/android/documentsui/State.java b/packages/DocumentsUI/src/com/android/documentsui/State.java index 534a483474d3..c7d60e3d1133 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/State.java +++ b/packages/DocumentsUI/src/com/android/documentsui/State.java @@ -93,6 +93,8 @@ public class State implements android.os.Parcelable { public boolean forceSize; public boolean showSize; public boolean localOnly; + public boolean showAdvancedOption; + public boolean showAdvanced; public boolean restored; /* * Indicates handler was an external app, like photos. @@ -194,6 +196,8 @@ public class State implements android.os.Parcelable { out.writeInt(forceSize ? 1 : 0); out.writeInt(showSize ? 1 : 0); out.writeInt(localOnly ? 1 : 0); + out.writeInt(showAdvancedOption ? 1 : 0); + out.writeInt(showAdvanced ? 1 : 0); out.writeInt(restored ? 1 : 0); out.writeInt(external ? 1 : 0); DurableUtils.writeToParcel(out, stack); @@ -223,6 +227,8 @@ public class State implements android.os.Parcelable { state.forceSize = in.readInt() != 0; state.showSize = in.readInt() != 0; state.localOnly = in.readInt() != 0; + state.showAdvancedOption = in.readInt() != 0; + state.showAdvanced = in.readInt() != 0; state.restored = in.readInt() != 0; state.external = in.readInt() != 0; DurableUtils.readFromParcel(in, state.stack); diff --git a/packages/DocumentsUI/tests/src/com/android/documentsui/RootsCacheTest.java b/packages/DocumentsUI/tests/src/com/android/documentsui/RootsCacheTest.java index e73dd8cdfec2..2e81545e8e54 100644 --- a/packages/DocumentsUI/tests/src/com/android/documentsui/RootsCacheTest.java +++ b/packages/DocumentsUI/tests/src/com/android/documentsui/RootsCacheTest.java @@ -55,6 +55,7 @@ public class RootsCacheTest extends AndroidTestCase { mState = new State(); mState.action = State.ACTION_OPEN; + mState.showAdvanced = true; mState.localOnly = false; } diff --git a/packages/PrintServiceRecommendationService/Android.mk b/packages/PrintServiceRecommendationService/Android.mk new file mode 100644 index 000000000000..66cb0573aef0 --- /dev/null +++ b/packages/PrintServiceRecommendationService/Android.mk @@ -0,0 +1,29 @@ +# Copyright (C) 2016 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. + +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := PrintRecommendationService + +include $(BUILD_PACKAGE) + +LOCAL_SDK_VERSION := system_current + +include $(call all-makefiles-under, $(LOCAL_PATH)) diff --git a/packages/PrintServiceRecommendationService/AndroidManifest.xml b/packages/PrintServiceRecommendationService/AndroidManifest.xml new file mode 100644 index 000000000000..0eb218c853ec --- /dev/null +++ b/packages/PrintServiceRecommendationService/AndroidManifest.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* + * Copyright (c) 2016 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. + */ +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.printservice.recommendation"> + + <uses-permission android:name="android.permission.INTERNET" /> + + <application + android:allowClearUserData="false" + android:label="@string/app_label" + android:allowBackup= "false"> + + <service + android:name=".RecommendationServiceImpl" + android:permission="android.permission.BIND_PRINT_RECOMMENDATION_SERVICE"> + + <intent-filter> + <action android:name="android.printservice.recommendation.RecommendationService" /> + </intent-filter> + </service> + + </application> + +</manifest> diff --git a/packages/PrintServiceRecommendationService/MODULE_LICENSE_APACHE2 b/packages/PrintServiceRecommendationService/MODULE_LICENSE_APACHE2 new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/packages/PrintServiceRecommendationService/MODULE_LICENSE_APACHE2 diff --git a/packages/PrintServiceRecommendationService/NOTICE b/packages/PrintServiceRecommendationService/NOTICE new file mode 100644 index 000000000000..c5b1efa7aac7 --- /dev/null +++ b/packages/PrintServiceRecommendationService/NOTICE @@ -0,0 +1,190 @@ + + Copyright (c) 2005-2008, 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. + + 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. + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/packages/PrintServiceRecommendationService/res/values/donottranslate.xml b/packages/PrintServiceRecommendationService/res/values/donottranslate.xml new file mode 100644 index 000000000000..4cf0eaf4181b --- /dev/null +++ b/packages/PrintServiceRecommendationService/res/values/donottranslate.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2016 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. +--> +<resources> + <string name="app_label">Print Service Recommendation Service</string> +</resources> diff --git a/packages/PrintServiceRecommendationService/res/values/strings.xml b/packages/PrintServiceRecommendationService/res/values/strings.xml new file mode 100644 index 000000000000..83d38000395a --- /dev/null +++ b/packages/PrintServiceRecommendationService/res/values/strings.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="utf-8"?> + +<!-- + (c) Copyright 2016 Mopria Alliance, Inc. + (c) Copyright 2016 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. +--> + +<resources> + <string name="plugin_vendor_hp">HP</string> + <string name="plugin_vendor_lexmark">Lexmark</string> + <string name="plugin_vendor_brother">Brother</string> + <string name="plugin_vendor_canon">Canon</string> + <string name="plugin_vendor_xerox">Xerox</string> + <string name="plugin_vendor_samsung">Samsung Electorics</string> + <string name="plugin_vendor_epson">Epson</string> + <string name="plugin_vendor_konika_minolta">Konika Minolta</string> + <string name="plugin_vendor_fuji">Fuji</string> +</resources> diff --git a/packages/PrintServiceRecommendationService/res/xml/vendorconfigs.xml b/packages/PrintServiceRecommendationService/res/xml/vendorconfigs.xml new file mode 100644 index 000000000000..fda2768c8678 --- /dev/null +++ b/packages/PrintServiceRecommendationService/res/xml/vendorconfigs.xml @@ -0,0 +1,96 @@ +<?xml version="1.0" encoding="utf-8"?> + +<!-- + (c) Copyright 2016 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. + --> + +<vendors> + <vendor> + <name>@string/plugin_vendor_hp</name> + <package>com.hp.android.printservice</package> + <mdns-names> + <mdns-name>HP</mdns-name> + <mdns-name>Hewlett-Packard</mdns-name> + <mdns-name>Hewlett Packard</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_lexmark</name> + <package>com.lexmark.print.plugin</package> + <mdns-names> + <mdns-name>Lexmark</mdns-name> + <mdns-name>Lexmark International</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_brother</name> + <package>com.brother.printservice</package> + <mdns-names> + <mdns-name>Brother</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_canon</name> + <package>com.xerox.printservice</package> + <mdns-names> + <mdns-name>Canon</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_xerox</name> + <package>jp.co.canon.android.printservice.plugin</package> + <mdns-names> + <mdns-name>Xerox</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_samsung</name> + <package>com.sec.app.samsungprintservice</package> + <mdns-names> + <mdns-name>Samsung</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_epson</name> + <package>com.epson.mobilephone.android.epsonprintserviceplugin</package> + <mdns-names> + <mdns-name>Epson</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_konika_minolta</name> + <package>com.kmbt.printservice</package> + <mdns-names> + <mdns-name>kmkmkm</mdns-name> + <mdns-name>Konica Minolta</mdns-name> + <mdns-name>Minolta</mdns-name> + </mdns-names> + </vendor> + + <vendor> + <name>@string/plugin_vendor_fuji</name> + <package>jp.co.fujixerox.prt.PrintUtil.PCL</package> + <mdns-names> + <mdns-name>FUJI XEROX</mdns-name> + </mdns-names> + </vendor> +</vendors> diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java new file mode 100644 index 000000000000..d604ef8a49ea --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 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.printservice.recommendation; + +import android.annotation.IntRange; +import android.annotation.NonNull; +import android.annotation.StringRes; + +/** + * Interface to be implemented by each print service plugin. + * <p/> + * A print service plugin is a minimal version of a real {@link android.printservice.PrintService + * print service}. You cannot print using the plugin. The only functionality in the plugin is to + * report the number of printers that the real service would discover. + */ +public interface PrintServicePlugin { + /** + * Call back used by the print service plugins. + */ + interface PrinterDiscoveryCallback { + /** + * Announce that something changed and the UI for this plugin should be updated. + * + * @param numDiscoveredPrinters The number of printers discovered. + */ + void onChanged(@IntRange(from = 0) int numDiscoveredPrinters); + } + + /** + * Get the name (a string reference) of the {@link android.printservice.PrintService print + * service} with the {@link #getPackageName specified package name}. This is read once, hence + * returning different data at different times is not allowed. + * + * @return The name of the print service as a string reference. The localization is handled + * outside of the plugin. + */ + @StringRes int getName(); + + /** + * The package name of the full print service. + * + * @return The package name + */ + @NonNull CharSequence getPackageName(); + + /** + * Start the discovery plugin. + * + * @param callback Callbacks used by this plugin. + * + * @throws Exception If anything went wrong when starting the plugin + */ + void start(@NonNull PrinterDiscoveryCallback callback) throws Exception; + + /** + * Stop the plugin. This can only return once the plugin is completely finished and cleaned up. + * + * @throws Exception If anything went wrong while stopping plugin + */ + void stop() throws Exception; +} diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java new file mode 100644 index 000000000000..9f6dad8f2e2a --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2016 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.printservice.recommendation; + +import android.content.res.Configuration; +import android.printservice.recommendation.RecommendationInfo; +import android.printservice.recommendation.RecommendationService; +import android.printservice.PrintService; +import android.util.Log; +import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugin; +import com.android.printservice.recommendation.plugin.mdnsFilter.VendorConfig; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; +import java.util.ArrayList; + +/** + * Service that recommends {@link PrintService print services} that might be a good idea to install. + */ +public class RecommendationServiceImpl extends RecommendationService + implements RemotePrintServicePlugin.OnChangedListener { + private static final String LOG_TAG = "PrintServiceRecService"; + + /** All registered plugins */ + private ArrayList<RemotePrintServicePlugin> mPlugins; + + @Override + public void onConnected() { + mPlugins = new ArrayList<>(); + + try { + for (VendorConfig config : VendorConfig.getAllConfigs(this)) { + try { + mPlugins.add(new RemotePrintServicePlugin(new MDNSFilterPlugin(this, + config.name, config.packageName, config.mDNSNames), this, false)); + } catch (Exception e) { + Log.e(LOG_TAG, "Could not initiate simple MDNS plugin for " + + config.packageName, e); + } + } + } catch (IOException | XmlPullParserException e) { + new RuntimeException("Could not parse vendorconfig", e); + } + + final int numPlugins = mPlugins.size(); + for (int i = 0; i < numPlugins; i++) { + try { + mPlugins.get(i).start(); + } catch (RemotePrintServicePlugin.PluginException e) { + Log.e(LOG_TAG, "Could not start plugin", e); + } + } + } + + @Override + public void onDisconnected() { + final int numPlugins = mPlugins.size(); + for (int i = 0; i < numPlugins; i++) { + try { + mPlugins.get(i).stop(); + } catch (RemotePrintServicePlugin.PluginException e) { + Log.e(LOG_TAG, "Could not stop plugin", e); + } + } + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + // Need to update plugin names as they might be localized + onChanged(); + } + + @Override + public void onChanged() { + ArrayList<RecommendationInfo> recommendations = new ArrayList<>(); + + final int numPlugins = mPlugins.size(); + for (int i = 0; i < numPlugins; i++) { + RemotePrintServicePlugin plugin = mPlugins.get(i); + + try { + int numPrinters = plugin.getNumPrinters(); + + if (numPrinters > 0) { + recommendations.add(new RecommendationInfo(plugin.packageName, + getString(plugin.name), numPrinters, + plugin.recommendsMultiVendorService)); + } + } catch (Exception e) { + Log.e(LOG_TAG, "Could not read state of plugin for " + plugin.packageName, e); + } + } + + updateRecommendations(recommendations); + } +} diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java new file mode 100644 index 000000000000..dbd164946dfb --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2016 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.printservice.recommendation; + +import android.annotation.IntRange; +import android.annotation.NonNull; +import android.annotation.StringRes; +import com.android.internal.util.Preconditions; + +/** + * Wrapper for a {@link PrintServicePlugin}, isolating issues with the plugin as good as possible + * from the {@link RecommendationServiceImpl service}. + */ +class RemotePrintServicePlugin implements PrintServicePlugin.PrinterDiscoveryCallback { + /** Lock for this object */ + private final Object mLock = new Object(); + + /** The name of the print service. */ + public final @StringRes int name; + + /** If the print service if for more than a single vendor */ + public final boolean recommendsMultiVendorService; + + /** The package name of the full print service */ + public final @NonNull CharSequence packageName; + + /** Wrapped plugin */ + private final @NonNull PrintServicePlugin mPlugin; + + /** The number of printers discovered by the plugin */ + private @IntRange(from = 0) int mNumPrinters; + + /** If the plugin is started by not yet stopped */ + private boolean isRunning; + + /** Listener for changes to {@link #mNumPrinters}. */ + private @NonNull OnChangedListener mListener; + + /** + * Create a new remote for a {@link PrintServicePlugin plugin}. + * + * @param plugin The plugin to be wrapped + * @param listener The listener to be notified about changes in this plugin + * @param recommendsMultiVendorService If the plugin detects printers of more than a single + * vendor + * + * @throws PluginException If the plugin has issues while caching basic stub properties + */ + public RemotePrintServicePlugin(@NonNull PrintServicePlugin plugin, + @NonNull OnChangedListener listener, boolean recommendsMultiVendorService) + throws PluginException { + mListener = listener; + mPlugin = plugin; + this.recommendsMultiVendorService = recommendsMultiVendorService; + + // We handle any throwable to isolate our self from bugs in the plugin code. + // Cache simple properties to avoid having to deal with exceptions later in the code. + try { + name = Preconditions.checkArgumentPositive(mPlugin.getName(), "name"); + packageName = Preconditions.checkStringNotEmpty(mPlugin.getPackageName(), + "packageName"); + } catch (Throwable e) { + throw new PluginException(mPlugin, "Cannot cache simple properties ", e); + } + + isRunning = false; + } + + /** + * Start the plugin. From now on there might be callbacks to the registered listener. + */ + public void start() + throws PluginException { + // We handle any throwable to isolate our self from bugs in the stub code + try { + synchronized (mLock) { + isRunning = true; + mPlugin.start(this); + } + } catch (Throwable e) { + throw new PluginException(mPlugin, "Cannot start", e); + } + } + + /** + * Stop the plugin. From this call on there will not be any more callbacks. + */ + public void stop() throws PluginException { + // We handle any throwable to isolate our self from bugs in the stub code + try { + synchronized (mLock) { + mPlugin.stop(); + isRunning = false; + } + } catch (Throwable e) { + throw new PluginException(mPlugin, "Cannot stop", e); + } + } + + /** + * Get the current number of printers reported by the stub. + * + * @return The number of printers reported by the stub. + */ + public @IntRange(from = 0) int getNumPrinters() { + return mNumPrinters; + } + + @Override + public void onChanged(@IntRange(from = 0) int numDiscoveredPrinters) { + synchronized (mLock) { + Preconditions.checkState(isRunning); + + mNumPrinters = Preconditions.checkArgumentNonnegative(numDiscoveredPrinters, + "numDiscoveredPrinters"); + + if (mNumPrinters > 0) { + mListener.onChanged(); + } + } + } + + /** + * Listener to listen for changes to {@link #getNumPrinters} + */ + public interface OnChangedListener { + void onChanged(); + } + + /** + * Exception thrown if the stub has any issues. + */ + public class PluginException extends Exception { + private PluginException(PrintServicePlugin plugin, String message, Throwable e) { + super(plugin + ": " + message, e); + } + } +} diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java new file mode 100644 index 000000000000..26300b1e37b9 --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2016 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.printservice.recommendation.plugin.mdnsFilter; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.StringRes; +import android.content.Context; +import android.net.nsd.NsdManager; +import android.net.nsd.NsdServiceInfo; +import android.util.Log; +import com.android.internal.annotations.GuardedBy; +import com.android.internal.util.Preconditions; +import com.android.printservice.recommendation.PrintServicePlugin; +import com.android.printservice.recommendation.util.MDNSUtils; +import com.android.printservice.recommendation.util.NsdResolveQueue; + +import java.util.HashSet; +import java.util.List; + +/** + * A plugin listening for mDNS results and only adding the ones that {@link + * MDNSUtils#isVendorPrinter match} configured list + */ +public class MDNSFilterPlugin implements PrintServicePlugin, NsdManager.DiscoveryListener { + private static final String LOG_TAG = "MDNSFilterPlugin"; + + private static final String PRINTER_SERVICE_TYPE = "_ipp._tcp"; + + /** Name of the print service this plugin is for */ + private final @StringRes int mName; + + /** Package name of the print service this plugin is for */ + private final @NonNull CharSequence mPackageName; + + /** mDNS names handled by the print service this plugin is for */ + private final @NonNull HashSet<String> mMDNSNames; + + /** Printer identifiers of the mPrinters found. */ + @GuardedBy("mLock") + private final @NonNull HashSet<String> mPrinters; + + /** Context of the user of this plugin */ + private final @NonNull Context mContext; + + /** + * Call back to report the number of mPrinters found. + * + * We assume that {@link #start} and {@link #stop} are never called in parallel, hence it is + * safe to not synchronize access to this field. + */ + private @Nullable PrinterDiscoveryCallback mCallback; + + /** Queue used to resolve nsd infos */ + private final @NonNull NsdResolveQueue mResolveQueue; + + /** + * Create new stub that assumes that a print service can be used to print on all mPrinters + * matching some mDNS names. + * + * @param context The context the plugin runs in + * @param name The user friendly name of the print service + * @param packageName The package name of the print service + * @param mDNSNames The mDNS names of the printer. + */ + public MDNSFilterPlugin(@NonNull Context context, @NonNull String name, + @NonNull CharSequence packageName, @NonNull List<String> mDNSNames) { + mContext = Preconditions.checkNotNull(context, "context"); + mName = mContext.getResources().getIdentifier(Preconditions.checkStringNotEmpty(name, + "name"), null, mContext.getPackageName()); + mPackageName = Preconditions.checkStringNotEmpty(packageName); + mMDNSNames = new HashSet<>(Preconditions + .checkCollectionNotEmpty(Preconditions.checkCollectionElementsNotNull(mDNSNames, + "mDNSNames"), "mDNSNames")); + + mResolveQueue = NsdResolveQueue.getInstance(); + mPrinters = new HashSet<>(); + } + + @Override + public @NonNull CharSequence getPackageName() { + return mPackageName; + } + + /** + * @return The NDS manager + */ + private NsdManager getNDSManager() { + return (NsdManager) mContext.getSystemService(Context.NSD_SERVICE); + } + + @Override + public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception { + mCallback = callback; + + getNDSManager().discoverServices(PRINTER_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, + this); + } + + @Override + public @StringRes int getName() { + return mName; + } + + @Override + public void stop() throws Exception { + mCallback.onChanged(0); + mCallback = null; + + getNDSManager().stopServiceDiscovery(this); + } + + @Override + public void onStartDiscoveryFailed(String serviceType, int errorCode) { + Log.w(LOG_TAG, "Failed to start network discovery for type " + serviceType + ": " + + errorCode); + } + + @Override + public void onStopDiscoveryFailed(String serviceType, int errorCode) { + Log.w(LOG_TAG, "Failed to stop network discovery for type " + serviceType + ": " + + errorCode); + } + + @Override + public void onDiscoveryStarted(String serviceType) { + // empty + } + + @Override + public void onDiscoveryStopped(String serviceType) { + mPrinters.clear(); + } + + @Override + public void onServiceFound(NsdServiceInfo serviceInfo) { + mResolveQueue.resolve(getNDSManager(), serviceInfo, + new NsdManager.ResolveListener() { + @Override + public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { + Log.w(LOG_TAG, "Service found: could not resolve " + serviceInfo + ": " + + errorCode); + } + + @Override + public void onServiceResolved(NsdServiceInfo serviceInfo) { + if (MDNSUtils.isVendorPrinter(serviceInfo, mMDNSNames)) { + if (mCallback != null) { + boolean added = mPrinters.add(serviceInfo.getHost().getHostAddress()); + + if (added) { + mCallback.onChanged(mPrinters.size()); + } + } + } + } + }); + } + + @Override + public void onServiceLost(NsdServiceInfo serviceInfo) { + mResolveQueue.resolve(getNDSManager(), serviceInfo, + new NsdManager.ResolveListener() { + @Override + public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { + Log.w(LOG_TAG, "Service lost: Could not resolve " + serviceInfo + ": " + + errorCode); + } + + @Override + public void onServiceResolved(NsdServiceInfo serviceInfo) { + if (MDNSUtils.isVendorPrinter(serviceInfo, mMDNSNames)) { + if (mCallback != null) { + boolean removed = mPrinters + .remove(serviceInfo.getHost().getHostAddress()); + + if (removed) { + mCallback.onChanged(mPrinters.size()); + } + } + } + } + }); + } +} diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java new file mode 100644 index 000000000000..57d5c710f6bd --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java @@ -0,0 +1,325 @@ +/* + * Copyright (C) 2016 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.printservice.recommendation.plugin.mdnsFilter; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.content.Context; +import android.content.res.XmlResourceParser; +import android.util.ArrayMap; +import com.android.internal.annotations.Immutable; +import com.android.internal.util.Preconditions; +import com.android.printservice.recommendation.R; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Vendor configuration as read from {@link R.xml#vendorconfigs vendorconfigs.xml}. Configuration + * can be read via {@link #getConfig(Context, String)}. + */ +@Immutable +public class VendorConfig { + /** Lock for {@link #sConfigs} */ + private static final Object sLock = new Object(); + + /** Strings used as XML tags */ + private static final String VENDORS_TAG = "vendors"; + private static final String VENDOR_TAG = "vendor"; + private static final String NAME_TAG = "name"; + private static final String PACKAGE_TAG = "package"; + private static final String MDNSNAMES_TAG = "mdns-names"; + private static final String MDNSNAME_TAG = "mdns-name"; + + /** Map from vendor name to config. Initialized on first {@link #getConfig use}. */ + private static @Nullable ArrayMap<String, VendorConfig> sConfigs; + + /** Localized vendor name */ + public final @NonNull String name; + + /** Package name containing the print service for this vendor */ + public final @NonNull String packageName; + + /** mDNS names used by this vendor */ + public final @NonNull List<String> mDNSNames; + + /** + * Create an immutable configuration. + */ + private VendorConfig(@NonNull String name, @NonNull String packageName, + @NonNull List<String> mDNSNames) { + this.name = Preconditions.checkStringNotEmpty(name); + this.packageName = Preconditions.checkStringNotEmpty(packageName); + this.mDNSNames = Preconditions.checkCollectionElementsNotNull(mDNSNames, "mDNSName"); + } + + /** + * Get the configuration for a vendor. + * + * @param context Calling context + * @param name The name of the config to read + * + * @return the config for the vendor or null if not found + * + * @throws IOException + * @throws XmlPullParserException + */ + public static @Nullable VendorConfig getConfig(@NonNull Context context, @NonNull String name) + throws IOException, XmlPullParserException { + synchronized (sLock) { + if (sConfigs == null) { + sConfigs = readVendorConfigs(context); + } + + return sConfigs.get(name); + } + } + + /** + * Get all known vendor configurations. + * + * @param context Calling context + * + * @return The known configurations + * + * @throws IOException + * @throws XmlPullParserException + */ + public static @NonNull Collection<VendorConfig> getAllConfigs(@NonNull Context context) + throws IOException, XmlPullParserException { + synchronized (sLock) { + if (sConfigs == null) { + sConfigs = readVendorConfigs(context); + } + + return sConfigs.values(); + } + } + + /** + * Read the text from a XML tag. + * + * @param parser XML parser to read from + * + * @return The text or "" if no text was found + * + * @throws IOException + * @throws XmlPullParserException + */ + private static @NonNull String readText(XmlPullParser parser) + throws IOException, XmlPullParserException { + String result = ""; + + if (parser.next() == XmlPullParser.TEXT) { + result = parser.getText(); + parser.nextTag(); + } + + return result; + } + + /** + * Read a tag with a text content from the parser. + * + * @param parser XML parser to read from + * @param tagName The name of the tag to read + * + * @return The text content of the tag + * + * @throws IOException + * @throws XmlPullParserException + */ + private static @NonNull String readSimpleTag(@NonNull Context context, + @NonNull XmlPullParser parser, @NonNull String tagName, boolean resolveReferences) + throws IOException, XmlPullParserException { + parser.require(XmlPullParser.START_TAG, null, tagName); + String text = readText(parser); + parser.require(XmlPullParser.END_TAG, null, tagName); + + if (resolveReferences && text.startsWith("@")) { + return context.getResources().getString( + context.getResources().getIdentifier(text, null, context.getPackageName())); + } else { + return text; + } + } + + /** + * Read content of a list of tags. + * + * @param parser XML parser to read from + * @param tagName The name of the list tag + * @param subTagName The name of the list-element tags + * @param tagReader The {@link TagReader reader} to use to read the tag content + * @param <T> The type of the parsed tag content + * + * @return A list of {@link T} + * + * @throws XmlPullParserException + * @throws IOException + */ + private static @NonNull <T> ArrayList<T> readTagList(@NonNull XmlPullParser parser, + @NonNull String tagName, @NonNull String subTagName, @NonNull TagReader<T> tagReader) + throws XmlPullParserException, IOException { + ArrayList<T> entries = new ArrayList<>(); + + parser.require(XmlPullParser.START_TAG, null, tagName); + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + if (parser.getName().equals(subTagName)) { + entries.add(tagReader.readTag(parser, subTagName)); + } else { + throw new XmlPullParserException( + "Unexpected subtag of " + tagName + ": " + parser.getName()); + } + } + + return entries; + } + + /** + * Read the vendor configuration file. + * + * @param context The content issuing the read + * + * @return An map pointing from vendor name to config + * + * @throws IOException + * @throws XmlPullParserException + */ + private static @NonNull ArrayMap<String, VendorConfig> readVendorConfigs( + @NonNull final Context context) throws IOException, XmlPullParserException { + try (XmlResourceParser parser = context.getResources().getXml(R.xml.vendorconfigs)) { + // Skip header + int parsingEvent; + do { + parsingEvent = parser.next(); + } while (parsingEvent != XmlResourceParser.START_TAG); + + ArrayList<VendorConfig> configs = readTagList(parser, VENDORS_TAG, VENDOR_TAG, + new TagReader<VendorConfig>() { + public VendorConfig readTag(XmlPullParser parser, String tagName) + throws XmlPullParserException, IOException { + return readVendorConfig(context, parser, tagName); + } + }); + + ArrayMap<String, VendorConfig> configMap = new ArrayMap<>(configs.size()); + final int numConfigs = configs.size(); + for (int i = 0; i < numConfigs; i++) { + VendorConfig config = configs.get(i); + + configMap.put(config.name, config); + } + + return configMap; + } + } + + /** + * Read a single vendor configuration. + * + * @param parser XML parser to read from + * @param tagName The vendor tag + * @param context Calling context + * + * @return A config + * + * @throws XmlPullParserException + * @throws IOException + */ + private static VendorConfig readVendorConfig(@NonNull final Context context, + @NonNull XmlPullParser parser, @NonNull String tagName) throws XmlPullParserException, + IOException { + parser.require(XmlPullParser.START_TAG, null, tagName); + + String name = null; + String packageName = null; + List<String> mDNSNames = null; + + while (parser.next() != XmlPullParser.END_TAG) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + String subTagName = parser.getName(); + + switch (subTagName) { + case NAME_TAG: + name = readSimpleTag(context, parser, NAME_TAG, false); + break; + case PACKAGE_TAG: + packageName = readSimpleTag(context, parser, PACKAGE_TAG, true); + break; + case MDNSNAMES_TAG: + mDNSNames = readTagList(parser, MDNSNAMES_TAG, MDNSNAME_TAG, + new TagReader<String>() { + public String readTag(XmlPullParser parser, String tagName) + throws XmlPullParserException, IOException { + return readSimpleTag(context, parser, tagName, true); + } + } + ); + break; + default: + throw new XmlPullParserException("Unexpected subtag of " + tagName + ": " + + subTagName); + + } + } + + if (name == null) { + throw new XmlPullParserException("name is required"); + } + + if (packageName == null) { + throw new XmlPullParserException("package is required"); + } + + if (mDNSNames == null) { + mDNSNames = Collections.emptyList(); + } + + // A vendor config should be immutable + mDNSNames = Collections.unmodifiableList(mDNSNames); + + return new VendorConfig(name, packageName, mDNSNames); + } + + @Override + public String toString() { + return name + " -> " + packageName + ", " + mDNSNames; + } + + /** + * Used a a "function pointer" when reading a tag in {@link #readTagList(XmlPullParser, String, + * String, TagReader)}. + * + * @param <T> The type of content to read + */ + private interface TagReader<T> { + T readTag(XmlPullParser parser, String tagName) throws XmlPullParserException, IOException; + } +} diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java new file mode 100644 index 000000000000..0541c3565dba --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java @@ -0,0 +1,98 @@ +/* + * (c) Copyright 2016 Mopria Alliance, Inc. + * (c) Copyright 2016 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.printservice.recommendation.util; + +import android.annotation.NonNull; +import android.net.nsd.NsdServiceInfo; + +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Set; + +/** + * Utils for dealing with mDNS attributes + */ +public class MDNSUtils { + public static final String ATTRIBUTE_TY = "ty"; + public static final String ATTRIBUTE_PRODUCT = "product"; + public static final String ATTRIBUTE_USB_MFG = "usb_mfg"; + public static final String ATTRIBUTE_MFG = "mfg"; + + /** + * Check if the service has any of a set of vendor names. + * + * @param serviceInfo The service + * @param vendorNames The vendors + * + * @return true iff the has any of the set of vendor names + */ + public static boolean isVendorPrinter(@NonNull NsdServiceInfo serviceInfo, + @NonNull Set<String> vendorNames) { + for (Map.Entry<String, byte[]> entry : serviceInfo.getAttributes().entrySet()) { + // keys are case insensitive + String key = entry.getKey().toLowerCase(); + + switch (key) { + case ATTRIBUTE_TY: + case ATTRIBUTE_PRODUCT: + case ATTRIBUTE_USB_MFG: + case ATTRIBUTE_MFG: + if (entry.getValue() != null) { + if (containsVendor(new String(entry.getValue(), StandardCharsets.UTF_8), + vendorNames)) { + return true; + } + } + break; + default: + break; + } + } + + return false; + } + + /** + * Check if the attribute matches any of the vendor names, ignoring capitalization. + * + * @param attr The attribute + * @param vendorNames The vendor names + * + * @return true iff the attribute matches any of the vendor names + */ + private static boolean containsVendor(@NonNull String attr, @NonNull Set<String> vendorNames) { + for (String name : vendorNames) { + if (containsString(attr.toLowerCase(), name.toLowerCase())) { + return true; + } + } + return false; + } + + /** + * Check if a string in another string. + * + * @param container The string that contains the string + * @param contained The string that is contained + * + * @return true if the string is contained in the other + */ + private static boolean containsString(@NonNull String container, @NonNull String contained) { + return container.equalsIgnoreCase(contained) || container.contains(contained + " "); + } +} diff --git a/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java new file mode 100644 index 000000000000..fad50f6a404b --- /dev/null +++ b/packages/PrintServiceRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2016 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.printservice.recommendation.util; + +import android.annotation.NonNull; +import android.net.nsd.NsdManager; +import android.net.nsd.NsdServiceInfo; +import com.android.internal.annotations.GuardedBy; + +import java.util.LinkedList; + +/** + * Nsd resolve requests for the same info cancel each other. Hence this class synchronizes the + * resolutions to hide this effect. + */ +public class NsdResolveQueue { + /** Lock for {@link #sInstance} */ + private static final Object sLock = new Object(); + + /** Instance of this singleton */ + @GuardedBy("sLock") + private static NsdResolveQueue sInstance; + + /** Lock for {@link #mResolveRequests} */ + private final Object mLock = new Object(); + + /** Current set of registered service info resolve attempts */ + @GuardedBy("mLock") + private final LinkedList<NsdResolveRequest> mResolveRequests = new LinkedList<>(); + + public static NsdResolveQueue getInstance() { + synchronized (sLock) { + if (sInstance == null) { + sInstance = new NsdResolveQueue(); + } + + return sInstance; + } + } + + /** + * Container for a request to resolve a serviceInfo. + */ + private static class NsdResolveRequest { + final @NonNull NsdManager nsdManager; + final @NonNull NsdServiceInfo serviceInfo; + final @NonNull NsdManager.ResolveListener listener; + + private NsdResolveRequest(@NonNull NsdManager nsdManager, + @NonNull NsdServiceInfo serviceInfo, @NonNull NsdManager.ResolveListener listener) { + this.nsdManager = nsdManager; + this.serviceInfo = serviceInfo; + this.listener = listener; + } + } + + /** + * Resolve a serviceInfo or queue the request if there is a request currently in flight. + * + * @param nsdManager The nsd manager to use + * @param serviceInfo The service info to resolve + * @param listener The listener to call back once the info is resolved. + */ + public void resolve(@NonNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, + @NonNull NsdManager.ResolveListener listener) { + synchronized (mLock) { + mResolveRequests.addLast(new NsdResolveRequest(nsdManager, serviceInfo, + new ListenerWrapper(listener))); + + if (mResolveRequests.size() == 1) { + resolveNextRequest(); + } + } + } + + /** + * Wrapper for a {@link NsdManager.ResolveListener}. Calls the listener and then + * {@link #resolveNextRequest()}. + */ + private class ListenerWrapper implements NsdManager.ResolveListener { + private final @NonNull NsdManager.ResolveListener mListener; + + private ListenerWrapper(@NonNull NsdManager.ResolveListener listener) { + mListener = listener; + } + + @Override + public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { + mListener.onResolveFailed(serviceInfo, errorCode); + + synchronized (mLock) { + mResolveRequests.pop(); + resolveNextRequest(); + } + } + + @Override + public void onServiceResolved(NsdServiceInfo serviceInfo) { + mListener.onServiceResolved(serviceInfo); + + synchronized (mLock) { + mResolveRequests.pop(); + resolveNextRequest(); + } + } + } + + /** + * Resolve the next request if there is one. + */ + private void resolveNextRequest() { + if (!mResolveRequests.isEmpty()) { + NsdResolveRequest request = mResolveRequests.getFirst(); + + request.nsdManager.resolveService(request.serviceInfo, request.listener); + } + } + +} diff --git a/packages/PrintSpooler/res/drawable/ic_download_from_market.xml b/packages/PrintSpooler/res/drawable/ic_download_from_market.xml new file mode 100644 index 000000000000..44a5edf5c9f8 --- /dev/null +++ b/packages/PrintSpooler/res/drawable/ic_download_from_market.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2016 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. +--> + +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="36dp" + android:height="36dp" + android:viewportWidth="48.0" + android:viewportHeight="48.0"> + <path + android:pathData="M40,12h-8L32,8l-4,-4h-8l-4,4v4L8,12c-2.21,0 -3.98,1.79 -3.98,4L4,38c0,2.21 1.79,4 4,4h32c2.21,0 4,-1.79 4,-4L44,16c0,-2.21 -1.79,-4 -4,-4zM20,8h8v4h-8L20,8zM24,38L14,28h6v-8h8v8h6L24,38z" + android:fillColor="?android:attr/colorAccent"/> +</vector> diff --git a/packages/PrintSpooler/res/layout/print_service_recommendations_list_item.xml b/packages/PrintSpooler/res/layout/print_service_recommendations_list_item.xml new file mode 100644 index 000000000000..86ac26db5de0 --- /dev/null +++ b/packages/PrintSpooler/res/layout/print_service_recommendations_list_item.xml @@ -0,0 +1,56 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2016 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. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="?android:attr/listPreferredItemHeight" + android:paddingStart="?android:attr/listPreferredItemPaddingStart" + android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" + android:orientation="horizontal" + android:gravity="start|center_vertical"> + + <ImageView + android:layout_width="36dip" + android:layout_height="36dip" + android:src="@drawable/ic_download_from_market" + android:layout_marginRight="4dip" + android:layout_gravity="center_vertical" + android:contentDescription="@null" /> + + <RelativeLayout + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_marginStart="16dip"> + + <TextView + android:id="@+id/title" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:textAppearance="?android:attr/textAppearanceListItem" + android:singleLine="true" + android:ellipsize="end" /> + + <TextView + android:id="@+id/subtitle" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_below="@id/title" + android:textAppearance="?android:attr/textAppearanceListItemSecondary" + android:text="@string/enable_print_service" /> + + </RelativeLayout> + +</LinearLayout> diff --git a/packages/PrintSpooler/res/values/strings.xml b/packages/PrintSpooler/res/values/strings.xml index 2f24d2cbd240..2836adb9830e 100644 --- a/packages/PrintSpooler/res/values/strings.xml +++ b/packages/PrintSpooler/res/values/strings.xml @@ -185,6 +185,12 @@ <!-- Label for the list item that links to the list of all print services. [CHAR LIMIT=50] --> <string name="all_services_title">All services</string> + <!-- Subtitle for a print service recommendation. [CHAR LIMIT=50] --> + <plurals name="print_services_recommendation_subtitle"> + <item quantity="one">Install to discover <xliff:g id="count" example="1">%1$s</xliff:g> printer</item> + <item quantity="other">Install to discover <xliff:g id="count" example="2">%1$s</xliff:g> printers</item> + </plurals> + <!-- Notifications --> <!-- Template for the notification label for a printing print job. [CHAR LIMIT=25] --> diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java index f2b3e6ee04c7..42ef10e01158 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java @@ -30,10 +30,13 @@ import android.database.DataSetObserver; import android.net.Uri; import android.os.Bundle; import android.print.PrintManager; +import android.printservice.recommendation.RecommendationInfo; +import android.print.PrintServiceRecommendationsLoader; import android.print.PrintServicesLoader; import android.printservice.PrintServiceInfo; import android.provider.Settings; import android.text.TextUtils; +import android.util.ArraySet; import android.util.Log; import android.util.Pair; import android.view.View; @@ -45,8 +48,10 @@ import android.widget.ImageView; import android.widget.TextView; import com.android.printspooler.R; +import java.text.Collator; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; /** @@ -57,31 +62,38 @@ import java.util.List; * when the item is clicked.</li> * <li>{@link #mDisabledServicesAdapter} for all disabled services. Once clicked the settings page * for this service is opened.</li> - * <li>{@link RecommendedServicesAdapter} for a link to all services. If this item is clicked + * <li>{@link #mRecommendedServicesAdapter} for a link to all services. If this item is clicked * the market app is opened to show all print services.</li> * </ul> */ -public class AddPrinterActivity extends ListActivity implements - LoaderManager.LoaderCallbacks<List<PrintServiceInfo>>, - AdapterView.OnItemClickListener { +public class AddPrinterActivity extends ListActivity implements AdapterView.OnItemClickListener { private static final String LOG_TAG = "AddPrinterActivity"; /** Ids for the loaders */ private static final int LOADER_ID_ENABLED_SERVICES = 1; private static final int LOADER_ID_DISABLED_SERVICES = 2; + private static final int LOADER_ID_RECOMMENDED_SERVICES = 3; + private static final int LOADER_ID_ALL_SERVICES = 4; /** * The enabled services list. This is filled from the {@link #LOADER_ID_ENABLED_SERVICES} - * loader in {@link #onLoadFinished}. + * loader in {@link PrintServiceInfoLoaderCallbacks#onLoadFinished}. */ private EnabledServicesAdapter mEnabledServicesAdapter; /** * The disabled services list. This is filled from the {@link #LOADER_ID_DISABLED_SERVICES} - * loader in {@link #onLoadFinished}. + * loader in {@link PrintServiceInfoLoaderCallbacks#onLoadFinished}. */ private DisabledServicesAdapter mDisabledServicesAdapter; + /** + * The recommended services list. This is filled from the + * {@link #LOADER_ID_RECOMMENDED_SERVICES} loader in + * {@link PrintServicePrintServiceRecommendationLoaderCallbacks#onLoadFinished}. + */ + private RecommendedServicesAdapter mRecommendedServicesAdapter; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -90,75 +102,122 @@ public class AddPrinterActivity extends ListActivity implements mEnabledServicesAdapter = new EnabledServicesAdapter(); mDisabledServicesAdapter = new DisabledServicesAdapter(); + mRecommendedServicesAdapter = new RecommendedServicesAdapter(); ArrayList<ActionAdapter> adapterList = new ArrayList<>(3); adapterList.add(mEnabledServicesAdapter); - adapterList.add(new RecommendedServicesAdapter()); + adapterList.add(mRecommendedServicesAdapter); adapterList.add(mDisabledServicesAdapter); setListAdapter(new CombinedAdapter(adapterList)); getListView().setOnItemClickListener(this); - getLoaderManager().initLoader(LOADER_ID_ENABLED_SERVICES, null, this); - getLoaderManager().initLoader(LOADER_ID_DISABLED_SERVICES, null, this); - // TODO: Load recommended services - } + PrintServiceInfoLoaderCallbacks printServiceLoaderCallbacks = + new PrintServiceInfoLoaderCallbacks(); - @Override - public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) { - switch (id) { - case LOADER_ID_ENABLED_SERVICES: - return new PrintServicesLoader( - (PrintManager) getSystemService(Context.PRINT_SERVICE), this, - PrintManager.ENABLED_SERVICES); - case LOADER_ID_DISABLED_SERVICES: - return new PrintServicesLoader( - (PrintManager) getSystemService(Context.PRINT_SERVICE), this, - PrintManager.DISABLED_SERVICES); - // TODO: Load recommended services - default: - // not reached - return null; - } + getLoaderManager().initLoader(LOADER_ID_ENABLED_SERVICES, null, printServiceLoaderCallbacks); + getLoaderManager().initLoader(LOADER_ID_DISABLED_SERVICES, null, printServiceLoaderCallbacks); + getLoaderManager().initLoader(LOADER_ID_RECOMMENDED_SERVICES, null, + new PrintServicePrintServiceRecommendationLoaderCallbacks()); + getLoaderManager().initLoader(LOADER_ID_ALL_SERVICES, null, printServiceLoaderCallbacks); } - @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - ((ActionAdapter) getListAdapter()).performAction(position); - } - - @Override - public void onLoadFinished(Loader<List<PrintServiceInfo>> loader, - List<PrintServiceInfo> data) { - switch (loader.getId()) { - case LOADER_ID_ENABLED_SERVICES: - mEnabledServicesAdapter.updateData(data); - break; - case LOADER_ID_DISABLED_SERVICES: - mDisabledServicesAdapter.updateData(data); - break; - // TODO: Load recommended services - default: - // not reached + /** + * Callbacks for the loaders operating on list of {@link PrintServiceInfo print service infos}. + */ + private class PrintServiceInfoLoaderCallbacks implements + LoaderManager.LoaderCallbacks<List<PrintServiceInfo>> { + @Override + public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) { + switch (id) { + case LOADER_ID_ENABLED_SERVICES: + return new PrintServicesLoader( + (PrintManager) getSystemService(Context.PRINT_SERVICE), + AddPrinterActivity.this, PrintManager.ENABLED_SERVICES); + case LOADER_ID_DISABLED_SERVICES: + return new PrintServicesLoader( + (PrintManager) getSystemService(Context.PRINT_SERVICE), + AddPrinterActivity.this, PrintManager.DISABLED_SERVICES); + case LOADER_ID_ALL_SERVICES: + return new PrintServicesLoader( + (PrintManager) getSystemService(Context.PRINT_SERVICE), + AddPrinterActivity.this, PrintManager.ALL_SERVICES); + default: + // not reached + return null; + } } - } - @Override - public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) { - if (!isFinishing()) { + + @Override + public void onLoadFinished(Loader<List<PrintServiceInfo>> loader, + List<PrintServiceInfo> data) { switch (loader.getId()) { case LOADER_ID_ENABLED_SERVICES: - mEnabledServicesAdapter.updateData(null); + mEnabledServicesAdapter.updateData(data); break; case LOADER_ID_DISABLED_SERVICES: - mDisabledServicesAdapter.updateData(null); + mDisabledServicesAdapter.updateData(data); break; - // TODO: Reset recommended services + case LOADER_ID_ALL_SERVICES: + mRecommendedServicesAdapter.updateInstalledServices(data); default: // not reached } } + + @Override + public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) { + if (!isFinishing()) { + switch (loader.getId()) { + case LOADER_ID_ENABLED_SERVICES: + mEnabledServicesAdapter.updateData(null); + break; + case LOADER_ID_DISABLED_SERVICES: + mDisabledServicesAdapter.updateData(null); + break; + case LOADER_ID_ALL_SERVICES: + mRecommendedServicesAdapter.updateInstalledServices(null); + break; + default: + // not reached + } + } + } + } + + /** + * Callbacks for the loaders operating on list of {@link RecommendationInfo print service + * recommendations}. + */ + private class PrintServicePrintServiceRecommendationLoaderCallbacks implements + LoaderManager.LoaderCallbacks<List<RecommendationInfo>> { + @Override + public Loader<List<RecommendationInfo>> onCreateLoader(int id, Bundle args) { + return new PrintServiceRecommendationsLoader( + (PrintManager) getSystemService(Context.PRINT_SERVICE), + AddPrinterActivity.this); + } + + + @Override + public void onLoadFinished(Loader<List<RecommendationInfo>> loader, + List<RecommendationInfo> data) { + mRecommendedServicesAdapter.updateRecommendations(data); + } + + @Override + public void onLoaderReset(Loader<List<RecommendationInfo>> loader) { + if (!isFinishing()) { + mRecommendedServicesAdapter.updateRecommendations(null); + } + } + } + + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + ((ActionAdapter) getListAdapter()).performAction(position); } /** @@ -490,28 +549,65 @@ public class AddPrinterActivity extends ListActivity implements * Adapter for the recommended services. */ private class RecommendedServicesAdapter extends ActionAdapter { + /** Package names of all installed print services */ + private @NonNull final ArraySet<String> mInstalledServices; + + /** All print service recommendations */ + private @Nullable List<RecommendationInfo> mRecommendations; + + /** + * Sorted print service recommendations for services that are not installed + * + * @see #filterRecommendations + */ + private @Nullable List<RecommendationInfo> mFilteredRecommendations; + + /** + * Create a new adapter. + */ + private RecommendedServicesAdapter() { + mInstalledServices = new ArraySet<>(); + } + @Override public int getCount() { - return 2; + if (mFilteredRecommendations == null) { + return 2; + } else { + return mFilteredRecommendations.size() + 2; + } } @Override public int getViewTypeCount() { - return 2; + return 3; + } + + /** + * @return The position the all services link is at. + */ + private int getAllServicesPos() { + return getCount() - 1; } @Override public int getItemViewType(int position) { if (position == 0) { return 0; - } else { + } else if (getAllServicesPos() == position) { return 1; + } else { + return 2; } } @Override public Object getItem(int position) { - return null; + if (position == 0 || position == getAllServicesPos()) { + return null; + } else { + return mFilteredRecommendations.get(position - 1); + } } @Override @@ -531,11 +627,27 @@ public class AddPrinterActivity extends ListActivity implements .setText(R.string.recommended_services_title); return convertView; - } + } else if (position == getAllServicesPos()) { + if (convertView == null) { + convertView = getLayoutInflater().inflate(R.layout.all_print_services_list_item, + parent, false); + } + } else { + RecommendationInfo recommendation = (RecommendationInfo) getItem(position); - if (convertView == null) { - convertView = getLayoutInflater().inflate(R.layout.all_print_services_list_item, - parent, false); + if (convertView == null) { + convertView = getLayoutInflater().inflate( + R.layout.print_service_recommendations_list_item, parent, false); + } + + ((TextView) convertView.findViewById(R.id.title)).setText(recommendation.getName()); + + ((TextView) convertView.findViewById(R.id.subtitle)).setText(getResources() + .getQuantityString(R.plurals.print_services_recommendation_subtitle, + recommendation.getNumDiscoveredPrinters(), + recommendation.getNumDiscoveredPrinters())); + + return convertView; } return convertView; @@ -548,16 +660,107 @@ public class AddPrinterActivity extends ListActivity implements @Override public void performAction(@IntRange(from = 0) int position) { - String searchUri = Settings.Secure - .getString(getContentResolver(), Settings.Secure.PRINT_SERVICE_SEARCH_URI); + if (position == getAllServicesPos()) { + String searchUri = Settings.Secure + .getString(getContentResolver(), Settings.Secure.PRINT_SERVICE_SEARCH_URI); + + if (searchUri != null) { + try { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri))); + } catch (ActivityNotFoundException e) { + Log.e(LOG_TAG, "Cannot start market", e); + } + } + } else { + RecommendationInfo recommendation = (RecommendationInfo) getItem(position); - if (searchUri != null) { try { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri))); + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString( + R.string.uri_package_details, recommendation.getPackageName())))); } catch (ActivityNotFoundException e) { Log.e(LOG_TAG, "Cannot start market", e); } } } + + /** + * Filter recommended services. + */ + private void filterRecommendations() { + if (mRecommendations == null) { + mFilteredRecommendations = null; + } else { + mFilteredRecommendations = new ArrayList<>(); + + // Filter out recommendations for already installed services + final int numRecommendations = mRecommendations.size(); + for (int i = 0; i < numRecommendations; i++) { + RecommendationInfo recommendation = mRecommendations.get(i); + + if (!mInstalledServices.contains(recommendation.getPackageName())) { + mFilteredRecommendations.add(recommendation); + } + } + } + + notifyDataSetChanged(); + } + + /** + * Update the installed print services. + * + * @param services The new set of services + */ + public void updateInstalledServices(List<PrintServiceInfo> services) { + mInstalledServices.clear(); + + final int numServices = services.size(); + for (int i = 0; i < numServices; i++) { + mInstalledServices.add(services.get(i).getComponentName().getPackageName()); + } + + filterRecommendations(); + } + + /** + * Update the recommended print services. + * + * @param recommendations The new set of recommendations + */ + public void updateRecommendations(List<RecommendationInfo> recommendations) { + if (recommendations != null) { + final Collator collator = Collator.getInstance(); + + // Sort recommendations (early conditions are more important) + // - higher number of discovered printers first + // - single vendor services first + // - alphabetically + Collections.sort(recommendations, + new Comparator<RecommendationInfo>() { + @Override public int compare(RecommendationInfo o1, + RecommendationInfo o2) { + if (o1.getNumDiscoveredPrinters() != + o2.getNumDiscoveredPrinters()) { + return o2.getNumDiscoveredPrinters() - + o1.getNumDiscoveredPrinters(); + } else if (o1.recommendsMultiVendorService() + != o2.recommendsMultiVendorService()) { + if (o1.recommendsMultiVendorService()) { + return 1; + } else { + return -1; + } + } else { + return collator.compare(o1.getName().toString(), + o2.getName().toString()); + } + } + }); + } + + mRecommendations = recommendations; + + filterRecommendations(); + } } } diff --git a/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java b/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java index 1f1a9b81ed48..11352009a3b8 100644 --- a/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java @@ -78,7 +78,8 @@ public class RestrictedLockUtils { int deviceOwnerUserId = dpm.getDeviceOwnerUserId(); boolean enforcedByDeviceOwner = false; if (deviceOwner != null && deviceOwnerUserId != UserHandle.USER_NULL) { - Bundle enforcedRestrictions = dpm.getUserRestrictions(deviceOwner, deviceOwnerUserId); + Bundle enforcedRestrictions = + dpm.getUserRestrictionsForUser(deviceOwner, deviceOwnerUserId); if (enforcedRestrictions != null && enforcedRestrictions.getBoolean(userRestriction, false)) { enforcedByDeviceOwner = true; @@ -90,7 +91,8 @@ public class RestrictedLockUtils { if (userId != UserHandle.USER_NULL) { profileOwner = dpm.getProfileOwnerAsUser(userId); if (profileOwner != null) { - Bundle enforcedRestrictions = dpm.getUserRestrictions(profileOwner, userId); + Bundle enforcedRestrictions = + dpm.getUserRestrictionsForUser(profileOwner, userId); if (enforcedRestrictions != null && enforcedRestrictions.getBoolean(userRestriction, false)) { enforcedByProfileOwner = true; diff --git a/packages/Shell/src/com/android/shell/BugreportStorageProvider.java b/packages/Shell/src/com/android/shell/BugreportStorageProvider.java index 49759c5f6ee1..814aa8cb8c06 100644 --- a/packages/Shell/src/com/android/shell/BugreportStorageProvider.java +++ b/packages/Shell/src/com/android/shell/BugreportStorageProvider.java @@ -56,7 +56,7 @@ public class BugreportStorageProvider extends DocumentsProvider { final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection)); final RowBuilder row = result.newRow(); row.add(Root.COLUMN_ROOT_ID, DOC_ID_ROOT); - row.add(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY); + row.add(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY | Root.FLAG_ADVANCED); row.add(Root.COLUMN_ICON, android.R.mipmap.sym_def_app_icon); row.add(Root.COLUMN_TITLE, getContext().getString(R.string.bugreport_storage_title)); row.add(Root.COLUMN_DOCUMENT_ID, DOC_ID_ROOT); diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java index 8777a9140a51..3e32905d64cd 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java @@ -24,8 +24,6 @@ import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.os.UserHandle; -import android.os.UserManager; import android.util.Log; import android.util.SparseArray; import android.view.View; @@ -85,8 +83,6 @@ public abstract class QSTile<TState extends State> implements Listenable { abstract protected void handleClick(); abstract protected void handleUpdateState(TState state, Object arg); - private UserManager mUserManager; - /** * Declare the category of this tile. * @@ -99,7 +95,6 @@ public abstract class QSTile<TState extends State> implements Listenable { mHost = host; mContext = host.getContext(); mHandler = new H(host.getLooper()); - mUserManager = UserManager.get(mContext); } public String getTileSpec() { @@ -290,11 +285,12 @@ public abstract class QSTile<TState extends State> implements Listenable { } protected void checkIfRestrictionEnforcedByAdminOnly(State state, String userRestriction) { - UserHandle user = UserHandle.of(ActivityManager.getCurrentUser()); - if (mUserManager.hasUserRestriction(userRestriction, user) - && !mUserManager.hasBaseUserRestriction(userRestriction, user)) { + EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, + userRestriction, ActivityManager.getCurrentUser()); + if (admin != null && !RestrictedLockUtils.hasBaseUserRestriction(mContext, + userRestriction, ActivityManager.getCurrentUser())) { state.disabledByPolicy = true; - state.enforcedAdmin = EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN; + state.enforcedAdmin = admin; } else { state.disabledByPolicy = false; state.enforcedAdmin = null; diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java index d7777d569337..fff7d78d0078 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java @@ -428,8 +428,8 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD } @Override - public void onMultiWindowChanged(boolean inMultiWindow) { - super.onMultiWindowChanged(inMultiWindow); + public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { + super.onMultiWindowModeChanged(isInMultiWindowMode); EventBus.getDefault().send(new ConfigurationChangedEvent(true /* fromMultiWindow */, false /* fromOrientationChange */)); @@ -449,7 +449,7 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD mRecentsView.updateStack(loadPlan.getTaskStack()); } - EventBus.getDefault().send(new MultiWindowStateChangedEvent(inMultiWindow)); + EventBus.getDefault().send(new MultiWindowStateChangedEvent(isInMultiWindowMode)); } @Override diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java index 95f26d4cde87..6a2ecf4e6b0b 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java @@ -654,7 +654,7 @@ public class StatusBarWindowView extends FrameLayout { } @Override - public void onMultiWindowChanged() { + public void onMultiWindowModeChanged() { } @Override diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java index 84e785d066ce..ab44b6a277ee 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java @@ -654,11 +654,12 @@ public class UserSwitcherController { } private void checkIfAddUserDisallowedByAdminOnly(UserRecord record) { - UserHandle user = UserHandle.of(ActivityManager.getCurrentUser()); - if (mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER, user) - && !mUserManager.hasBaseUserRestriction(UserManager.DISALLOW_ADD_USER, user)) { + EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, + UserManager.DISALLOW_ADD_USER, ActivityManager.getCurrentUser()); + if (admin != null && !RestrictedLockUtils.hasBaseUserRestriction(mContext, + UserManager.DISALLOW_ADD_USER, ActivityManager.getCurrentUser())) { record.isDisabledByAdmin = true; - record.enforcedAdmin = EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN; + record.enforcedAdmin = admin; } else { record.isDisabledByAdmin = false; record.enforcedAdmin = null; diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 4bb3a541a23f..42cf42fdd650 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -351,7 +351,6 @@ import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV; import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE; import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN; import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_RELAUNCH; -import static com.android.server.wm.AppTransition.TRANSIT_DOCK_TASK_FROM_RECENTS; import static com.android.server.wm.AppTransition.TRANSIT_TASK_IN_PLACE; import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN; import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT; @@ -7231,7 +7230,7 @@ public final class ActivityManagerService extends ActivityManagerNative } @Override - public boolean inMultiWindow(IBinder token) { + public boolean isInMultiWindowMode(IBinder token) { final long origId = Binder.clearCallingIdentity(); try { synchronized(this) { @@ -7248,7 +7247,7 @@ public final class ActivityManagerService extends ActivityManagerNative } @Override - public boolean inPictureInPicture(IBinder token) { + public boolean isInPictureInPictureMode(IBinder token) { final long origId = Binder.clearCallingIdentity(); try { synchronized(this) { @@ -7264,24 +7263,24 @@ public final class ActivityManagerService extends ActivityManagerNative } @Override - public void enterPictureInPicture(IBinder token) { + public void enterPictureInPictureMode(IBinder token) { final long origId = Binder.clearCallingIdentity(); try { synchronized(this) { if (!mSupportsPictureInPicture) { - throw new IllegalStateException("enterPictureInPicture: " + throw new IllegalStateException("enterPictureInPictureMode: " + "Device doesn't support picture-in-picture mode."); } final ActivityRecord r = ActivityRecord.forTokenLocked(token); if (r == null) { - throw new IllegalStateException("enterPictureInPicture: " + throw new IllegalStateException("enterPictureInPictureMode: " + "Can't find activity for token=" + token); } if (!r.supportsPictureInPicture()) { - throw new IllegalArgumentException("enterPictureInPicture: " + throw new IllegalArgumentException("enterPictureInPictureMode: " + "Picture-In-Picture not supported for r=" + r); } @@ -7290,7 +7289,7 @@ public final class ActivityManagerService extends ActivityManagerNative ? mDefaultPinnedStackBounds : null; mStackSupervisor.moveActivityToPinnedStackLocked( - r, "enterPictureInPicture", bounds); + r, "enterPictureInPictureMode", bounds); } } finally { Binder.restoreCallingIdentity(origId); diff --git a/services/core/java/com/android/server/am/ActivityRecord.java b/services/core/java/com/android/server/am/ActivityRecord.java index 5219827d2f8e..48f87b66fc2c 100755 --- a/services/core/java/com/android/server/am/ActivityRecord.java +++ b/services/core/java/com/android/server/am/ActivityRecord.java @@ -23,7 +23,6 @@ import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS; import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE; import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE; import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION; -import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE; import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH; import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS; import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES; @@ -452,24 +451,24 @@ final class ActivityRecord { } } - void scheduleMultiWindowChanged() { + void scheduleMultiWindowModeChanged() { if (task == null || task.stack == null || app == null || app.thread == null) { return; } try { // An activity is considered to be in multi-window mode if its task isn't fullscreen. - app.thread.scheduleMultiWindowChanged(appToken, !task.mFullscreen); + app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen); } catch (Exception e) { // If process died, I don't care. } } - void schedulePictureInPictureChanged() { + void schedulePictureInPictureModeChanged() { if (task == null || task.stack == null || app == null || app.thread == null) { return; } try { - app.thread.schedulePictureInPictureChanged( + app.thread.schedulePictureInPictureModeChanged( appToken, task.stack.mStackId == PINNED_STACK_ID); } catch (Exception e) { // If process died, no one cares. diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java index a9ef1d60ebf6..b297938b8c86 100644 --- a/services/core/java/com/android/server/am/ActivityStack.java +++ b/services/core/java/com/android/server/am/ActivityStack.java @@ -4986,7 +4986,7 @@ final class ActivityStack { private void postAddTask(TaskRecord task, ActivityStack prevStack) { if (prevStack != null) { - mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack); + mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack); } else if (task.voiceSession != null) { try { task.voiceSession.taskStarted(task.intent, task.taskId); @@ -5045,7 +5045,7 @@ final class ActivityStack { r.setTask(task, null); task.addActivityToTop(r); setAppTask(r, task); - mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack); + mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack); moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack"); if (wasResumed) { prevStack.mResumedActivity = null; diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java index 950320ec49a5..0d70e9901f68 100644 --- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java +++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java @@ -102,7 +102,6 @@ import java.util.List; import java.util.Objects; import java.util.Set; -import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS; import static android.Manifest.permission.START_ANY_ACTIVITY; import static android.Manifest.permission.START_TASKS_FROM_RECENTS; import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED; @@ -3524,7 +3523,7 @@ public final class ActivityStackSupervisor implements DisplayListener { mActivityMetricsLogger.logWindowState(); } - void scheduleReportMultiWindowChanged(TaskRecord task) { + void scheduleReportMultiWindowModeChanged(TaskRecord task) { for (int i = task.mActivities.size() - 1; i >= 0; i--) { final ActivityRecord r = task.mActivities.get(i); if (r.app != null && r.app.thread != null) { @@ -3537,7 +3536,7 @@ public final class ActivityStackSupervisor implements DisplayListener { } } - void scheduleReportPictureInPictureChangedIfNeeded(TaskRecord task, ActivityStack prevStack) { + void scheduleReportPictureInPictureModeChangedIfNeeded(TaskRecord task, ActivityStack prevStack) { final ActivityStack stack = task.stack; if (prevStack == null || prevStack == stack || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) { @@ -3575,7 +3574,7 @@ public final class ActivityStackSupervisor implements DisplayListener { synchronized (mService) { for (int i = mMultiWindowModeChangedActivities.size() - 1; i >= 0; i--) { final ActivityRecord r = mMultiWindowModeChangedActivities.remove(i); - r.scheduleMultiWindowChanged(); + r.scheduleMultiWindowModeChanged(); } } } break; @@ -3583,7 +3582,7 @@ public final class ActivityStackSupervisor implements DisplayListener { synchronized (mService) { for (int i = mPipModeChangedActivities.size() - 1; i >= 0; i--) { final ActivityRecord r = mPipModeChangedActivities.remove(i); - r.schedulePictureInPictureChanged(); + r.schedulePictureInPictureModeChanged(); } } } break; diff --git a/services/core/java/com/android/server/am/TaskRecord.java b/services/core/java/com/android/server/am/TaskRecord.java index 0f1ebebce23e..b157070b1184 100644 --- a/services/core/java/com/android/server/am/TaskRecord.java +++ b/services/core/java/com/android/server/am/TaskRecord.java @@ -1449,7 +1449,7 @@ final class TaskRecord { } if (mFullscreen != oldFullscreen) { - mService.mStackSupervisor.scheduleReportMultiWindowChanged(this); + mService.mStackSupervisor.scheduleReportMultiWindowModeChanged(this); } return !mOverrideConfig.equals(oldConfig) ? mOverrideConfig : null; diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java index 574faa01a241..747e31e5c2a8 100644 --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -18,7 +18,6 @@ package com.android.server.policy; import static android.app.ActivityManager.StackId.DOCKED_STACK_ID; import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID; -import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; import static android.app.ActivityManager.StackId.HOME_STACK_ID; import static android.content.pm.PackageManager.FEATURE_PICTURE_IN_PICTURE; import static android.content.pm.PackageManager.FEATURE_TELEVISION; @@ -38,7 +37,6 @@ import static android.view.WindowManagerPolicy.WindowManagerFuncs.LID_ABSENT; import static android.view.WindowManagerPolicy.WindowManagerFuncs.LID_CLOSED; import static android.view.WindowManagerPolicy.WindowManagerFuncs.LID_OPEN; -import android.Manifest; import android.app.ActivityManager; import android.app.ActivityManager.StackId; import android.app.ActivityManagerInternal; @@ -4647,7 +4645,7 @@ public class PhoneWindowManager implements WindowManagerPolicy { // TYPE_SYSTEM_ERROR is above the NavigationBar so it can't be allowed to extend over it. // Also, we don't allow windows in multi-window mode to extend out of the screen. if ((fl & FLAG_LAYOUT_NO_LIMITS) != 0 && attrs.type != TYPE_SYSTEM_ERROR - && !win.inMultiWindowMode()) { + && !win.isInMultiWindowMode()) { df.left = df.top = -10000; df.right = df.bottom = 10000; if (attrs.type != TYPE_WALLPAPER) { diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java index 0866c039af1e..57ead8b3a617 100644 --- a/services/core/java/com/android/server/wm/WindowState.java +++ b/services/core/java/com/android/server/wm/WindowState.java @@ -642,7 +642,7 @@ final class WindowState implements WindowManagerPolicy.WindowState { mHaveFrame = true; final Task task = getTask(); - final boolean fullscreenTask = !inMultiWindowMode(); + final boolean fullscreenTask = !isInMultiWindowMode(); final boolean windowsAreFloating = task != null && task.isFloating(); // If the task has temp inset bounds set, we have to make sure all its windows uses @@ -2226,7 +2226,7 @@ final class WindowState implements WindowManagerPolicy.WindowState { } @Override - public boolean inMultiWindowMode() { + public boolean isInMultiWindowMode() { final Task task = getTask(); return task != null && !task.isFullscreen(); } @@ -2527,7 +2527,7 @@ final class WindowState implements WindowManagerPolicy.WindowState { final int pw = mContainingFrame.width(); final int ph = mContainingFrame.height(); final Task task = getTask(); - final boolean nonFullscreenTask = inMultiWindowMode(); + final boolean nonFullscreenTask = isInMultiWindowMode(); final boolean fitToDisplay = task != null && !task.isFloating() && !layoutInParentFrame(); float x, y; int w,h; diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 3e368f5d638b..96ec02add8e0 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -5796,8 +5796,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { transitionCheckNeeded = false; } else { // For all other cases, caller must have MANAGE_PROFILE_AND_DEVICE_OWNERS. - mContext.enforceCallingOrSelfPermission( - android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, null); + enforceCanManageProfileAndDeviceOwners(); } final DevicePolicyData policyData = getUserData(userHandle); @@ -5990,8 +5989,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } return; } - mContext.enforceCallingOrSelfPermission( - android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, null); + enforceCanManageProfileAndDeviceOwners(); if (hasUserSetupCompleted(userHandle) && !isCallerWithSystemUid()) { throw new IllegalStateException("Cannot set the profile owner on a user which is " + "already set-up"); @@ -6006,8 +6004,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { int callingUid = mInjector.binderGetCallingUid(); boolean isAdb = callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID; if (!isAdb) { - mContext.enforceCallingOrSelfPermission( - android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, null); + enforceCanManageProfileAndDeviceOwners(); } final int code = checkSetDeviceOwnerPreCondition(userId, isAdb); @@ -6663,6 +6660,9 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } synchronized (this) { ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); + if (admin == null) { + return false; + } if (admin.permittedAccessiblityServices == null) { return true; } @@ -6833,6 +6833,9 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } synchronized (this) { ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); + if (admin == null) { + return false; + } if (admin.permittedInputMethods == null) { return true; } @@ -7103,19 +7106,30 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } @Override - public Bundle getUserRestrictions(ComponentName who, int userHandle) { + public Bundle getUserRestrictions(ComponentName who) { + if (!mHasFeature) { + return null; + } + Preconditions.checkNotNull(who, "ComponentName is null"); + synchronized (this) { + final ActiveAdmin activeAdmin = getActiveAdminForCallerLocked(who, + DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); + return activeAdmin.userRestrictions; + } + } + + @Override + public Bundle getUserRestrictionsForUser(ComponentName who, int userHandle) { + if (!mHasFeature) { + return null; + } Preconditions.checkNotNull(who, "ComponentName is null"); enforceFullCrossUsersPermission(userHandle); + enforceCanManageProfileAndDeviceOwners(); synchronized (this) { ActiveAdmin activeAdmin = getActiveAdminUncheckedLocked(who, userHandle); if (activeAdmin == null) { - throw new SecurityException("No active admin: " + activeAdmin); - } - if (activeAdmin.getUid() != mInjector.binderGetCallingUid()) { - mContext.enforceCallingOrSelfPermission( - android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, - "Calling uid " + mInjector.binderGetCallingUid() + " neither owns the admin" - + " " + who + " nor has MANAGE_PROFILE_AND_DEVICE_OWNERS permission"); + return null; } return activeAdmin.userRestrictions; } @@ -8689,6 +8703,11 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { null); } + private void enforceCanManageProfileAndDeviceOwners() { + mContext.enforceCallingOrSelfPermission( + android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, null); + } + @Override public boolean isUninstallInQueue(final String packageName) { enforceCanManageDeviceAdmin(); diff --git a/services/print/java/com/android/server/print/PrintManagerService.java b/services/print/java/com/android/server/print/PrintManagerService.java index 985917bf68da..4d02928920c6 100644 --- a/services/print/java/com/android/server/print/PrintManagerService.java +++ b/services/print/java/com/android/server/print/PrintManagerService.java @@ -41,12 +41,14 @@ import android.os.UserManager; import android.print.IPrintDocumentAdapter; import android.print.IPrintJobStateChangeListener; import android.print.IPrintManager; +import android.printservice.recommendation.IRecommendationsChangeListener; import android.print.IPrintServicesChangeListener; import android.print.IPrinterDiscoveryObserver; import android.print.PrintAttributes; import android.print.PrintJobId; import android.print.PrintJobInfo; import android.print.PrintManager; +import android.printservice.recommendation.RecommendationInfo; import android.print.PrinterId; import android.printservice.PrintServiceInfo; import android.provider.Settings; @@ -265,7 +267,7 @@ public final class PrintManagerService extends SystemService { final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); final UserState userState; synchronized (mLock) { - // Only the current group members can get enabled services. + // Only the current group members can get print services. if (resolveCallingProfileParentLocked(resolvedUserId) != getCurrentUserId()) { return null; } @@ -314,6 +316,25 @@ public final class PrintManagerService extends SystemService { } @Override + public List<RecommendationInfo> getPrintServiceRecommendations(int userId) { + final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); + final UserState userState; + synchronized (mLock) { + // Only the current group members can get print service recommendations. + if (resolveCallingProfileParentLocked(resolvedUserId) != getCurrentUserId()) { + return null; + } + userState = getOrCreateUserStateLocked(resolvedUserId, false); + } + final long identity = Binder.clearCallingIdentity(); + try { + return userState.getPrintServiceRecommendations(); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + + @Override public void createPrinterDiscoverySession(IPrinterDiscoveryObserver observer, int userId) { observer = Preconditions.checkNotNull(observer); @@ -543,7 +564,7 @@ public final class PrintManagerService extends SystemService { final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); final UserState userState; synchronized (mLock) { - // Only the current group members can remove a print job listener. + // Only the current group members can remove a print services change listener. if (resolveCallingProfileParentLocked(resolvedUserId) != getCurrentUserId()) { return; } @@ -558,6 +579,52 @@ public final class PrintManagerService extends SystemService { } @Override + public void addPrintServiceRecommendationsChangeListener( + IRecommendationsChangeListener listener, int userId) + throws RemoteException { + listener = Preconditions.checkNotNull(listener); + + final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); + final UserState userState; + synchronized (mLock) { + // Only the current group members can add a print service recommendations listener. + if (resolveCallingProfileParentLocked(resolvedUserId) != getCurrentUserId()) { + return; + } + userState = getOrCreateUserStateLocked(resolvedUserId, false); + } + final long identity = Binder.clearCallingIdentity(); + try { + userState.addPrintServiceRecommendationsChangeListener(listener); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + + @Override + public void removePrintServiceRecommendationsChangeListener( + IRecommendationsChangeListener listener, int userId) { + listener = Preconditions.checkNotNull(listener); + + final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); + final UserState userState; + synchronized (mLock) { + // Only the current group members can remove a print service recommendations + // listener. + if (resolveCallingProfileParentLocked(resolvedUserId) != getCurrentUserId()) { + return; + } + userState = getOrCreateUserStateLocked(resolvedUserId, false); + } + final long identity = Binder.clearCallingIdentity(); + try { + userState.removePrintServiceRecommendationsChangeListener(listener); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + + @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { fd = Preconditions.checkNotNull(fd); pw = Preconditions.checkNotNull(pw); diff --git a/services/print/java/com/android/server/print/RemotePrintServiceRecommendationService.java b/services/print/java/com/android/server/print/RemotePrintServiceRecommendationService.java new file mode 100644 index 000000000000..fa1f2323af57 --- /dev/null +++ b/services/print/java/com/android/server/print/RemotePrintServiceRecommendationService.java @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2016 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.server.print; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.content.pm.ApplicationInfo; +import android.content.pm.ResolveInfo; +import android.os.IBinder; +import android.os.RemoteException; +import android.os.UserHandle; +import android.printservice.recommendation.IRecommendationService; +import android.printservice.recommendation.IRecommendationServiceCallbacks; +import android.printservice.recommendation.RecommendationInfo; +import android.util.Log; +import com.android.internal.annotations.GuardedBy; +import com.android.internal.util.Preconditions; + +import java.util.List; + +import static android.content.pm.PackageManager.GET_META_DATA; +import static android.content.pm.PackageManager.GET_SERVICES; +import static android.content.pm.PackageManager.MATCH_DEBUG_TRIAGED_MISSING; + +/** + * Connection to a remote print service recommendation service. + */ +class RemotePrintServiceRecommendationService { + private static final String LOG_TAG = "RemotePrintServiceRecS"; + + /** Lock for this object */ + private final Object mLock = new Object(); + + /** Context used for the connection */ + private @NonNull final Context mContext; + + /** The connection to the service (if {@link #mIsBound bound}) */ + @GuardedBy("mLock") + private @NonNull final Connection mConnection; + + /** If the service is currently bound. */ + @GuardedBy("mLock") + private boolean mIsBound; + + /** The service once bound */ + @GuardedBy("mLock") + private IRecommendationService mService; + + /** + * Callbacks to be called when there are updates to the print service recommendations. + */ + public interface RemotePrintServiceRecommendationServiceCallbacks { + /** + * Called when there is an update list of print service recommendations. + * + * @param recommendations The new recommendations. + */ + void onPrintServiceRecommendationsUpdated( + @Nullable List<RecommendationInfo> recommendations); + } + + /** + * @return The intent that is used to connect to the print service recommendation service. + */ + private Intent getServiceIntent(@NonNull UserHandle userHandle) throws Exception { + List<ResolveInfo> installedServices = mContext.getPackageManager() + .queryIntentServicesAsUser(new Intent( + android.printservice.recommendation.RecommendationService.SERVICE_INTERFACE), + GET_SERVICES | GET_META_DATA | MATCH_DEBUG_TRIAGED_MISSING, + userHandle.getIdentifier()); + + if (installedServices.size() != 1) { + throw new Exception(installedServices.size() + " instead of exactly one service found"); + } + + ResolveInfo installedService = installedServices.get(0); + + ComponentName serviceName = new ComponentName( + installedService.serviceInfo.packageName, + installedService.serviceInfo.name); + + ApplicationInfo appInfo = mContext.getPackageManager() + .getApplicationInfo(installedService.serviceInfo.packageName, 0); + + if (appInfo == null) { + throw new Exception("Cannot read appInfo for service"); + } + + if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { + throw new Exception("Service is not part of the system"); + } + + if (!android.Manifest.permission.BIND_PRINT_RECOMMENDATION_SERVICE.equals( + installedService.serviceInfo.permission)) { + throw new Exception("Service " + serviceName.flattenToShortString() + + " does not require permission " + + android.Manifest.permission.BIND_PRINT_RECOMMENDATION_SERVICE); + } + + Intent serviceIntent = new Intent(); + serviceIntent.setComponent(serviceName); + + return serviceIntent; + } + + /** + * Open a new connection to a {@link IRecommendationService remote print service + * recommendation service}. + * + * @param context The context establishing the connection + * @param userHandle The user the connection is for + * @param callbacks The callbacks to call by the service + */ + RemotePrintServiceRecommendationService(@NonNull Context context, + @NonNull UserHandle userHandle, + @NonNull RemotePrintServiceRecommendationServiceCallbacks callbacks) { + mContext = context; + mConnection = new Connection(callbacks); + + try { + Intent serviceIntent = getServiceIntent(userHandle); + + synchronized (mLock) { + mIsBound = mContext.bindServiceAsUser(serviceIntent, mConnection, + Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, userHandle); + + if (!mIsBound) { + throw new Exception("Failed to bind to service " + serviceIntent); + } + } + } catch (Exception e) { + Log.e(LOG_TAG, "Could not connect to print service recommendation service", e); + } + } + + /** + * Terminate the connection to the {@link IRecommendationService remote print + * service recommendation service}. + */ + void close() { + synchronized (mLock) { + if (mService != null) { + try { + mService.registerCallbacks(null); + } catch (RemoteException e) { + Log.e(LOG_TAG, "Could not unregister callbacks", e); + } + + mService = null; + } + + if (mIsBound) { + mContext.unbindService(mConnection); + mIsBound = false; + } + } + } + + @Override + protected void finalize() throws Throwable { + if (mIsBound || mService != null) { + Log.w(LOG_TAG, "Service still connected on finalize()"); + close(); + } + + super.finalize(); + } + + /** + * Connection to the service. + */ + private class Connection implements ServiceConnection { + private final RemotePrintServiceRecommendationServiceCallbacks mCallbacks; + + public Connection(@NonNull RemotePrintServiceRecommendationServiceCallbacks callbacks) { + mCallbacks = callbacks; + } + + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + synchronized (mLock) { + mService = (IRecommendationService)IRecommendationService.Stub.asInterface(service); + + try { + mService.registerCallbacks(new IRecommendationServiceCallbacks.Stub() { + @Override + public void onRecommendationsUpdated( + List<RecommendationInfo> recommendations) { + synchronized (mLock) { + if (mIsBound && mService != null) { + if (recommendations != null) { + Preconditions.checkCollectionElementsNotNull( + recommendations, "recommendation"); + } + + mCallbacks.onPrintServiceRecommendationsUpdated( + recommendations); + } + } + } + }); + } catch (RemoteException e) { + Log.e(LOG_TAG, "Could not register callbacks", e); + } + } + } + + @Override + public void onServiceDisconnected(ComponentName name) { + Log.w(LOG_TAG, "Unexpected termination of connection"); + + synchronized (mLock) { + mService = null; + } + } + } +} diff --git a/services/print/java/com/android/server/print/UserState.java b/services/print/java/com/android/server/print/UserState.java index 263dead9cf1e..026942e11e40 100644 --- a/services/print/java/com/android/server/print/UserState.java +++ b/services/print/java/com/android/server/print/UserState.java @@ -37,6 +37,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.IBinder.DeathRecipient; +import android.os.IInterface; import android.os.Looper; import android.os.Message; import android.os.RemoteCallbackList; @@ -44,12 +45,14 @@ import android.os.RemoteException; import android.os.UserHandle; import android.print.IPrintDocumentAdapter; import android.print.IPrintJobStateChangeListener; +import android.printservice.recommendation.IRecommendationsChangeListener; import android.print.IPrintServicesChangeListener; import android.print.IPrinterDiscoveryObserver; import android.print.PrintAttributes; import android.print.PrintJobId; import android.print.PrintJobInfo; import android.print.PrintManager; +import android.printservice.recommendation.RecommendationInfo; import android.print.PrinterId; import android.print.PrinterInfo; import android.printservice.PrintServiceInfo; @@ -68,6 +71,7 @@ import com.android.internal.os.BackgroundThread; import com.android.internal.os.SomeArgs; import com.android.server.print.RemotePrintService.PrintServiceCallbacks; import com.android.server.print.RemotePrintSpooler.PrintSpoolerCallbacks; +import com.android.server.print.RemotePrintServiceRecommendationService.RemotePrintServiceRecommendationServiceCallbacks; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -82,7 +86,8 @@ import java.util.Set; /** * Represents the print state for a user. */ -final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { +final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks, + RemotePrintServiceRecommendationServiceCallbacks { private static final String LOG_TAG = "UserState"; @@ -122,10 +127,22 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { private List<PrintJobStateChangeListenerRecord> mPrintJobStateChangeListenerRecords; - private List<PrintServicesChangeListenerRecord> mPrintServicesChangeListenerRecords; + private List<ListenerRecord<IPrintServicesChangeListener>> mPrintServicesChangeListenerRecords; + + private List<ListenerRecord<IRecommendationsChangeListener>> + mPrintServiceRecommendationsChangeListenerRecords; private boolean mDestroyed; + /** Currently known list of print service recommendations */ + private List<RecommendationInfo> mPrintServiceRecommendations; + + /** + * Connection to the service updating the {@link #mPrintServiceRecommendations print service + * recommendations}. + */ + private RemotePrintServiceRecommendationService mPrintServiceRecommendationsService; + public UserState(Context context, int userId, Object lock, boolean lowPriority) { mContext = context; mUserId = userId; @@ -409,6 +426,13 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } } + /** + * @return The currently known print service recommendations + */ + public @Nullable List<RecommendationInfo> getPrintServiceRecommendations() { + return mPrintServiceRecommendations; + } + public void createPrinterDiscoverySession(@NonNull IPrinterDiscoveryObserver observer) { synchronized (mLock) { throwIfDestroyedLocked(); @@ -566,7 +590,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { mPrintServicesChangeListenerRecords = new ArrayList<>(); } mPrintServicesChangeListenerRecords.add( - new PrintServicesChangeListenerRecord(listener) { + new ListenerRecord<IPrintServicesChangeListener>(listener) { @Override public void onBinderDied() { mPrintServicesChangeListenerRecords.remove(this); @@ -583,7 +607,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } final int recordCount = mPrintServicesChangeListenerRecords.size(); for (int i = 0; i < recordCount; i++) { - PrintServicesChangeListenerRecord record = + ListenerRecord<IPrintServicesChangeListener> record = mPrintServicesChangeListenerRecords.get(i); if (record.listener.asBinder().equals(listener.asBinder())) { mPrintServicesChangeListenerRecords.remove(i); @@ -596,6 +620,54 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } } + public void addPrintServiceRecommendationsChangeListener( + @NonNull IRecommendationsChangeListener listener) throws RemoteException { + synchronized (mLock) { + throwIfDestroyedLocked(); + if (mPrintServiceRecommendationsChangeListenerRecords == null) { + mPrintServiceRecommendationsChangeListenerRecords = new ArrayList<>(); + + mPrintServiceRecommendationsService = + new RemotePrintServiceRecommendationService(mContext, + UserHandle.getUserHandleForUid(mUserId), this); + } + mPrintServiceRecommendationsChangeListenerRecords.add( + new ListenerRecord<IRecommendationsChangeListener>(listener) { + @Override + public void onBinderDied() { + mPrintServiceRecommendationsChangeListenerRecords.remove(this); + } + }); + } + } + + public void removePrintServiceRecommendationsChangeListener( + @NonNull IRecommendationsChangeListener listener) { + synchronized (mLock) { + throwIfDestroyedLocked(); + if (mPrintServiceRecommendationsChangeListenerRecords == null) { + return; + } + final int recordCount = mPrintServiceRecommendationsChangeListenerRecords.size(); + for (int i = 0; i < recordCount; i++) { + ListenerRecord<IRecommendationsChangeListener> record = + mPrintServiceRecommendationsChangeListenerRecords.get(i); + if (record.listener.asBinder().equals(listener.asBinder())) { + mPrintServiceRecommendationsChangeListenerRecords.remove(i); + break; + } + } + if (mPrintServiceRecommendationsChangeListenerRecords.isEmpty()) { + mPrintServiceRecommendationsChangeListenerRecords = null; + + mPrintServiceRecommendations = null; + + mPrintServiceRecommendationsService.close(); + mPrintServiceRecommendationsService = null; + } + } + } + @Override public void onPrintJobStateChanged(PrintJobInfo printJob) { mPrintJobForAppCache.onPrintJobStateChanged(printJob); @@ -608,6 +680,12 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } @Override + public void onPrintServiceRecommendationsUpdated(List<RecommendationInfo> recommendations) { + mHandler.obtainMessage(UserStateHandler.MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED, + 0, 0, recommendations).sendToTarget(); + } + + @Override public void onPrintersAdded(List<PrinterInfo> printers) { synchronized (mLock) { throwIfDestroyedLocked(); @@ -1058,7 +1136,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } private void handleDispatchPrintServicesChanged() { - final List<PrintServicesChangeListenerRecord> records; + final List<ListenerRecord<IPrintServicesChangeListener>> records; synchronized (mLock) { if (mPrintServicesChangeListenerRecords == null) { return; @@ -1067,7 +1145,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } final int recordCount = records.size(); for (int i = 0; i < recordCount; i++) { - PrintServicesChangeListenerRecord record = records.get(i); + ListenerRecord<IPrintServicesChangeListener> record = records.get(i); try { record.listener.onPrintServicesChanged();; @@ -1077,9 +1155,33 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } } + private void handleDispatchPrintServiceRecommendationsUpdated( + @Nullable List<RecommendationInfo> recommendations) { + final List<ListenerRecord<IRecommendationsChangeListener>> records; + synchronized (mLock) { + if (mPrintServiceRecommendationsChangeListenerRecords == null) { + return; + } + records = new ArrayList<>(mPrintServiceRecommendationsChangeListenerRecords); + + mPrintServiceRecommendations = recommendations; + } + final int recordCount = records.size(); + for (int i = 0; i < recordCount; i++) { + ListenerRecord<IRecommendationsChangeListener> record = records.get(i); + + try { + record.listener.onRecommendationsChanged(); + } catch (RemoteException re) { + Log.e(LOG_TAG, "Error notifying for print service recommendations change", re); + } + } + } + private final class UserStateHandler extends Handler { public static final int MSG_DISPATCH_PRINT_JOB_STATE_CHANGED = 1; public static final int MSG_DISPATCH_PRINT_SERVICES_CHANGED = 2; + public static final int MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED = 3; public UserStateHandler(Looper looper) { super(looper, null, false); @@ -1096,6 +1198,10 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { case MSG_DISPATCH_PRINT_SERVICES_CHANGED: handleDispatchPrintServicesChanged(); break; + case MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED: + handleDispatchPrintServiceRecommendationsUpdated( + (List<RecommendationInfo>) message.obj); + break; default: // not reached } @@ -1122,10 +1228,10 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { public abstract void onBinderDied(); } - private abstract class PrintServicesChangeListenerRecord implements DeathRecipient { - @NonNull final IPrintServicesChangeListener listener; + private abstract class ListenerRecord<T extends IInterface> implements DeathRecipient { + @NonNull final T listener; - public PrintServicesChangeListenerRecord(@NonNull IPrintServicesChangeListener listener) throws RemoteException { + public ListenerRecord(@NonNull T listener) throws RemoteException { this.listener = listener; listener.asBinder().linkToDeath(this, 0); } |