From b7da1f5f9886ff8a4ad81b0d617d442c461ca1e5 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Wed, 19 Aug 2015 11:44:43 -0700 Subject: Implement base version of GrantPermissions dialog With the Emerald release, we need to support the new permission APIs. This changelist adds a dialog for the Activity.requestPermissions() API so that users can request permissions. This check in is a functional version, but not polished. will need to be a follow up CL to add the correct animations and update the UI to the appropriate redlines. The implementation for the confirmation dialog is modeled after the one in the clockwork libs/Views folder. There are some tweaks to match the designed behavior of the permission dialog. When there's more time in the future, we should try to condense this to one implementation. BUG: 23118402 Change-Id: Ic90d37a2ce8a7adacb7c4e004b0a5260b624f5c8 --- .../permission/ui/GrantPermissionsActivity.java | 7 + .../ui/GrantPermissionsWatchViewHandler.java | 159 +++++++++++++++++++++ .../ui/PermissionConfirmationViewHandler.java | 149 +++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java create mode 100644 src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java index c451dd50..c55267e0 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java @@ -73,6 +73,8 @@ public class GrantPermissionsActivity extends OverlayTouchActivity if (Utils.isTelevision(this)) { mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this); + } else if (isWatch()) { + mViewHandler = new GrantPermissionsWatchViewHandler(this).setResultListener(this); } else { mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this); } @@ -357,6 +359,11 @@ public class GrantPermissionsActivity extends OverlayTouchActivity SafetyNetLogger.logPermissionsRequested(mAppPermissions.getPackageInfo(), groups); } + private boolean isWatch() { + PackageManager pm = getPackageManager(); + return pm.hasSystemFeature(pm.FEATURE_WATCH); + } + private static final class GroupState { static final int STATE_UNKNOWN = 0; static final int STATE_ALLOWED = 1; diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java new file mode 100644 index 00000000..ac573c43 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java @@ -0,0 +1,159 @@ +package com.android.packageinstaller.permission.ui; + +import android.content.Context; +import android.graphics.PixelFormat; +import android.graphics.drawable.Icon; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.WindowManager; + +import com.android.packageinstaller.R; + +/** + * Watch-specific view handler for the grant permissions activity. + */ +final class GrantPermissionsWatchViewHandler extends PermissionConfirmationViewHandler + implements GrantPermissionsViewHandler { + private static final String TAG = "GrantPermissionsViewH"; + + private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; + + private final Context mContext; + + private ResultListener mResultListener; + + private String mGroupName; + private boolean mShowDoNotAsk; + + private CharSequence mMessage; + private String mCurrentPageText; + private Icon mIcon; + + GrantPermissionsWatchViewHandler(Context context) { + super(context); + mContext = context; + } + + @Override + public GrantPermissionsWatchViewHandler setResultListener(ResultListener listener) { + mResultListener = listener; + return this; + } + + @Override + public View createView() { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "createView()"); + } + + mShowDoNotAsk = false; + + return super.createView(); + } + + @Override + public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { + outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; + outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; + outLayoutParams.format = PixelFormat.OPAQUE; + outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; + outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + } + + @Override + public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, + CharSequence message, boolean showDoNotAsk) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "updateUi() - groupName: " + groupName + + ", groupCount: " + groupCount + + ", groupIndex: " + groupIndex + + ", icon: " + icon + + ", message: " + message + + ", showDoNotAsk: " + showDoNotAsk); + } + + mGroupName = groupName; + mShowDoNotAsk = showDoNotAsk; + mMessage = message; + mIcon = icon; + mCurrentPageText = (groupCount > 1 ? + mContext.getString(R.string.current_permission_template, groupIndex + 1, groupCount) + : null); + + invalidate(); + } + + @Override + public void saveInstanceState(Bundle outState) { + outState.putString(ARG_GROUP_NAME, mGroupName); + } + + @Override + public void loadInstanceState(Bundle savedInstanceState) { + mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); + } + + @Override + public void onBackPressed() { + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, false, false); + } + } + + @Override // PermissionConfirmationViewHandler + public void onAllow() { + onClick(true /* granted */, false /* doNotAskAgain */); + } + + @Override // PermissionConfirmationViewHandler + public void onDeny() { + onClick(false /* granted */, false /* doNotAskAgain */); + } + + @Override // PermissionConfirmationViewHandler + public void onDenyDoNotAskAgain() { + onClick(false /* granted */, true /* doNotAskAgain */); + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getCurrentPageText() { + return mCurrentPageText; + } + + @Override // PermissionConfirmationViewHandler + public Icon getPermissionIcon() { + return mIcon; + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getMessage() { + return mMessage; + } + + @Override // PermissionConfirmationViewHandler + public int getButtonBarMode() { + return mShowDoNotAsk ? MODE_VERTICAL_BUTTONS : MODE_HORIZONTAL_BUTTONS; + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getVerticalAllowText() { + return mContext.getString(R.string.grant_dialog_button_allow); + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getVerticalDenyText() { + return mContext.getString(R.string.grant_dialog_button_deny); + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getVerticalDenyDoNotAskAgainText() { + return mContext.getString(R.string.grant_dialog_button_deny_dont_ask_again); + } + + private void onClick(boolean granted, boolean doNotAskAgain) { + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java new file mode 100644 index 00000000..63ed0a45 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java @@ -0,0 +1,149 @@ +package com.android.packageinstaller.permission.ui; + +import android.content.Context; +import android.graphics.drawable.Icon; +import android.os.Handler; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ScrollView; +import android.widget.TextView; + +import com.android.packageinstaller.R; + +public abstract class PermissionConfirmationViewHandler implements + View.OnClickListener { + public static final int MODE_HORIZONTAL_BUTTONS = 0; + public static final int MODE_VERTICAL_BUTTONS = 1; + + private View mRoot; + private TextView mCurrentPageText; + private ImageView mIcon; + private TextView mMessage; + private ScrollView mScrollingContainer; + private ViewGroup mContent; + private ViewGroup mHorizontalButtonBar; + private ViewGroup mVerticalButtonBar; + private Button mVerticalAllow; + private Button mVerticalDeny; + private Button mVerticalDenyDoNotAskAgain; + private View mButtonBarContainer; + + private Context mContext; + + // TODO: Move these into a builder + public abstract void onAllow(); + public abstract void onDeny(); + public abstract void onDenyDoNotAskAgain(); + public abstract CharSequence getVerticalAllowText(); + public abstract CharSequence getVerticalDenyText(); + public abstract CharSequence getVerticalDenyDoNotAskAgainText(); + public abstract CharSequence getCurrentPageText(); + public abstract Icon getPermissionIcon(); + public abstract CharSequence getMessage(); + + public PermissionConfirmationViewHandler(Context context) { + mContext = context; + } + + public View createView() { + mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null); + + mMessage = (TextView) mRoot.findViewById(R.id.message); + mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text); + mIcon = (ImageView) mRoot.findViewById(R.id.icon); + mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container); + mContent = (ViewGroup) mRoot.findViewById(R.id.content); + mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container); + mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar); + mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar); + + Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button); + Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button); + horizontalAllow.setOnClickListener(this); + horizontalDeny.setOnClickListener(this); + + mVerticalAllow = (Button) mRoot.findViewById(R.id.vertical_allow_button); + mVerticalDeny = (Button) mRoot.findViewById(R.id.vertical_deny_button); + mVerticalDenyDoNotAskAgain = + (Button) mRoot.findViewById(R.id.vertical_deny_do_not_ask_again_button); + mVerticalAllow.setOnClickListener(this); + mVerticalDeny.setOnClickListener(this); + mVerticalDenyDoNotAskAgain.setOnClickListener(this); + + return mRoot; + } + + /** + * Child class should override this for other modes. Call invalidate() to update the UI to the + * new button mode. + * @return The current mode the layout should use for the buttons + */ + public int getButtonBarMode() { + return MODE_HORIZONTAL_BUTTONS; + } + + public void invalidate() { + CharSequence currentPageText = getCurrentPageText(); + if (!TextUtils.isEmpty(currentPageText)) { + mCurrentPageText.setText(currentPageText); + mCurrentPageText.setVisibility(View.VISIBLE); + } else { + mCurrentPageText.setVisibility(View.INVISIBLE); + } + + Icon icon = getPermissionIcon(); + if (icon != null) { + mIcon.setImageIcon(icon); + mIcon.setVisibility(View.VISIBLE); + } else { + mIcon.setVisibility(View.INVISIBLE); + } + + mMessage.setText(getMessage()); + + switch (getButtonBarMode()) { + case MODE_HORIZONTAL_BUTTONS: + mHorizontalButtonBar.setVisibility(View.VISIBLE); + mVerticalButtonBar.setVisibility(View.GONE); + break; + case MODE_VERTICAL_BUTTONS: + mHorizontalButtonBar.setVisibility(View.GONE); + mVerticalButtonBar.setVisibility(View.VISIBLE); + mVerticalAllow.setText(getVerticalAllowText()); + mVerticalDeny.setText(getVerticalDenyText()); + mVerticalDenyDoNotAskAgain.setText(getVerticalDenyDoNotAskAgainText()); + + mVerticalAllow.setCompoundDrawablesWithIntrinsicBounds( + mContext.getDrawable(R.drawable.confirm_button), null, null, null); + mVerticalDeny.setCompoundDrawablesWithIntrinsicBounds( + mContext.getDrawable(R.drawable.cancel_button), null, null, null); + mVerticalDenyDoNotAskAgain.setCompoundDrawablesWithIntrinsicBounds( + mContext.getDrawable(R.drawable.cancel_button), null, null, null); + break; + } + + mScrollingContainer.scrollTo(0, 0); + } + + @Override + public void onClick(View v) { + int id = v.getId(); + switch (id) { + case R.id.horizontal_allow_button: + case R.id.vertical_allow_button: + onAllow(); + break; + case R.id.horizontal_deny_button: + case R.id.vertical_deny_button: + onDeny(); + break; + case R.id.vertical_deny_do_not_ask_again_button: + onDenyDoNotAskAgain(); + break; + } + } +} -- cgit v1.2.3 From 70d651e904e16723f4e55879f2e356e403fff61a Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Fri, 21 Aug 2015 20:57:03 +0000 Subject: Revert "Implement base version of GrantPermissions dialog" This reverts commit b7da1f5f9886ff8a4ad81b0d617d442c461ca1e5. We can't reference vendor from this package. Need to add the references manually. Change-Id: I3ee31de110f24014100ce804b0659055b26f963b --- .../permission/ui/GrantPermissionsActivity.java | 7 - .../ui/GrantPermissionsWatchViewHandler.java | 159 --------------------- .../ui/PermissionConfirmationViewHandler.java | 149 ------------------- 3 files changed, 315 deletions(-) delete mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java delete mode 100644 src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java index c55267e0..c451dd50 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java @@ -73,8 +73,6 @@ public class GrantPermissionsActivity extends OverlayTouchActivity if (Utils.isTelevision(this)) { mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this); - } else if (isWatch()) { - mViewHandler = new GrantPermissionsWatchViewHandler(this).setResultListener(this); } else { mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this); } @@ -359,11 +357,6 @@ public class GrantPermissionsActivity extends OverlayTouchActivity SafetyNetLogger.logPermissionsRequested(mAppPermissions.getPackageInfo(), groups); } - private boolean isWatch() { - PackageManager pm = getPackageManager(); - return pm.hasSystemFeature(pm.FEATURE_WATCH); - } - private static final class GroupState { static final int STATE_UNKNOWN = 0; static final int STATE_ALLOWED = 1; diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java deleted file mode 100644 index ac573c43..00000000 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java +++ /dev/null @@ -1,159 +0,0 @@ -package com.android.packageinstaller.permission.ui; - -import android.content.Context; -import android.graphics.PixelFormat; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.view.WindowManager; - -import com.android.packageinstaller.R; - -/** - * Watch-specific view handler for the grant permissions activity. - */ -final class GrantPermissionsWatchViewHandler extends PermissionConfirmationViewHandler - implements GrantPermissionsViewHandler { - private static final String TAG = "GrantPermissionsViewH"; - - private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; - - private final Context mContext; - - private ResultListener mResultListener; - - private String mGroupName; - private boolean mShowDoNotAsk; - - private CharSequence mMessage; - private String mCurrentPageText; - private Icon mIcon; - - GrantPermissionsWatchViewHandler(Context context) { - super(context); - mContext = context; - } - - @Override - public GrantPermissionsWatchViewHandler setResultListener(ResultListener listener) { - mResultListener = listener; - return this; - } - - @Override - public View createView() { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "createView()"); - } - - mShowDoNotAsk = false; - - return super.createView(); - } - - @Override - public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { - outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; - outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; - outLayoutParams.format = PixelFormat.OPAQUE; - outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; - outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; - } - - @Override - public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, - CharSequence message, boolean showDoNotAsk) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "updateUi() - groupName: " + groupName - + ", groupCount: " + groupCount - + ", groupIndex: " + groupIndex - + ", icon: " + icon - + ", message: " + message - + ", showDoNotAsk: " + showDoNotAsk); - } - - mGroupName = groupName; - mShowDoNotAsk = showDoNotAsk; - mMessage = message; - mIcon = icon; - mCurrentPageText = (groupCount > 1 ? - mContext.getString(R.string.current_permission_template, groupIndex + 1, groupCount) - : null); - - invalidate(); - } - - @Override - public void saveInstanceState(Bundle outState) { - outState.putString(ARG_GROUP_NAME, mGroupName); - } - - @Override - public void loadInstanceState(Bundle savedInstanceState) { - mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); - } - - @Override - public void onBackPressed() { - if (mResultListener != null) { - mResultListener.onPermissionGrantResult(mGroupName, false, false); - } - } - - @Override // PermissionConfirmationViewHandler - public void onAllow() { - onClick(true /* granted */, false /* doNotAskAgain */); - } - - @Override // PermissionConfirmationViewHandler - public void onDeny() { - onClick(false /* granted */, false /* doNotAskAgain */); - } - - @Override // PermissionConfirmationViewHandler - public void onDenyDoNotAskAgain() { - onClick(false /* granted */, true /* doNotAskAgain */); - } - - @Override // PermissionConfirmationViewHandler - public CharSequence getCurrentPageText() { - return mCurrentPageText; - } - - @Override // PermissionConfirmationViewHandler - public Icon getPermissionIcon() { - return mIcon; - } - - @Override // PermissionConfirmationViewHandler - public CharSequence getMessage() { - return mMessage; - } - - @Override // PermissionConfirmationViewHandler - public int getButtonBarMode() { - return mShowDoNotAsk ? MODE_VERTICAL_BUTTONS : MODE_HORIZONTAL_BUTTONS; - } - - @Override // PermissionConfirmationViewHandler - public CharSequence getVerticalAllowText() { - return mContext.getString(R.string.grant_dialog_button_allow); - } - - @Override // PermissionConfirmationViewHandler - public CharSequence getVerticalDenyText() { - return mContext.getString(R.string.grant_dialog_button_deny); - } - - @Override // PermissionConfirmationViewHandler - public CharSequence getVerticalDenyDoNotAskAgainText() { - return mContext.getString(R.string.grant_dialog_button_deny_dont_ask_again); - } - - private void onClick(boolean granted, boolean doNotAskAgain) { - if (mResultListener != null) { - mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java deleted file mode 100644 index 63ed0a45..00000000 --- a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.android.packageinstaller.permission.ui; - -import android.content.Context; -import android.graphics.drawable.Icon; -import android.os.Handler; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.ScrollView; -import android.widget.TextView; - -import com.android.packageinstaller.R; - -public abstract class PermissionConfirmationViewHandler implements - View.OnClickListener { - public static final int MODE_HORIZONTAL_BUTTONS = 0; - public static final int MODE_VERTICAL_BUTTONS = 1; - - private View mRoot; - private TextView mCurrentPageText; - private ImageView mIcon; - private TextView mMessage; - private ScrollView mScrollingContainer; - private ViewGroup mContent; - private ViewGroup mHorizontalButtonBar; - private ViewGroup mVerticalButtonBar; - private Button mVerticalAllow; - private Button mVerticalDeny; - private Button mVerticalDenyDoNotAskAgain; - private View mButtonBarContainer; - - private Context mContext; - - // TODO: Move these into a builder - public abstract void onAllow(); - public abstract void onDeny(); - public abstract void onDenyDoNotAskAgain(); - public abstract CharSequence getVerticalAllowText(); - public abstract CharSequence getVerticalDenyText(); - public abstract CharSequence getVerticalDenyDoNotAskAgainText(); - public abstract CharSequence getCurrentPageText(); - public abstract Icon getPermissionIcon(); - public abstract CharSequence getMessage(); - - public PermissionConfirmationViewHandler(Context context) { - mContext = context; - } - - public View createView() { - mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null); - - mMessage = (TextView) mRoot.findViewById(R.id.message); - mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text); - mIcon = (ImageView) mRoot.findViewById(R.id.icon); - mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container); - mContent = (ViewGroup) mRoot.findViewById(R.id.content); - mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container); - mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar); - mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar); - - Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button); - Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button); - horizontalAllow.setOnClickListener(this); - horizontalDeny.setOnClickListener(this); - - mVerticalAllow = (Button) mRoot.findViewById(R.id.vertical_allow_button); - mVerticalDeny = (Button) mRoot.findViewById(R.id.vertical_deny_button); - mVerticalDenyDoNotAskAgain = - (Button) mRoot.findViewById(R.id.vertical_deny_do_not_ask_again_button); - mVerticalAllow.setOnClickListener(this); - mVerticalDeny.setOnClickListener(this); - mVerticalDenyDoNotAskAgain.setOnClickListener(this); - - return mRoot; - } - - /** - * Child class should override this for other modes. Call invalidate() to update the UI to the - * new button mode. - * @return The current mode the layout should use for the buttons - */ - public int getButtonBarMode() { - return MODE_HORIZONTAL_BUTTONS; - } - - public void invalidate() { - CharSequence currentPageText = getCurrentPageText(); - if (!TextUtils.isEmpty(currentPageText)) { - mCurrentPageText.setText(currentPageText); - mCurrentPageText.setVisibility(View.VISIBLE); - } else { - mCurrentPageText.setVisibility(View.INVISIBLE); - } - - Icon icon = getPermissionIcon(); - if (icon != null) { - mIcon.setImageIcon(icon); - mIcon.setVisibility(View.VISIBLE); - } else { - mIcon.setVisibility(View.INVISIBLE); - } - - mMessage.setText(getMessage()); - - switch (getButtonBarMode()) { - case MODE_HORIZONTAL_BUTTONS: - mHorizontalButtonBar.setVisibility(View.VISIBLE); - mVerticalButtonBar.setVisibility(View.GONE); - break; - case MODE_VERTICAL_BUTTONS: - mHorizontalButtonBar.setVisibility(View.GONE); - mVerticalButtonBar.setVisibility(View.VISIBLE); - mVerticalAllow.setText(getVerticalAllowText()); - mVerticalDeny.setText(getVerticalDenyText()); - mVerticalDenyDoNotAskAgain.setText(getVerticalDenyDoNotAskAgainText()); - - mVerticalAllow.setCompoundDrawablesWithIntrinsicBounds( - mContext.getDrawable(R.drawable.confirm_button), null, null, null); - mVerticalDeny.setCompoundDrawablesWithIntrinsicBounds( - mContext.getDrawable(R.drawable.cancel_button), null, null, null); - mVerticalDenyDoNotAskAgain.setCompoundDrawablesWithIntrinsicBounds( - mContext.getDrawable(R.drawable.cancel_button), null, null, null); - break; - } - - mScrollingContainer.scrollTo(0, 0); - } - - @Override - public void onClick(View v) { - int id = v.getId(); - switch (id) { - case R.id.horizontal_allow_button: - case R.id.vertical_allow_button: - onAllow(); - break; - case R.id.horizontal_deny_button: - case R.id.vertical_deny_button: - onDeny(); - break; - case R.id.vertical_deny_do_not_ask_again_button: - onDenyDoNotAskAgain(); - break; - } - } -} -- cgit v1.2.3 From 9478120abc48a8934e5de14f7cf49342cef5485c Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Wed, 19 Aug 2015 11:44:43 -0700 Subject: Implement base version of GrantPermissions dialog With the Emerald release, we need to support the new permission APIs. This changelist adds a dialog for the Activity.requestPermissions() API so that users can request permissions. This check in is a functional version, but not polished. will need to be a follow up CL to add the correct animations and update the UI to the appropriate redlines. The implementation for the confirmation dialog is modeled after the one in the clockwork libs/Views folder. There are some tweaks to match the designed behavior of the permission dialog. When there's more time in the future, we should try to condense this to one implementation. This is a resubmit without the wearable-support lib. I will figure out how to get that referenced in the next UI pass. BUG: 23118402 Change-Id: Ib2fb94b356aa965b999b3e12726fda86928a963e --- .../permission/ui/GrantPermissionsActivity.java | 7 + .../ui/GrantPermissionsWatchViewHandler.java | 159 +++++++++++++++++++++ .../ui/PermissionConfirmationViewHandler.java | 149 +++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java create mode 100644 src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java index 0c087350..c1597f16 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java @@ -73,6 +73,8 @@ public class GrantPermissionsActivity extends OverlayTouchActivity if (Utils.isTelevision(this)) { mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this); + } else if (isWatch()) { + mViewHandler = new GrantPermissionsWatchViewHandler(this).setResultListener(this); } else { mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this); } @@ -368,6 +370,11 @@ public class GrantPermissionsActivity extends OverlayTouchActivity SafetyNetLogger.logPermissionsRequested(mAppPermissions.getPackageInfo(), groups); } + private boolean isWatch() { + PackageManager pm = getPackageManager(); + return pm.hasSystemFeature(pm.FEATURE_WATCH); + } + private static final class GroupState { static final int STATE_UNKNOWN = 0; static final int STATE_ALLOWED = 1; diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java new file mode 100644 index 00000000..ac573c43 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java @@ -0,0 +1,159 @@ +package com.android.packageinstaller.permission.ui; + +import android.content.Context; +import android.graphics.PixelFormat; +import android.graphics.drawable.Icon; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.WindowManager; + +import com.android.packageinstaller.R; + +/** + * Watch-specific view handler for the grant permissions activity. + */ +final class GrantPermissionsWatchViewHandler extends PermissionConfirmationViewHandler + implements GrantPermissionsViewHandler { + private static final String TAG = "GrantPermissionsViewH"; + + private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; + + private final Context mContext; + + private ResultListener mResultListener; + + private String mGroupName; + private boolean mShowDoNotAsk; + + private CharSequence mMessage; + private String mCurrentPageText; + private Icon mIcon; + + GrantPermissionsWatchViewHandler(Context context) { + super(context); + mContext = context; + } + + @Override + public GrantPermissionsWatchViewHandler setResultListener(ResultListener listener) { + mResultListener = listener; + return this; + } + + @Override + public View createView() { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "createView()"); + } + + mShowDoNotAsk = false; + + return super.createView(); + } + + @Override + public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { + outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; + outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; + outLayoutParams.format = PixelFormat.OPAQUE; + outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; + outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + } + + @Override + public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, + CharSequence message, boolean showDoNotAsk) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "updateUi() - groupName: " + groupName + + ", groupCount: " + groupCount + + ", groupIndex: " + groupIndex + + ", icon: " + icon + + ", message: " + message + + ", showDoNotAsk: " + showDoNotAsk); + } + + mGroupName = groupName; + mShowDoNotAsk = showDoNotAsk; + mMessage = message; + mIcon = icon; + mCurrentPageText = (groupCount > 1 ? + mContext.getString(R.string.current_permission_template, groupIndex + 1, groupCount) + : null); + + invalidate(); + } + + @Override + public void saveInstanceState(Bundle outState) { + outState.putString(ARG_GROUP_NAME, mGroupName); + } + + @Override + public void loadInstanceState(Bundle savedInstanceState) { + mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); + } + + @Override + public void onBackPressed() { + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, false, false); + } + } + + @Override // PermissionConfirmationViewHandler + public void onAllow() { + onClick(true /* granted */, false /* doNotAskAgain */); + } + + @Override // PermissionConfirmationViewHandler + public void onDeny() { + onClick(false /* granted */, false /* doNotAskAgain */); + } + + @Override // PermissionConfirmationViewHandler + public void onDenyDoNotAskAgain() { + onClick(false /* granted */, true /* doNotAskAgain */); + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getCurrentPageText() { + return mCurrentPageText; + } + + @Override // PermissionConfirmationViewHandler + public Icon getPermissionIcon() { + return mIcon; + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getMessage() { + return mMessage; + } + + @Override // PermissionConfirmationViewHandler + public int getButtonBarMode() { + return mShowDoNotAsk ? MODE_VERTICAL_BUTTONS : MODE_HORIZONTAL_BUTTONS; + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getVerticalAllowText() { + return mContext.getString(R.string.grant_dialog_button_allow); + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getVerticalDenyText() { + return mContext.getString(R.string.grant_dialog_button_deny); + } + + @Override // PermissionConfirmationViewHandler + public CharSequence getVerticalDenyDoNotAskAgainText() { + return mContext.getString(R.string.grant_dialog_button_deny_dont_ask_again); + } + + private void onClick(boolean granted, boolean doNotAskAgain) { + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java new file mode 100644 index 00000000..63ed0a45 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java @@ -0,0 +1,149 @@ +package com.android.packageinstaller.permission.ui; + +import android.content.Context; +import android.graphics.drawable.Icon; +import android.os.Handler; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ScrollView; +import android.widget.TextView; + +import com.android.packageinstaller.R; + +public abstract class PermissionConfirmationViewHandler implements + View.OnClickListener { + public static final int MODE_HORIZONTAL_BUTTONS = 0; + public static final int MODE_VERTICAL_BUTTONS = 1; + + private View mRoot; + private TextView mCurrentPageText; + private ImageView mIcon; + private TextView mMessage; + private ScrollView mScrollingContainer; + private ViewGroup mContent; + private ViewGroup mHorizontalButtonBar; + private ViewGroup mVerticalButtonBar; + private Button mVerticalAllow; + private Button mVerticalDeny; + private Button mVerticalDenyDoNotAskAgain; + private View mButtonBarContainer; + + private Context mContext; + + // TODO: Move these into a builder + public abstract void onAllow(); + public abstract void onDeny(); + public abstract void onDenyDoNotAskAgain(); + public abstract CharSequence getVerticalAllowText(); + public abstract CharSequence getVerticalDenyText(); + public abstract CharSequence getVerticalDenyDoNotAskAgainText(); + public abstract CharSequence getCurrentPageText(); + public abstract Icon getPermissionIcon(); + public abstract CharSequence getMessage(); + + public PermissionConfirmationViewHandler(Context context) { + mContext = context; + } + + public View createView() { + mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null); + + mMessage = (TextView) mRoot.findViewById(R.id.message); + mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text); + mIcon = (ImageView) mRoot.findViewById(R.id.icon); + mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container); + mContent = (ViewGroup) mRoot.findViewById(R.id.content); + mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container); + mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar); + mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar); + + Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button); + Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button); + horizontalAllow.setOnClickListener(this); + horizontalDeny.setOnClickListener(this); + + mVerticalAllow = (Button) mRoot.findViewById(R.id.vertical_allow_button); + mVerticalDeny = (Button) mRoot.findViewById(R.id.vertical_deny_button); + mVerticalDenyDoNotAskAgain = + (Button) mRoot.findViewById(R.id.vertical_deny_do_not_ask_again_button); + mVerticalAllow.setOnClickListener(this); + mVerticalDeny.setOnClickListener(this); + mVerticalDenyDoNotAskAgain.setOnClickListener(this); + + return mRoot; + } + + /** + * Child class should override this for other modes. Call invalidate() to update the UI to the + * new button mode. + * @return The current mode the layout should use for the buttons + */ + public int getButtonBarMode() { + return MODE_HORIZONTAL_BUTTONS; + } + + public void invalidate() { + CharSequence currentPageText = getCurrentPageText(); + if (!TextUtils.isEmpty(currentPageText)) { + mCurrentPageText.setText(currentPageText); + mCurrentPageText.setVisibility(View.VISIBLE); + } else { + mCurrentPageText.setVisibility(View.INVISIBLE); + } + + Icon icon = getPermissionIcon(); + if (icon != null) { + mIcon.setImageIcon(icon); + mIcon.setVisibility(View.VISIBLE); + } else { + mIcon.setVisibility(View.INVISIBLE); + } + + mMessage.setText(getMessage()); + + switch (getButtonBarMode()) { + case MODE_HORIZONTAL_BUTTONS: + mHorizontalButtonBar.setVisibility(View.VISIBLE); + mVerticalButtonBar.setVisibility(View.GONE); + break; + case MODE_VERTICAL_BUTTONS: + mHorizontalButtonBar.setVisibility(View.GONE); + mVerticalButtonBar.setVisibility(View.VISIBLE); + mVerticalAllow.setText(getVerticalAllowText()); + mVerticalDeny.setText(getVerticalDenyText()); + mVerticalDenyDoNotAskAgain.setText(getVerticalDenyDoNotAskAgainText()); + + mVerticalAllow.setCompoundDrawablesWithIntrinsicBounds( + mContext.getDrawable(R.drawable.confirm_button), null, null, null); + mVerticalDeny.setCompoundDrawablesWithIntrinsicBounds( + mContext.getDrawable(R.drawable.cancel_button), null, null, null); + mVerticalDenyDoNotAskAgain.setCompoundDrawablesWithIntrinsicBounds( + mContext.getDrawable(R.drawable.cancel_button), null, null, null); + break; + } + + mScrollingContainer.scrollTo(0, 0); + } + + @Override + public void onClick(View v) { + int id = v.getId(); + switch (id) { + case R.id.horizontal_allow_button: + case R.id.vertical_allow_button: + onAllow(); + break; + case R.id.horizontal_deny_button: + case R.id.vertical_deny_button: + onDeny(); + break; + case R.id.vertical_deny_do_not_ask_again_button: + onDenyDoNotAskAgain(); + break; + } + } +} -- cgit v1.2.3 From aa4368cafc3c92b33a99c3fbe0e6af08d7e092e4 Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Thu, 13 Aug 2015 09:50:06 -0700 Subject: Move ClockworkPackageInstaller functionality here - The code is being moved from https://cs.corp.google.com/#android/vendor/google_clockwork/packages/PackageInstaller/src/com/google/android/clockwork/packageinstaller/ Bug: 22411517 Change-Id: If6a0b7f49530176a6cff6b76ee6eff44ba822547 --- .../wear/WearPackageIconProvider.java | 202 +++++++ .../wear/WearPackageInstallerService.java | 608 +++++++++++++++++++++ .../packageinstaller/wear/WearPackageUtil.java | 154 ++++++ 3 files changed, 964 insertions(+) create mode 100644 src/com/android/packageinstaller/wear/WearPackageIconProvider.java create mode 100644 src/com/android/packageinstaller/wear/WearPackageInstallerService.java create mode 100644 src/com/android/packageinstaller/wear/WearPackageUtil.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageIconProvider.java b/src/com/android/packageinstaller/wear/WearPackageIconProvider.java new file mode 100644 index 00000000..02b9d298 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageIconProvider.java @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.annotation.TargetApi; +import android.app.ActivityManager; +import android.content.ContentProvider; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.net.Uri; +import android.os.Binder; +import android.os.Build; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.List; + +import static android.content.pm.PackageManager.PERMISSION_GRANTED; + +public class WearPackageIconProvider extends ContentProvider { + private static final String TAG = "WearPackageIconProvider"; + public static final String AUTHORITY = "com.google.android.packageinstaller.wear.provider"; + + private static final String REQUIRED_PERMISSION = + "com.google.android.permission.INSTALL_WEARABLE_PACKAGES"; + + /** MIME types. */ + public static final String ICON_TYPE = "vnd.android.cursor.item/cw_package_icon"; + + @Override + public boolean onCreate() { + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + throw new UnsupportedOperationException("Query is not supported."); + } + + @Override + public String getType(Uri uri) { + if (uri == null) { + throw new IllegalArgumentException("URI passed in is null."); + } + + if (AUTHORITY.equals(uri.getEncodedAuthority())) { + return ICON_TYPE; + } + return null; + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + throw new UnsupportedOperationException("Insert is not supported."); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + if (uri == null) { + throw new IllegalArgumentException("URI passed in is null."); + } + + enforcePermissions(uri); + + if (ICON_TYPE.equals(getType(uri))) { + final File file = WearPackageUtil.getIconFile( + this.getContext().getApplicationContext(), getPackageNameFromUri(uri)); + if (file != null) { + file.delete(); + } + } + + return 0; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException("Update is not supported."); + } + + @Override + public ParcelFileDescriptor openFile( + Uri uri, @SuppressWarnings("unused") String mode) throws FileNotFoundException { + if (uri == null) { + throw new IllegalArgumentException("URI passed in is null."); + } + + enforcePermissions(uri); + + if (ICON_TYPE.equals(getType(uri))) { + final File file = WearPackageUtil.getIconFile( + this.getContext().getApplicationContext(), getPackageNameFromUri(uri)); + if (file != null) { + return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); + } + } + return null; + } + + public static Uri getUriForPackage(final String packageName) { + return Uri.parse("content://" + AUTHORITY + "/icons/" + packageName + ".icon"); + } + + private String getPackageNameFromUri(Uri uri) { + if (uri == null) { + return null; + } + List pathSegments = uri.getPathSegments(); + String packageName = pathSegments.get(pathSegments.size() - 1); + + if (packageName.endsWith(".icon")) { + packageName = packageName.substring(0, packageName.lastIndexOf(".")); + } + return packageName; + } + + /** + * Make sure the calling app is either a system app or the same app or has the right permission. + * @throws SecurityException if the caller has insufficient permissions. + */ + @TargetApi(Build.VERSION_CODES.BASE_1_1) + private void enforcePermissions(Uri uri) { + // Redo some of the permission check in {@link ContentProvider}. Just add an extra check to + // allow System process to access this provider. + Context context = getContext(); + final int pid = Binder.getCallingPid(); + final int uid = Binder.getCallingUid(); + final int myUid = android.os.Process.myUid(); + + if (uid == myUid || isSystemApp(context, pid)) { + return; + } + + if (context.checkPermission(REQUIRED_PERMISSION, pid, uid) == PERMISSION_GRANTED) { + return; + } + + // last chance, check against any uri grants + if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION) + == PERMISSION_GRANTED) { + return; + } + + throw new SecurityException("Permission Denial: reading " + + getClass().getName() + " uri " + uri + " from pid=" + pid + + ", uid=" + uid); + } + + /** + * From the pid of the calling process, figure out whether this is a system app or not. We do + * this by checking the application information corresponding to the pid and then checking if + * FLAG_SYSTEM is set. + */ + @TargetApi(Build.VERSION_CODES.CUPCAKE) + private boolean isSystemApp(Context context, int pid) { + // Get the Activity Manager Object + ActivityManager aManager = + (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + // Get the list of running Applications + List rapInfoList = + aManager.getRunningAppProcesses(); + for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) { + if (rapInfo.pid == pid) { + try { + PackageInfo pkgInfo = context.getPackageManager().getPackageInfo( + rapInfo.pkgList[0], 0); + if (pkgInfo != null && pkgInfo.applicationInfo != null && + (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { + Log.d(TAG, pid + " is a system app."); + return true; + } + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Could not find package information.", e); + return false; + } + } + } + return false; + } +} diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java new file mode 100644 index 00000000..e9ea21e1 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -0,0 +1,608 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.annotation.Nullable; +import android.app.Service; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.FeatureInfo; +import android.content.pm.IPackageDeleteObserver; +import android.content.pm.IPackageInstallObserver; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageParser; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; +import android.os.ParcelFileDescriptor; +import android.os.PowerManager; +import android.os.Process; +import android.text.TextUtils; +import android.util.Log; + +import com.android.packageinstaller.PackageUtil; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Service that will install/uninstall packages. It will check for permissions and features as well. + * + * ----------- + * + * Debugging information: + * + * Install Action example: + * adb shell am startservice -a com.android.packageinstaller.wear.INSTALL_PACKAGE \ + * -t vnd.android.cursor.item/wearable_apk \ + * -d content://com.google.android.clockwork.home.provider/host/com.google.android.wearable.app/wearable/com.google.android.gms/apk \ + * --es android.intent.extra.INSTALLER_PACKAGE_NAME com.google.android.gms \ + * --ez com.google.android.clockwork.EXTRA_CHECK_PERMS false \ + * --eu com.google.android.clockwork.EXTRA_PERM_URI content://com.google.android.clockwork.home.provider/host/com.google.android.wearable.app/permissions \ + * com.android.packageinstaller/com.android.packageinstaller.wear.WearPackageInstallerService + * + * Retry GMS: + * adb shell am startservice -a com.android.packageinstaller.wear.RETRY_GMS \ + * com.android.packageinstaller/com.android.packageinstaller.wear.WearPackageInstallerService + */ +public class WearPackageInstallerService extends Service { + private static final String TAG = "WearPkgInstallerService"; + + private static final String KEY_PERM_URI = + "com.google.android.clockwork.EXTRA_PERM_URI"; + private static final String KEY_CHECK_PERMS = + "com.google.android.clockwork.EXTRA_CHECK_PERMS"; + private static final String KEY_SKIP_IF_SAME_VERSION = + "com.google.android.clockwork.EXTRA_SKIP_IF_SAME_VERSION"; + private static final String KEY_COMPRESSION_ALG = + "com.google.android.clockwork.EXTRA_KEY_COMPRESSION_ALG"; + private static final String KEY_COMPANION_SDK_VERSION = + "com.google.android.clockwork.EXTRA_KEY_COMPANION_SDK_VERSION"; + private static final String KEY_COMPANION_DEVICE_VERSION = + "com.google.android.clockwork.EXTRA_KEY_COMPANION_DEVICE_VERSION"; + + private static final String KEY_PACKAGE_NAME = + "com.google.android.clockwork.EXTRA_PACKAGE_NAME"; + private static final String KEY_APP_LABEL = "com.google.android.clockwork.EXTRA_APP_LABEL"; + private static final String KEY_APP_ICON_URI = + "com.google.android.clockwork.EXTRA_APP_ICON_URI"; + private static final String KEY_PERMS_LIST = "com.google.android.clockwork.EXTRA_PERMS_LIST"; + private static final String KEY_HAS_LAUNCHER = + "com.google.android.clockwork.EXTRA_HAS_LAUNCHER"; + + private static final String HOME_APP_PACKAGE_NAME = "com.google.android.wearable.app"; + private static final String SHOW_PERMS_SERVICE_CLASS = + "com.google.android.clockwork.packagemanager.ShowPermsService"; + + private static final String ASSET_URI_ARG = "assetUri"; + private static final String PACKAGE_NAME_ARG = "packageName"; + private static final String PERM_URI_ARG = "permUri"; + private static final String START_ID_ARG = "startId"; + private static final String CHECK_PERMS_ARG = "checkPerms"; + private static final String SKIP_IF_SAME_VERSION_ARG = "skipIfSameVersion"; + private static final String COMPRESSION_ALG = "compressionAlg"; + private static final String COMPANION_SDK_VERSION = "companionSdkVersion"; + private static final String COMPANION_DEVICE_VERSION = "companionDeviceVersion"; + + /** + * Normally sent by the Play store (See http://go/playstore-gms_updated), we instead + * broadcast, ourselves. http://b/17387718 + */ + private static final String GMS_UPDATED_BROADCAST = "com.google.android.gms.GMS_UPDATED"; + public static final String GMS_PACKAGE_NAME = "com.google.android.gms"; + + private final int START_INSTALL = 1; + private final int START_UNINSTALL = 2; + + private final class ServiceHandler extends Handler { + public ServiceHandler(Looper looper) { + super(looper); + } + + public void handleMessage(Message msg) { + switch (msg.what) { + case START_INSTALL: + installPackage(msg.getData().getString(PACKAGE_NAME_ARG), + (Uri) msg.getData().getParcelable(ASSET_URI_ARG), + (Uri) msg.getData().getParcelable(PERM_URI_ARG), + msg.getData().getInt(START_ID_ARG), + msg.getData().getBoolean(CHECK_PERMS_ARG), + msg.getData().getBoolean(SKIP_IF_SAME_VERSION_ARG), + msg.getData().getString(COMPRESSION_ALG), + msg.getData().getInt(COMPANION_SDK_VERSION), + msg.getData().getInt(COMPANION_DEVICE_VERSION)); + break; + case START_UNINSTALL: + uninstallPackage(msg.getData().getString(PACKAGE_NAME_ARG), + msg.getData().getInt(START_ID_ARG)); + break; + } + } + } + private ServiceHandler mServiceHandler; + + private static volatile PowerManager.WakeLock lockStatic = null; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + super.onCreate(); + HandlerThread thread = new HandlerThread("PackageInstallerThread", + Process.THREAD_PRIORITY_BACKGROUND); + thread.start(); + + mServiceHandler = new ServiceHandler(thread.getLooper()); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (!WearPackageUtil.isWear(this)) { + Log.w(TAG, "Not running on wearable"); + return START_NOT_STICKY; + } + PowerManager.WakeLock lock = getLock(this.getApplicationContext()); + if (!lock.isHeld()) { + lock.acquire(); + } + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Got install/uninstall request " + intent); + } + if (intent != null) { + if (Intent.ACTION_INSTALL_PACKAGE.equals(intent.getAction())) { + final Message msg = mServiceHandler.obtainMessage(START_INSTALL); + final Bundle startInstallArgs = new Bundle(); + startInstallArgs.putParcelable(ASSET_URI_ARG, intent.getData()); + startInstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( + Intent.EXTRA_INSTALLER_PACKAGE_NAME)); + startInstallArgs.putInt(START_ID_ARG, startId); + Uri permUri = intent.getParcelableExtra(KEY_PERM_URI); + startInstallArgs.putParcelable(PERM_URI_ARG, permUri); + startInstallArgs.putBoolean(CHECK_PERMS_ARG, + intent.getBooleanExtra(KEY_CHECK_PERMS, true)); + startInstallArgs.putBoolean(SKIP_IF_SAME_VERSION_ARG, + intent.getBooleanExtra(KEY_SKIP_IF_SAME_VERSION, false)); + startInstallArgs.putString(COMPRESSION_ALG, + intent.getStringExtra(KEY_COMPRESSION_ALG)); + startInstallArgs.putInt(COMPANION_SDK_VERSION, + intent.getIntExtra(KEY_COMPANION_SDK_VERSION, 0)); + startInstallArgs.putInt(COMPANION_DEVICE_VERSION, + intent.getIntExtra(KEY_COMPANION_DEVICE_VERSION, 0)); + msg.setData(startInstallArgs); + mServiceHandler.sendMessage(msg); + } else if (Intent.ACTION_UNINSTALL_PACKAGE.equals(intent.getAction())) { + Message msg = mServiceHandler.obtainMessage(START_UNINSTALL); + Bundle startUninstallArgs = new Bundle(); + startUninstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( + Intent.EXTRA_INSTALLER_PACKAGE_NAME)); + startUninstallArgs.putInt(START_ID_ARG, startId); + msg.setData(startUninstallArgs); + mServiceHandler.sendMessage(msg); + } + } + return START_NOT_STICKY; + } + + private void installPackage(String packageName, Uri packageUri, Uri permUri, int startId, + boolean checkPerms, boolean skipIfSameVersion, @Nullable String compressionAlg, + int companionSdkVersion, int companionDeviceVersion) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Installing package: " + packageName + ", packageUri: " + packageUri + + ",permUri: " + permUri + ", startId: " + startId + ", checkPerms: " + + checkPerms + ", skipIfSameVersion: " + skipIfSameVersion + + ", compressionAlg: " + compressionAlg + ", companionSdkVersion: " + + companionSdkVersion + ", companionDeviceVersion: " + companionDeviceVersion); + } + final PackageManager pm = getPackageManager(); + File tempFile = null; + int installFlags = 0; + PowerManager.WakeLock lock = getLock(this.getApplicationContext()); + boolean messageSent = false; + try { + PackageInfo existingPkgInfo = null; + try { + existingPkgInfo = pm.getPackageInfo(packageName, + PackageManager.GET_UNINSTALLED_PACKAGES); + if(existingPkgInfo != null) { + installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; + } + } catch (PackageManager.NameNotFoundException e) { + // Ignore this exception. We could not find the package, will treat as a new + // installation. + } + if((installFlags & PackageManager.INSTALL_REPLACE_EXISTING )!= 0) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Replacing package:" + packageName); + } + } + ParcelFileDescriptor parcelFd = getContentResolver() + .openFileDescriptor(packageUri, "r"); + tempFile = WearPackageUtil.getFileFromFd(WearPackageInstallerService.this, + parcelFd, packageName, compressionAlg); + if (tempFile == null) { + Log.e(TAG, "Could not create a temp file from FD for " + packageName); + return; + } + PackageParser.Package pkg = PackageUtil.getPackageInfo(tempFile); + if (pkg == null) { + Log.e(TAG, "Could not parse apk information for " + packageName); + return; + } + + if (!pkg.packageName.equals(packageName)) { + Log.e(TAG, "Wearable Package Name has to match what is provided for " + + packageName); + return; + } + + List wearablePerms = pkg.requestedPermissions; + + // Log if the installed pkg has a higher version number. + if (existingPkgInfo != null) { + if (existingPkgInfo.versionCode == pkg.mVersionCode) { + if (skipIfSameVersion) { + Log.w(TAG, "Version number (" + pkg.mVersionCode + + ") of new app is equal to existing app for " + packageName + + "; not installing due to versionCheck"); + return; + } else { + Log.w(TAG, "Version number of new app (" + pkg.mVersionCode + + ") is equal to existing app for " + packageName); + } + } else if (existingPkgInfo.versionCode > pkg.mVersionCode) { + Log.w(TAG, "Version number of new app (" + pkg.mVersionCode + + ") is lower than existing app ( " + existingPkgInfo.versionCode + + ") for " + packageName); + } + + // Following the Android Phone model, we should only check for permissions for any + // newly defined perms. + if (existingPkgInfo.requestedPermissions != null) { + for (int i = 0; i < existingPkgInfo.requestedPermissions.length; ++i) { + // If the permission is granted, then we will not ask to request it again. + if ((existingPkgInfo.requestedPermissionsFlags[i] & + PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) { + wearablePerms.remove(existingPkgInfo.requestedPermissions[i]); + } + } + } + } + + // Check permissions on both the new wearable package and also on the already installed + // wearable package. + // If the app is targeting API level 23, we will also start a service in ClockworkHome + // which will ultimately prompt the user to accept/reject permissions. + if (checkPerms && !checkPermissions(pkg, companionSdkVersion, companionDeviceVersion, + permUri, wearablePerms, tempFile)) { + Log.w(TAG, "Wearable does not have enough permissions."); + return; + } + + // Check that the wearable has all the features. + boolean hasAllFeatures = true; + if (pkg.reqFeatures != null) { + for (FeatureInfo feature : pkg.reqFeatures) { + if (feature.name != null && !pm.hasSystemFeature(feature.name) && + (feature.flags & FeatureInfo.FLAG_REQUIRED) != 0) { + Log.e(TAG, "Wearable does not have required feature: " + feature + + " for " + packageName); + hasAllFeatures = false; + } + } + } + + if (!hasAllFeatures) { + return; + } + + // Finally install the package. + pm.installPackage(Uri.fromFile(tempFile), + new PackageInstallObserver(this, lock, startId), installFlags, packageName); + messageSent = true; + Log.i(TAG, "Sent installation request for " + packageName); + } catch (FileNotFoundException e) { + Log.e(TAG, "Could not find the file with URI " + packageUri, e); + } finally { + if (!messageSent) { + // Some error happened. If the message has been sent, we can wait for the observer + // which will finish the service. + if (tempFile != null) { + tempFile.delete(); + } + finishService(lock, startId); + } + } + } + + private void uninstallPackage(String packageName, int startId) { + final PackageManager pm = getPackageManager(); + PowerManager.WakeLock lock = getLock(this.getApplicationContext()); + pm.deletePackage(packageName, new PackageDeleteObserver(lock, startId), + PackageManager.DELETE_ALL_USERS); + startPermsServiceForUninstall(packageName); + Log.i(TAG, "Sent delete request for " + packageName); + } + + private boolean checkPermissions(PackageParser.Package pkg, int companionSdkVersion, + int companionDeviceVersion, Uri permUri, List wearablePermissions, + File apkFile) { + if (permUri == null) { + Log.e(TAG, "Permission URI is null"); + return false; + } + Cursor permCursor = getContentResolver().query(permUri, null, null, null, null); + if (permCursor == null) { + Log.e(TAG, "Could not get the cursor for the permissions"); + return false; + } + + final String packageName = pkg.packageName; + + Set grantedPerms = new HashSet<>(); + Set ungrantedPerms = new HashSet<>(); + while(permCursor.moveToNext()) { + // Make sure that the MatrixCursor returned by the ContentProvider has 2 columns and + // verify their types. + if (permCursor.getColumnCount() == 2 + && Cursor.FIELD_TYPE_STRING == permCursor.getType(0) + && Cursor.FIELD_TYPE_INTEGER == permCursor.getType(1)) { + String perm = permCursor.getString(0); + Integer granted = permCursor.getInt(1); + if (granted == 1) { + grantedPerms.add(perm); + } else { + ungrantedPerms.add(perm); + } + } + } + permCursor.close(); + + ArrayList unavailableWearablePerms = new ArrayList<>(); + for (String wearablePerm : wearablePermissions) { + if (!grantedPerms.contains(wearablePerm)) { + unavailableWearablePerms.add(wearablePerm); + if (!ungrantedPerms.contains(wearablePerm)) { + // This is an error condition. This means that the wearable has permissions that + // are not even declared in its host app. This is a developer error. + Log.e(TAG, "Wearable " + packageName + " has a permission \"" + wearablePerm + + "\" that is not defined in the host application's manifest."); + } else { + Log.w(TAG, "Wearable " + packageName + " has a permission \"" + wearablePerm + + "\" that is not granted in the host application."); + } + } + } + + + // If the Wear App is targeted for M-release, since the permission model has been changed, + // permissions may not be granted on the phone yet. We need a different flow for user to + // accept these permissions. + // + // Case 1: Companion App >= 23 (and running on M), Wear App targeting >= 23 + // - If Wear is running L (ie DMR1), show a dialog so that the user can accept all perms + // - If Wear is running M (ie E-release), use new permission model. + // Case 2: Companion App <= 22, Wear App targeting <= 22 + // - Default to old behavior. + // Case 3: Companion App <= 22, Wear App targeting >= 23 + // - If Wear is running L (ie DMR1), install the app as before. In effect, pretend + // like wear app is targeting 22. + // - If Wear is running M (ie E-release), use new permission model. + // Case 4: Companion App >= 23 (and running on M), Wear App targeting <= 22 + // - Show a warning below to the developer. + // - Show a dialog as in Case 1 with DMR1. This behavior will happen in E and DMR1. + // Case 5: We did not get Companion App's/Device's version (we have to guess here) + // - Show dialog if Wear App targeting >= 23 and Wear is not running M + if (unavailableWearablePerms.size() > 0) { + boolean isCompanionTargetingM = companionSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isCompanionRunningM = companionDeviceVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isWearTargetingM = + pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isWearRunningM = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; + + if (companionSdkVersion == 0 || companionDeviceVersion == 0) { // Case 5 + if (isWearTargetingM && !isWearRunningM) { + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } + } else if (isCompanionTargetingM && isCompanionRunningM) { + if (!isWearTargetingM) { // Case 4 + Log.w(TAG, "MNC: Wear app's targetSdkVersion should be at least 23, if phone " + + "app is targeting at least 23."); + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } else if (!isWearRunningM) { // Case 1, part 1 + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } + } // Else, nothing to do. See explanation above. + } + + return unavailableWearablePerms.size() == 0; + } + + private void finishService(PowerManager.WakeLock lock, int startId) { + if (lock.isHeld()) { + lock.release(); + } + stopSelf(startId); + } + + private synchronized PowerManager.WakeLock getLock(Context context) { + if (lockStatic == null) { + PowerManager mgr = + (PowerManager) context.getSystemService(Context.POWER_SERVICE); + lockStatic = mgr.newWakeLock( + PowerManager.PARTIAL_WAKE_LOCK, context.getClass().getSimpleName()); + lockStatic.setReferenceCounted(true); + } + return lockStatic; + } + + private void startPermsServiceForInstall(final PackageParser.Package pkg, final File apkFile, + ArrayList unavailableWearablePerms) { + final String packageName = pkg.packageName; + + Intent showPermsIntent = new Intent() + .setComponent(new ComponentName(HOME_APP_PACKAGE_NAME, SHOW_PERMS_SERVICE_CLASS)) + .setAction(Intent.ACTION_INSTALL_PACKAGE); + final PackageManager pm = getPackageManager(); + pkg.applicationInfo.publicSourceDir = apkFile.getPath(); + final CharSequence label = pkg.applicationInfo.loadLabel(pm); + final Uri iconUri = getIconFileUri(packageName, pkg.applicationInfo.loadIcon(pm)); + if (TextUtils.isEmpty(label) || iconUri == null) { + Log.e(TAG, "MNC: Could not launch service since either label " + label + + ", or icon Uri " + iconUri + " is invalid."); + } else { + showPermsIntent.putExtra(KEY_APP_LABEL, label); + showPermsIntent.putExtra(KEY_APP_ICON_URI, iconUri); + showPermsIntent.putExtra(KEY_PACKAGE_NAME, packageName); + showPermsIntent.putExtra(KEY_PERMS_LIST, + unavailableWearablePerms.toArray(new String[0])); + showPermsIntent.putExtra(KEY_HAS_LAUNCHER, WearPackageUtil.hasLauncherActivity(pkg)); + + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "MNC: Launching Intent " + showPermsIntent + " for " + packageName + + " with name " + label); + } + startService(showPermsIntent); + } + } + + private void startPermsServiceForUninstall(final String packageName) { + Intent showPermsIntent = new Intent() + .setComponent(new ComponentName(HOME_APP_PACKAGE_NAME, SHOW_PERMS_SERVICE_CLASS)) + .setAction(Intent.ACTION_UNINSTALL_PACKAGE); + showPermsIntent.putExtra(KEY_PACKAGE_NAME, packageName); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Launching Intent " + showPermsIntent + " for " + packageName); + } + startService(showPermsIntent); + } + + private Uri getIconFileUri(final String packageName, final Drawable d) { + if (d == null || !(d instanceof BitmapDrawable)) { + Log.e(TAG, "Drawable is not a BitmapDrawable for " + packageName); + return null; + } + File iconFile = WearPackageUtil.getIconFile(this, packageName); + + if (iconFile == null) { + Log.e(TAG, "Could not get icon file for " + packageName); + return null; + } + + FileOutputStream fos = null; + try { + // Convert bitmap to byte array + Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); + + // Write the bytes into the file + fos = new FileOutputStream(iconFile); + fos.write(bos.toByteArray()); + fos.flush(); + + return WearPackageIconProvider.getUriForPackage(packageName); + } catch (IOException e) { + Log.e(TAG, "Could not convert drawable to icon file for package " + packageName, e); + return null; + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + // ignore + } + } + } + } + + private class PackageInstallObserver extends IPackageInstallObserver.Stub { + private Context mContext; + private PowerManager.WakeLock mWakeLock; + private int mStartId; + private PackageInstallObserver(Context context, PowerManager.WakeLock wakeLock, + int startId) { + mContext = context; + mWakeLock = wakeLock; + mStartId = startId; + } + + public void packageInstalled(String packageName, int returnCode) { + if (returnCode >= 0) { + Log.i(TAG, "Package " + packageName + " was installed."); + } else { + Log.e(TAG, "Package install failed " + packageName + ", returnCode " + returnCode); + } + + // Delete tempFile from the file system. + File tempFile = WearPackageUtil.getTemporaryFile(mContext, packageName); + if (tempFile != null) { + tempFile.delete(); + } + + // Broadcast the "UPDATED" gmscore intent, normally sent by play store. + // TODO: Remove this broadcast if/when we get the play store to do this for us. + if (GMS_PACKAGE_NAME.equals(packageName)) { + Intent gmsInstalledIntent = new Intent(GMS_UPDATED_BROADCAST); + gmsInstalledIntent.setPackage(GMS_PACKAGE_NAME); + mContext.sendBroadcast(gmsInstalledIntent); + } + + finishService(mWakeLock, mStartId); + } + } + + private class PackageDeleteObserver extends IPackageDeleteObserver.Stub { + private PowerManager.WakeLock mWakeLock; + private int mStartId; + + private PackageDeleteObserver(PowerManager.WakeLock wakeLock, int startId) { + mWakeLock = wakeLock; + mStartId = startId; + } + + public void packageDeleted(String packageName, int returnCode) { + if (returnCode >= 0) { + Log.i(TAG, "Package " + packageName + " was uninstalled."); + } else { + Log.e(TAG, "Package uninstall failed " + packageName + ", returnCode " + + returnCode); + } + finishService(mWakeLock, mStartId); + } + } +} diff --git a/src/com/android/packageinstaller/wear/WearPackageUtil.java b/src/com/android/packageinstaller/wear/WearPackageUtil.java new file mode 100644 index 00000000..bec697d9 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageUtil.java @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.annotation.Nullable; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.PackageParser; +import android.os.ParcelFileDescriptor; +import android.system.ErrnoException; +import android.system.Os; +import android.text.TextUtils; +import android.util.Log; + +import org.tukaani.xz.LZMAInputStream; +import org.tukaani.xz.XZInputStream; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +public class WearPackageUtil { + private static final String TAG = "WearablePkgInstaller"; + + private static final String COMPRESSION_LZMA = "lzma"; + private static final String COMPRESSION_XZ = "xz"; + + public static File getTemporaryFile(Context context, String packageName) { + try { + File newFileDir = new File(context.getFilesDir(), "tmp"); + newFileDir.mkdirs(); + Os.chmod(newFileDir.getAbsolutePath(), 0771); + File newFile = new File(newFileDir, packageName + ".apk"); + return newFile; + } catch (ErrnoException e) { + Log.e(TAG, "Failed to open.", e); + return null; + } + } + + public static File getIconFile(final Context context, final String packageName) { + try { + File newFileDir = new File(context.getFilesDir(), "images/icons"); + newFileDir.mkdirs(); + Os.chmod(newFileDir.getAbsolutePath(), 0771); + return new File(newFileDir, packageName + ".icon"); + } catch (ErrnoException e) { + Log.e(TAG, "Failed to open.", e); + return null; + } + } + + /** + * In order to make sure that the Wearable Asset Manager has a reasonable apk that can be used + * by the PackageManager, we will parse it before sending it to the PackageManager. + * Unfortunately, PackageParser needs a file to parse. So, we have to temporarily convert the fd + * to a File. + * + * @param context + * @param fd FileDescriptor to convert to File + * @param packageName Name of package, will define the name of the file + * @param compressionAlg Can be null. For ALT mode the APK will be compressed. We will + * decompress it here + */ + public static File getFileFromFd(Context context, ParcelFileDescriptor fd, + String packageName, @Nullable String compressionAlg) { + File newFile = getTemporaryFile(context, packageName); + if (fd == null || fd.getFileDescriptor() == null) { + return null; + } + InputStream fr = new ParcelFileDescriptor.AutoCloseInputStream(fd); + try { + if (TextUtils.equals(compressionAlg, COMPRESSION_XZ)) { + fr = new XZInputStream(fr); + } else if (TextUtils.equals(compressionAlg, COMPRESSION_LZMA)) { + fr = new LZMAInputStream(fr); + } + } catch (IOException e) { + Log.e(TAG, "Compression was set to " + compressionAlg + ", but could not decode ", e); + return null; + } + + int nRead; + byte[] data = new byte[1024]; + try { + final FileOutputStream fo = new FileOutputStream(newFile); + while ((nRead = fr.read(data, 0, data.length)) != -1) { + fo.write(data, 0, nRead); + } + fo.flush(); + fo.close(); + Os.chmod(newFile.getAbsolutePath(), 0644); + return newFile; + } catch (IOException e) { + Log.e(TAG, "Reading from Asset FD or writing to temp file failed ", e); + return null; + } catch (ErrnoException e) { + Log.e(TAG, "Could not set permissions on file ", e); + return null; + } finally { + try { + fr.close(); + } catch (IOException e) { + Log.e(TAG, "Failed to close the file from FD ", e); + } + } + } + + public static boolean hasLauncherActivity(PackageParser.Package pkg) { + if (pkg == null || pkg.activities == null) { + return false; + } + + final int activityCount = pkg.activities.size(); + for (int i = 0; i < activityCount; ++i) { + if (pkg.activities.get(i).intents != null) { + ArrayList intents = + pkg.activities.get(i).intents; + final int intentsCount = intents.size(); + for (int j = 0; j < intentsCount; ++j) { + final PackageParser.ActivityIntentInfo intentInfo = intents.get(j); + if (intentInfo.hasAction(Intent.ACTION_MAIN)) { + if (intentInfo.hasCategory(Intent.CATEGORY_INFO) || + intentInfo .hasCategory(Intent.CATEGORY_LAUNCHER)) { + return true; + } + } + } + } + } + return false; + } + + public static boolean isWear(final Context context) { + return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); + } +} -- cgit v1.2.3 From 5b26c2e64ceeaa1e71436b36a28f53100e358ed8 Mon Sep 17 00:00:00 2001 From: Griff Hazen Date: Tue, 8 Sep 2015 22:59:10 +0000 Subject: Revert "Move ClockworkPackageInstaller functionality here" Breaking ub-wear-dinar/ub-wear-master builds This reverts commit aa4368cafc3c92b33a99c3fbe0e6af08d7e092e4. Change-Id: Ib22189891e9cd9eef61cdaa5ac329d05ab9783e8 --- .../wear/WearPackageIconProvider.java | 202 ------- .../wear/WearPackageInstallerService.java | 608 --------------------- .../packageinstaller/wear/WearPackageUtil.java | 154 ------ 3 files changed, 964 deletions(-) delete mode 100644 src/com/android/packageinstaller/wear/WearPackageIconProvider.java delete mode 100644 src/com/android/packageinstaller/wear/WearPackageInstallerService.java delete mode 100644 src/com/android/packageinstaller/wear/WearPackageUtil.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageIconProvider.java b/src/com/android/packageinstaller/wear/WearPackageIconProvider.java deleted file mode 100644 index 02b9d298..00000000 --- a/src/com/android/packageinstaller/wear/WearPackageIconProvider.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.wear; - -import android.annotation.TargetApi; -import android.app.ActivityManager; -import android.content.ContentProvider; -import android.content.ContentValues; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.database.Cursor; -import android.net.Uri; -import android.os.Binder; -import android.os.Build; -import android.os.ParcelFileDescriptor; -import android.util.Log; - -import java.io.File; -import java.io.FileNotFoundException; -import java.util.List; - -import static android.content.pm.PackageManager.PERMISSION_GRANTED; - -public class WearPackageIconProvider extends ContentProvider { - private static final String TAG = "WearPackageIconProvider"; - public static final String AUTHORITY = "com.google.android.packageinstaller.wear.provider"; - - private static final String REQUIRED_PERMISSION = - "com.google.android.permission.INSTALL_WEARABLE_PACKAGES"; - - /** MIME types. */ - public static final String ICON_TYPE = "vnd.android.cursor.item/cw_package_icon"; - - @Override - public boolean onCreate() { - return true; - } - - @Override - public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, - String sortOrder) { - throw new UnsupportedOperationException("Query is not supported."); - } - - @Override - public String getType(Uri uri) { - if (uri == null) { - throw new IllegalArgumentException("URI passed in is null."); - } - - if (AUTHORITY.equals(uri.getEncodedAuthority())) { - return ICON_TYPE; - } - return null; - } - - @Override - public Uri insert(Uri uri, ContentValues values) { - throw new UnsupportedOperationException("Insert is not supported."); - } - - @Override - public int delete(Uri uri, String selection, String[] selectionArgs) { - if (uri == null) { - throw new IllegalArgumentException("URI passed in is null."); - } - - enforcePermissions(uri); - - if (ICON_TYPE.equals(getType(uri))) { - final File file = WearPackageUtil.getIconFile( - this.getContext().getApplicationContext(), getPackageNameFromUri(uri)); - if (file != null) { - file.delete(); - } - } - - return 0; - } - - @Override - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { - throw new UnsupportedOperationException("Update is not supported."); - } - - @Override - public ParcelFileDescriptor openFile( - Uri uri, @SuppressWarnings("unused") String mode) throws FileNotFoundException { - if (uri == null) { - throw new IllegalArgumentException("URI passed in is null."); - } - - enforcePermissions(uri); - - if (ICON_TYPE.equals(getType(uri))) { - final File file = WearPackageUtil.getIconFile( - this.getContext().getApplicationContext(), getPackageNameFromUri(uri)); - if (file != null) { - return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); - } - } - return null; - } - - public static Uri getUriForPackage(final String packageName) { - return Uri.parse("content://" + AUTHORITY + "/icons/" + packageName + ".icon"); - } - - private String getPackageNameFromUri(Uri uri) { - if (uri == null) { - return null; - } - List pathSegments = uri.getPathSegments(); - String packageName = pathSegments.get(pathSegments.size() - 1); - - if (packageName.endsWith(".icon")) { - packageName = packageName.substring(0, packageName.lastIndexOf(".")); - } - return packageName; - } - - /** - * Make sure the calling app is either a system app or the same app or has the right permission. - * @throws SecurityException if the caller has insufficient permissions. - */ - @TargetApi(Build.VERSION_CODES.BASE_1_1) - private void enforcePermissions(Uri uri) { - // Redo some of the permission check in {@link ContentProvider}. Just add an extra check to - // allow System process to access this provider. - Context context = getContext(); - final int pid = Binder.getCallingPid(); - final int uid = Binder.getCallingUid(); - final int myUid = android.os.Process.myUid(); - - if (uid == myUid || isSystemApp(context, pid)) { - return; - } - - if (context.checkPermission(REQUIRED_PERMISSION, pid, uid) == PERMISSION_GRANTED) { - return; - } - - // last chance, check against any uri grants - if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION) - == PERMISSION_GRANTED) { - return; - } - - throw new SecurityException("Permission Denial: reading " - + getClass().getName() + " uri " + uri + " from pid=" + pid - + ", uid=" + uid); - } - - /** - * From the pid of the calling process, figure out whether this is a system app or not. We do - * this by checking the application information corresponding to the pid and then checking if - * FLAG_SYSTEM is set. - */ - @TargetApi(Build.VERSION_CODES.CUPCAKE) - private boolean isSystemApp(Context context, int pid) { - // Get the Activity Manager Object - ActivityManager aManager = - (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); - // Get the list of running Applications - List rapInfoList = - aManager.getRunningAppProcesses(); - for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) { - if (rapInfo.pid == pid) { - try { - PackageInfo pkgInfo = context.getPackageManager().getPackageInfo( - rapInfo.pkgList[0], 0); - if (pkgInfo != null && pkgInfo.applicationInfo != null && - (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { - Log.d(TAG, pid + " is a system app."); - return true; - } - } catch (PackageManager.NameNotFoundException e) { - Log.e(TAG, "Could not find package information.", e); - return false; - } - } - } - return false; - } -} diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java deleted file mode 100644 index e9ea21e1..00000000 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ /dev/null @@ -1,608 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.wear; - -import android.annotation.Nullable; -import android.app.Service; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.FeatureInfo; -import android.content.pm.IPackageDeleteObserver; -import android.content.pm.IPackageInstallObserver; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageParser; -import android.database.Cursor; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.os.HandlerThread; -import android.os.IBinder; -import android.os.Looper; -import android.os.Message; -import android.os.ParcelFileDescriptor; -import android.os.PowerManager; -import android.os.Process; -import android.text.TextUtils; -import android.util.Log; - -import com.android.packageinstaller.PackageUtil; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Service that will install/uninstall packages. It will check for permissions and features as well. - * - * ----------- - * - * Debugging information: - * - * Install Action example: - * adb shell am startservice -a com.android.packageinstaller.wear.INSTALL_PACKAGE \ - * -t vnd.android.cursor.item/wearable_apk \ - * -d content://com.google.android.clockwork.home.provider/host/com.google.android.wearable.app/wearable/com.google.android.gms/apk \ - * --es android.intent.extra.INSTALLER_PACKAGE_NAME com.google.android.gms \ - * --ez com.google.android.clockwork.EXTRA_CHECK_PERMS false \ - * --eu com.google.android.clockwork.EXTRA_PERM_URI content://com.google.android.clockwork.home.provider/host/com.google.android.wearable.app/permissions \ - * com.android.packageinstaller/com.android.packageinstaller.wear.WearPackageInstallerService - * - * Retry GMS: - * adb shell am startservice -a com.android.packageinstaller.wear.RETRY_GMS \ - * com.android.packageinstaller/com.android.packageinstaller.wear.WearPackageInstallerService - */ -public class WearPackageInstallerService extends Service { - private static final String TAG = "WearPkgInstallerService"; - - private static final String KEY_PERM_URI = - "com.google.android.clockwork.EXTRA_PERM_URI"; - private static final String KEY_CHECK_PERMS = - "com.google.android.clockwork.EXTRA_CHECK_PERMS"; - private static final String KEY_SKIP_IF_SAME_VERSION = - "com.google.android.clockwork.EXTRA_SKIP_IF_SAME_VERSION"; - private static final String KEY_COMPRESSION_ALG = - "com.google.android.clockwork.EXTRA_KEY_COMPRESSION_ALG"; - private static final String KEY_COMPANION_SDK_VERSION = - "com.google.android.clockwork.EXTRA_KEY_COMPANION_SDK_VERSION"; - private static final String KEY_COMPANION_DEVICE_VERSION = - "com.google.android.clockwork.EXTRA_KEY_COMPANION_DEVICE_VERSION"; - - private static final String KEY_PACKAGE_NAME = - "com.google.android.clockwork.EXTRA_PACKAGE_NAME"; - private static final String KEY_APP_LABEL = "com.google.android.clockwork.EXTRA_APP_LABEL"; - private static final String KEY_APP_ICON_URI = - "com.google.android.clockwork.EXTRA_APP_ICON_URI"; - private static final String KEY_PERMS_LIST = "com.google.android.clockwork.EXTRA_PERMS_LIST"; - private static final String KEY_HAS_LAUNCHER = - "com.google.android.clockwork.EXTRA_HAS_LAUNCHER"; - - private static final String HOME_APP_PACKAGE_NAME = "com.google.android.wearable.app"; - private static final String SHOW_PERMS_SERVICE_CLASS = - "com.google.android.clockwork.packagemanager.ShowPermsService"; - - private static final String ASSET_URI_ARG = "assetUri"; - private static final String PACKAGE_NAME_ARG = "packageName"; - private static final String PERM_URI_ARG = "permUri"; - private static final String START_ID_ARG = "startId"; - private static final String CHECK_PERMS_ARG = "checkPerms"; - private static final String SKIP_IF_SAME_VERSION_ARG = "skipIfSameVersion"; - private static final String COMPRESSION_ALG = "compressionAlg"; - private static final String COMPANION_SDK_VERSION = "companionSdkVersion"; - private static final String COMPANION_DEVICE_VERSION = "companionDeviceVersion"; - - /** - * Normally sent by the Play store (See http://go/playstore-gms_updated), we instead - * broadcast, ourselves. http://b/17387718 - */ - private static final String GMS_UPDATED_BROADCAST = "com.google.android.gms.GMS_UPDATED"; - public static final String GMS_PACKAGE_NAME = "com.google.android.gms"; - - private final int START_INSTALL = 1; - private final int START_UNINSTALL = 2; - - private final class ServiceHandler extends Handler { - public ServiceHandler(Looper looper) { - super(looper); - } - - public void handleMessage(Message msg) { - switch (msg.what) { - case START_INSTALL: - installPackage(msg.getData().getString(PACKAGE_NAME_ARG), - (Uri) msg.getData().getParcelable(ASSET_URI_ARG), - (Uri) msg.getData().getParcelable(PERM_URI_ARG), - msg.getData().getInt(START_ID_ARG), - msg.getData().getBoolean(CHECK_PERMS_ARG), - msg.getData().getBoolean(SKIP_IF_SAME_VERSION_ARG), - msg.getData().getString(COMPRESSION_ALG), - msg.getData().getInt(COMPANION_SDK_VERSION), - msg.getData().getInt(COMPANION_DEVICE_VERSION)); - break; - case START_UNINSTALL: - uninstallPackage(msg.getData().getString(PACKAGE_NAME_ARG), - msg.getData().getInt(START_ID_ARG)); - break; - } - } - } - private ServiceHandler mServiceHandler; - - private static volatile PowerManager.WakeLock lockStatic = null; - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public void onCreate() { - super.onCreate(); - HandlerThread thread = new HandlerThread("PackageInstallerThread", - Process.THREAD_PRIORITY_BACKGROUND); - thread.start(); - - mServiceHandler = new ServiceHandler(thread.getLooper()); - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - if (!WearPackageUtil.isWear(this)) { - Log.w(TAG, "Not running on wearable"); - return START_NOT_STICKY; - } - PowerManager.WakeLock lock = getLock(this.getApplicationContext()); - if (!lock.isHeld()) { - lock.acquire(); - } - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Got install/uninstall request " + intent); - } - if (intent != null) { - if (Intent.ACTION_INSTALL_PACKAGE.equals(intent.getAction())) { - final Message msg = mServiceHandler.obtainMessage(START_INSTALL); - final Bundle startInstallArgs = new Bundle(); - startInstallArgs.putParcelable(ASSET_URI_ARG, intent.getData()); - startInstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( - Intent.EXTRA_INSTALLER_PACKAGE_NAME)); - startInstallArgs.putInt(START_ID_ARG, startId); - Uri permUri = intent.getParcelableExtra(KEY_PERM_URI); - startInstallArgs.putParcelable(PERM_URI_ARG, permUri); - startInstallArgs.putBoolean(CHECK_PERMS_ARG, - intent.getBooleanExtra(KEY_CHECK_PERMS, true)); - startInstallArgs.putBoolean(SKIP_IF_SAME_VERSION_ARG, - intent.getBooleanExtra(KEY_SKIP_IF_SAME_VERSION, false)); - startInstallArgs.putString(COMPRESSION_ALG, - intent.getStringExtra(KEY_COMPRESSION_ALG)); - startInstallArgs.putInt(COMPANION_SDK_VERSION, - intent.getIntExtra(KEY_COMPANION_SDK_VERSION, 0)); - startInstallArgs.putInt(COMPANION_DEVICE_VERSION, - intent.getIntExtra(KEY_COMPANION_DEVICE_VERSION, 0)); - msg.setData(startInstallArgs); - mServiceHandler.sendMessage(msg); - } else if (Intent.ACTION_UNINSTALL_PACKAGE.equals(intent.getAction())) { - Message msg = mServiceHandler.obtainMessage(START_UNINSTALL); - Bundle startUninstallArgs = new Bundle(); - startUninstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( - Intent.EXTRA_INSTALLER_PACKAGE_NAME)); - startUninstallArgs.putInt(START_ID_ARG, startId); - msg.setData(startUninstallArgs); - mServiceHandler.sendMessage(msg); - } - } - return START_NOT_STICKY; - } - - private void installPackage(String packageName, Uri packageUri, Uri permUri, int startId, - boolean checkPerms, boolean skipIfSameVersion, @Nullable String compressionAlg, - int companionSdkVersion, int companionDeviceVersion) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Installing package: " + packageName + ", packageUri: " + packageUri + - ",permUri: " + permUri + ", startId: " + startId + ", checkPerms: " + - checkPerms + ", skipIfSameVersion: " + skipIfSameVersion + - ", compressionAlg: " + compressionAlg + ", companionSdkVersion: " + - companionSdkVersion + ", companionDeviceVersion: " + companionDeviceVersion); - } - final PackageManager pm = getPackageManager(); - File tempFile = null; - int installFlags = 0; - PowerManager.WakeLock lock = getLock(this.getApplicationContext()); - boolean messageSent = false; - try { - PackageInfo existingPkgInfo = null; - try { - existingPkgInfo = pm.getPackageInfo(packageName, - PackageManager.GET_UNINSTALLED_PACKAGES); - if(existingPkgInfo != null) { - installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; - } - } catch (PackageManager.NameNotFoundException e) { - // Ignore this exception. We could not find the package, will treat as a new - // installation. - } - if((installFlags & PackageManager.INSTALL_REPLACE_EXISTING )!= 0) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Replacing package:" + packageName); - } - } - ParcelFileDescriptor parcelFd = getContentResolver() - .openFileDescriptor(packageUri, "r"); - tempFile = WearPackageUtil.getFileFromFd(WearPackageInstallerService.this, - parcelFd, packageName, compressionAlg); - if (tempFile == null) { - Log.e(TAG, "Could not create a temp file from FD for " + packageName); - return; - } - PackageParser.Package pkg = PackageUtil.getPackageInfo(tempFile); - if (pkg == null) { - Log.e(TAG, "Could not parse apk information for " + packageName); - return; - } - - if (!pkg.packageName.equals(packageName)) { - Log.e(TAG, "Wearable Package Name has to match what is provided for " + - packageName); - return; - } - - List wearablePerms = pkg.requestedPermissions; - - // Log if the installed pkg has a higher version number. - if (existingPkgInfo != null) { - if (existingPkgInfo.versionCode == pkg.mVersionCode) { - if (skipIfSameVersion) { - Log.w(TAG, "Version number (" + pkg.mVersionCode + - ") of new app is equal to existing app for " + packageName + - "; not installing due to versionCheck"); - return; - } else { - Log.w(TAG, "Version number of new app (" + pkg.mVersionCode + - ") is equal to existing app for " + packageName); - } - } else if (existingPkgInfo.versionCode > pkg.mVersionCode) { - Log.w(TAG, "Version number of new app (" + pkg.mVersionCode + - ") is lower than existing app ( " + existingPkgInfo.versionCode + - ") for " + packageName); - } - - // Following the Android Phone model, we should only check for permissions for any - // newly defined perms. - if (existingPkgInfo.requestedPermissions != null) { - for (int i = 0; i < existingPkgInfo.requestedPermissions.length; ++i) { - // If the permission is granted, then we will not ask to request it again. - if ((existingPkgInfo.requestedPermissionsFlags[i] & - PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) { - wearablePerms.remove(existingPkgInfo.requestedPermissions[i]); - } - } - } - } - - // Check permissions on both the new wearable package and also on the already installed - // wearable package. - // If the app is targeting API level 23, we will also start a service in ClockworkHome - // which will ultimately prompt the user to accept/reject permissions. - if (checkPerms && !checkPermissions(pkg, companionSdkVersion, companionDeviceVersion, - permUri, wearablePerms, tempFile)) { - Log.w(TAG, "Wearable does not have enough permissions."); - return; - } - - // Check that the wearable has all the features. - boolean hasAllFeatures = true; - if (pkg.reqFeatures != null) { - for (FeatureInfo feature : pkg.reqFeatures) { - if (feature.name != null && !pm.hasSystemFeature(feature.name) && - (feature.flags & FeatureInfo.FLAG_REQUIRED) != 0) { - Log.e(TAG, "Wearable does not have required feature: " + feature + - " for " + packageName); - hasAllFeatures = false; - } - } - } - - if (!hasAllFeatures) { - return; - } - - // Finally install the package. - pm.installPackage(Uri.fromFile(tempFile), - new PackageInstallObserver(this, lock, startId), installFlags, packageName); - messageSent = true; - Log.i(TAG, "Sent installation request for " + packageName); - } catch (FileNotFoundException e) { - Log.e(TAG, "Could not find the file with URI " + packageUri, e); - } finally { - if (!messageSent) { - // Some error happened. If the message has been sent, we can wait for the observer - // which will finish the service. - if (tempFile != null) { - tempFile.delete(); - } - finishService(lock, startId); - } - } - } - - private void uninstallPackage(String packageName, int startId) { - final PackageManager pm = getPackageManager(); - PowerManager.WakeLock lock = getLock(this.getApplicationContext()); - pm.deletePackage(packageName, new PackageDeleteObserver(lock, startId), - PackageManager.DELETE_ALL_USERS); - startPermsServiceForUninstall(packageName); - Log.i(TAG, "Sent delete request for " + packageName); - } - - private boolean checkPermissions(PackageParser.Package pkg, int companionSdkVersion, - int companionDeviceVersion, Uri permUri, List wearablePermissions, - File apkFile) { - if (permUri == null) { - Log.e(TAG, "Permission URI is null"); - return false; - } - Cursor permCursor = getContentResolver().query(permUri, null, null, null, null); - if (permCursor == null) { - Log.e(TAG, "Could not get the cursor for the permissions"); - return false; - } - - final String packageName = pkg.packageName; - - Set grantedPerms = new HashSet<>(); - Set ungrantedPerms = new HashSet<>(); - while(permCursor.moveToNext()) { - // Make sure that the MatrixCursor returned by the ContentProvider has 2 columns and - // verify their types. - if (permCursor.getColumnCount() == 2 - && Cursor.FIELD_TYPE_STRING == permCursor.getType(0) - && Cursor.FIELD_TYPE_INTEGER == permCursor.getType(1)) { - String perm = permCursor.getString(0); - Integer granted = permCursor.getInt(1); - if (granted == 1) { - grantedPerms.add(perm); - } else { - ungrantedPerms.add(perm); - } - } - } - permCursor.close(); - - ArrayList unavailableWearablePerms = new ArrayList<>(); - for (String wearablePerm : wearablePermissions) { - if (!grantedPerms.contains(wearablePerm)) { - unavailableWearablePerms.add(wearablePerm); - if (!ungrantedPerms.contains(wearablePerm)) { - // This is an error condition. This means that the wearable has permissions that - // are not even declared in its host app. This is a developer error. - Log.e(TAG, "Wearable " + packageName + " has a permission \"" + wearablePerm - + "\" that is not defined in the host application's manifest."); - } else { - Log.w(TAG, "Wearable " + packageName + " has a permission \"" + wearablePerm + - "\" that is not granted in the host application."); - } - } - } - - - // If the Wear App is targeted for M-release, since the permission model has been changed, - // permissions may not be granted on the phone yet. We need a different flow for user to - // accept these permissions. - // - // Case 1: Companion App >= 23 (and running on M), Wear App targeting >= 23 - // - If Wear is running L (ie DMR1), show a dialog so that the user can accept all perms - // - If Wear is running M (ie E-release), use new permission model. - // Case 2: Companion App <= 22, Wear App targeting <= 22 - // - Default to old behavior. - // Case 3: Companion App <= 22, Wear App targeting >= 23 - // - If Wear is running L (ie DMR1), install the app as before. In effect, pretend - // like wear app is targeting 22. - // - If Wear is running M (ie E-release), use new permission model. - // Case 4: Companion App >= 23 (and running on M), Wear App targeting <= 22 - // - Show a warning below to the developer. - // - Show a dialog as in Case 1 with DMR1. This behavior will happen in E and DMR1. - // Case 5: We did not get Companion App's/Device's version (we have to guess here) - // - Show dialog if Wear App targeting >= 23 and Wear is not running M - if (unavailableWearablePerms.size() > 0) { - boolean isCompanionTargetingM = companionSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; - boolean isCompanionRunningM = companionDeviceVersion > Build.VERSION_CODES.LOLLIPOP_MR1; - boolean isWearTargetingM = - pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; - boolean isWearRunningM = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; - - if (companionSdkVersion == 0 || companionDeviceVersion == 0) { // Case 5 - if (isWearTargetingM && !isWearRunningM) { - startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); - } - } else if (isCompanionTargetingM && isCompanionRunningM) { - if (!isWearTargetingM) { // Case 4 - Log.w(TAG, "MNC: Wear app's targetSdkVersion should be at least 23, if phone " + - "app is targeting at least 23."); - startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); - } else if (!isWearRunningM) { // Case 1, part 1 - startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); - } - } // Else, nothing to do. See explanation above. - } - - return unavailableWearablePerms.size() == 0; - } - - private void finishService(PowerManager.WakeLock lock, int startId) { - if (lock.isHeld()) { - lock.release(); - } - stopSelf(startId); - } - - private synchronized PowerManager.WakeLock getLock(Context context) { - if (lockStatic == null) { - PowerManager mgr = - (PowerManager) context.getSystemService(Context.POWER_SERVICE); - lockStatic = mgr.newWakeLock( - PowerManager.PARTIAL_WAKE_LOCK, context.getClass().getSimpleName()); - lockStatic.setReferenceCounted(true); - } - return lockStatic; - } - - private void startPermsServiceForInstall(final PackageParser.Package pkg, final File apkFile, - ArrayList unavailableWearablePerms) { - final String packageName = pkg.packageName; - - Intent showPermsIntent = new Intent() - .setComponent(new ComponentName(HOME_APP_PACKAGE_NAME, SHOW_PERMS_SERVICE_CLASS)) - .setAction(Intent.ACTION_INSTALL_PACKAGE); - final PackageManager pm = getPackageManager(); - pkg.applicationInfo.publicSourceDir = apkFile.getPath(); - final CharSequence label = pkg.applicationInfo.loadLabel(pm); - final Uri iconUri = getIconFileUri(packageName, pkg.applicationInfo.loadIcon(pm)); - if (TextUtils.isEmpty(label) || iconUri == null) { - Log.e(TAG, "MNC: Could not launch service since either label " + label + - ", or icon Uri " + iconUri + " is invalid."); - } else { - showPermsIntent.putExtra(KEY_APP_LABEL, label); - showPermsIntent.putExtra(KEY_APP_ICON_URI, iconUri); - showPermsIntent.putExtra(KEY_PACKAGE_NAME, packageName); - showPermsIntent.putExtra(KEY_PERMS_LIST, - unavailableWearablePerms.toArray(new String[0])); - showPermsIntent.putExtra(KEY_HAS_LAUNCHER, WearPackageUtil.hasLauncherActivity(pkg)); - - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "MNC: Launching Intent " + showPermsIntent + " for " + packageName + - " with name " + label); - } - startService(showPermsIntent); - } - } - - private void startPermsServiceForUninstall(final String packageName) { - Intent showPermsIntent = new Intent() - .setComponent(new ComponentName(HOME_APP_PACKAGE_NAME, SHOW_PERMS_SERVICE_CLASS)) - .setAction(Intent.ACTION_UNINSTALL_PACKAGE); - showPermsIntent.putExtra(KEY_PACKAGE_NAME, packageName); - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Launching Intent " + showPermsIntent + " for " + packageName); - } - startService(showPermsIntent); - } - - private Uri getIconFileUri(final String packageName, final Drawable d) { - if (d == null || !(d instanceof BitmapDrawable)) { - Log.e(TAG, "Drawable is not a BitmapDrawable for " + packageName); - return null; - } - File iconFile = WearPackageUtil.getIconFile(this, packageName); - - if (iconFile == null) { - Log.e(TAG, "Could not get icon file for " + packageName); - return null; - } - - FileOutputStream fos = null; - try { - // Convert bitmap to byte array - Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); - - // Write the bytes into the file - fos = new FileOutputStream(iconFile); - fos.write(bos.toByteArray()); - fos.flush(); - - return WearPackageIconProvider.getUriForPackage(packageName); - } catch (IOException e) { - Log.e(TAG, "Could not convert drawable to icon file for package " + packageName, e); - return null; - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - // ignore - } - } - } - } - - private class PackageInstallObserver extends IPackageInstallObserver.Stub { - private Context mContext; - private PowerManager.WakeLock mWakeLock; - private int mStartId; - private PackageInstallObserver(Context context, PowerManager.WakeLock wakeLock, - int startId) { - mContext = context; - mWakeLock = wakeLock; - mStartId = startId; - } - - public void packageInstalled(String packageName, int returnCode) { - if (returnCode >= 0) { - Log.i(TAG, "Package " + packageName + " was installed."); - } else { - Log.e(TAG, "Package install failed " + packageName + ", returnCode " + returnCode); - } - - // Delete tempFile from the file system. - File tempFile = WearPackageUtil.getTemporaryFile(mContext, packageName); - if (tempFile != null) { - tempFile.delete(); - } - - // Broadcast the "UPDATED" gmscore intent, normally sent by play store. - // TODO: Remove this broadcast if/when we get the play store to do this for us. - if (GMS_PACKAGE_NAME.equals(packageName)) { - Intent gmsInstalledIntent = new Intent(GMS_UPDATED_BROADCAST); - gmsInstalledIntent.setPackage(GMS_PACKAGE_NAME); - mContext.sendBroadcast(gmsInstalledIntent); - } - - finishService(mWakeLock, mStartId); - } - } - - private class PackageDeleteObserver extends IPackageDeleteObserver.Stub { - private PowerManager.WakeLock mWakeLock; - private int mStartId; - - private PackageDeleteObserver(PowerManager.WakeLock wakeLock, int startId) { - mWakeLock = wakeLock; - mStartId = startId; - } - - public void packageDeleted(String packageName, int returnCode) { - if (returnCode >= 0) { - Log.i(TAG, "Package " + packageName + " was uninstalled."); - } else { - Log.e(TAG, "Package uninstall failed " + packageName + ", returnCode " + - returnCode); - } - finishService(mWakeLock, mStartId); - } - } -} diff --git a/src/com/android/packageinstaller/wear/WearPackageUtil.java b/src/com/android/packageinstaller/wear/WearPackageUtil.java deleted file mode 100644 index bec697d9..00000000 --- a/src/com/android/packageinstaller/wear/WearPackageUtil.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.wear; - -import android.annotation.Nullable; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.PackageParser; -import android.os.ParcelFileDescriptor; -import android.system.ErrnoException; -import android.system.Os; -import android.text.TextUtils; -import android.util.Log; - -import org.tukaani.xz.LZMAInputStream; -import org.tukaani.xz.XZInputStream; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; - -public class WearPackageUtil { - private static final String TAG = "WearablePkgInstaller"; - - private static final String COMPRESSION_LZMA = "lzma"; - private static final String COMPRESSION_XZ = "xz"; - - public static File getTemporaryFile(Context context, String packageName) { - try { - File newFileDir = new File(context.getFilesDir(), "tmp"); - newFileDir.mkdirs(); - Os.chmod(newFileDir.getAbsolutePath(), 0771); - File newFile = new File(newFileDir, packageName + ".apk"); - return newFile; - } catch (ErrnoException e) { - Log.e(TAG, "Failed to open.", e); - return null; - } - } - - public static File getIconFile(final Context context, final String packageName) { - try { - File newFileDir = new File(context.getFilesDir(), "images/icons"); - newFileDir.mkdirs(); - Os.chmod(newFileDir.getAbsolutePath(), 0771); - return new File(newFileDir, packageName + ".icon"); - } catch (ErrnoException e) { - Log.e(TAG, "Failed to open.", e); - return null; - } - } - - /** - * In order to make sure that the Wearable Asset Manager has a reasonable apk that can be used - * by the PackageManager, we will parse it before sending it to the PackageManager. - * Unfortunately, PackageParser needs a file to parse. So, we have to temporarily convert the fd - * to a File. - * - * @param context - * @param fd FileDescriptor to convert to File - * @param packageName Name of package, will define the name of the file - * @param compressionAlg Can be null. For ALT mode the APK will be compressed. We will - * decompress it here - */ - public static File getFileFromFd(Context context, ParcelFileDescriptor fd, - String packageName, @Nullable String compressionAlg) { - File newFile = getTemporaryFile(context, packageName); - if (fd == null || fd.getFileDescriptor() == null) { - return null; - } - InputStream fr = new ParcelFileDescriptor.AutoCloseInputStream(fd); - try { - if (TextUtils.equals(compressionAlg, COMPRESSION_XZ)) { - fr = new XZInputStream(fr); - } else if (TextUtils.equals(compressionAlg, COMPRESSION_LZMA)) { - fr = new LZMAInputStream(fr); - } - } catch (IOException e) { - Log.e(TAG, "Compression was set to " + compressionAlg + ", but could not decode ", e); - return null; - } - - int nRead; - byte[] data = new byte[1024]; - try { - final FileOutputStream fo = new FileOutputStream(newFile); - while ((nRead = fr.read(data, 0, data.length)) != -1) { - fo.write(data, 0, nRead); - } - fo.flush(); - fo.close(); - Os.chmod(newFile.getAbsolutePath(), 0644); - return newFile; - } catch (IOException e) { - Log.e(TAG, "Reading from Asset FD or writing to temp file failed ", e); - return null; - } catch (ErrnoException e) { - Log.e(TAG, "Could not set permissions on file ", e); - return null; - } finally { - try { - fr.close(); - } catch (IOException e) { - Log.e(TAG, "Failed to close the file from FD ", e); - } - } - } - - public static boolean hasLauncherActivity(PackageParser.Package pkg) { - if (pkg == null || pkg.activities == null) { - return false; - } - - final int activityCount = pkg.activities.size(); - for (int i = 0; i < activityCount; ++i) { - if (pkg.activities.get(i).intents != null) { - ArrayList intents = - pkg.activities.get(i).intents; - final int intentsCount = intents.size(); - for (int j = 0; j < intentsCount; ++j) { - final PackageParser.ActivityIntentInfo intentInfo = intents.get(j); - if (intentInfo.hasAction(Intent.ACTION_MAIN)) { - if (intentInfo.hasCategory(Intent.CATEGORY_INFO) || - intentInfo .hasCategory(Intent.CATEGORY_LAUNCHER)) { - return true; - } - } - } - } - } - return false; - } - - public static boolean isWear(final Context context) { - return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); - } -} -- cgit v1.2.3 From 46fed54b3ee49e4bebabca50bcff5bceaece96b8 Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Wed, 9 Sep 2015 00:59:53 +0000 Subject: Redo Move ClockworkPackageInstaller functionality here" This reverts commit 5b26c2e64ceeaa1e71436b36a28f53100e358ed8. Change-Id: I88fa09f87023a7c1b2aac3100cfbdce6283de770 --- .../wear/WearPackageIconProvider.java | 202 +++++++ .../wear/WearPackageInstallerService.java | 608 +++++++++++++++++++++ .../packageinstaller/wear/WearPackageUtil.java | 154 ++++++ 3 files changed, 964 insertions(+) create mode 100644 src/com/android/packageinstaller/wear/WearPackageIconProvider.java create mode 100644 src/com/android/packageinstaller/wear/WearPackageInstallerService.java create mode 100644 src/com/android/packageinstaller/wear/WearPackageUtil.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageIconProvider.java b/src/com/android/packageinstaller/wear/WearPackageIconProvider.java new file mode 100644 index 00000000..02b9d298 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageIconProvider.java @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.annotation.TargetApi; +import android.app.ActivityManager; +import android.content.ContentProvider; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.net.Uri; +import android.os.Binder; +import android.os.Build; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.List; + +import static android.content.pm.PackageManager.PERMISSION_GRANTED; + +public class WearPackageIconProvider extends ContentProvider { + private static final String TAG = "WearPackageIconProvider"; + public static final String AUTHORITY = "com.google.android.packageinstaller.wear.provider"; + + private static final String REQUIRED_PERMISSION = + "com.google.android.permission.INSTALL_WEARABLE_PACKAGES"; + + /** MIME types. */ + public static final String ICON_TYPE = "vnd.android.cursor.item/cw_package_icon"; + + @Override + public boolean onCreate() { + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + throw new UnsupportedOperationException("Query is not supported."); + } + + @Override + public String getType(Uri uri) { + if (uri == null) { + throw new IllegalArgumentException("URI passed in is null."); + } + + if (AUTHORITY.equals(uri.getEncodedAuthority())) { + return ICON_TYPE; + } + return null; + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + throw new UnsupportedOperationException("Insert is not supported."); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + if (uri == null) { + throw new IllegalArgumentException("URI passed in is null."); + } + + enforcePermissions(uri); + + if (ICON_TYPE.equals(getType(uri))) { + final File file = WearPackageUtil.getIconFile( + this.getContext().getApplicationContext(), getPackageNameFromUri(uri)); + if (file != null) { + file.delete(); + } + } + + return 0; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException("Update is not supported."); + } + + @Override + public ParcelFileDescriptor openFile( + Uri uri, @SuppressWarnings("unused") String mode) throws FileNotFoundException { + if (uri == null) { + throw new IllegalArgumentException("URI passed in is null."); + } + + enforcePermissions(uri); + + if (ICON_TYPE.equals(getType(uri))) { + final File file = WearPackageUtil.getIconFile( + this.getContext().getApplicationContext(), getPackageNameFromUri(uri)); + if (file != null) { + return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); + } + } + return null; + } + + public static Uri getUriForPackage(final String packageName) { + return Uri.parse("content://" + AUTHORITY + "/icons/" + packageName + ".icon"); + } + + private String getPackageNameFromUri(Uri uri) { + if (uri == null) { + return null; + } + List pathSegments = uri.getPathSegments(); + String packageName = pathSegments.get(pathSegments.size() - 1); + + if (packageName.endsWith(".icon")) { + packageName = packageName.substring(0, packageName.lastIndexOf(".")); + } + return packageName; + } + + /** + * Make sure the calling app is either a system app or the same app or has the right permission. + * @throws SecurityException if the caller has insufficient permissions. + */ + @TargetApi(Build.VERSION_CODES.BASE_1_1) + private void enforcePermissions(Uri uri) { + // Redo some of the permission check in {@link ContentProvider}. Just add an extra check to + // allow System process to access this provider. + Context context = getContext(); + final int pid = Binder.getCallingPid(); + final int uid = Binder.getCallingUid(); + final int myUid = android.os.Process.myUid(); + + if (uid == myUid || isSystemApp(context, pid)) { + return; + } + + if (context.checkPermission(REQUIRED_PERMISSION, pid, uid) == PERMISSION_GRANTED) { + return; + } + + // last chance, check against any uri grants + if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION) + == PERMISSION_GRANTED) { + return; + } + + throw new SecurityException("Permission Denial: reading " + + getClass().getName() + " uri " + uri + " from pid=" + pid + + ", uid=" + uid); + } + + /** + * From the pid of the calling process, figure out whether this is a system app or not. We do + * this by checking the application information corresponding to the pid and then checking if + * FLAG_SYSTEM is set. + */ + @TargetApi(Build.VERSION_CODES.CUPCAKE) + private boolean isSystemApp(Context context, int pid) { + // Get the Activity Manager Object + ActivityManager aManager = + (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + // Get the list of running Applications + List rapInfoList = + aManager.getRunningAppProcesses(); + for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) { + if (rapInfo.pid == pid) { + try { + PackageInfo pkgInfo = context.getPackageManager().getPackageInfo( + rapInfo.pkgList[0], 0); + if (pkgInfo != null && pkgInfo.applicationInfo != null && + (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { + Log.d(TAG, pid + " is a system app."); + return true; + } + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Could not find package information.", e); + return false; + } + } + } + return false; + } +} diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java new file mode 100644 index 00000000..e9ea21e1 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -0,0 +1,608 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.annotation.Nullable; +import android.app.Service; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.FeatureInfo; +import android.content.pm.IPackageDeleteObserver; +import android.content.pm.IPackageInstallObserver; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageParser; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; +import android.os.ParcelFileDescriptor; +import android.os.PowerManager; +import android.os.Process; +import android.text.TextUtils; +import android.util.Log; + +import com.android.packageinstaller.PackageUtil; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Service that will install/uninstall packages. It will check for permissions and features as well. + * + * ----------- + * + * Debugging information: + * + * Install Action example: + * adb shell am startservice -a com.android.packageinstaller.wear.INSTALL_PACKAGE \ + * -t vnd.android.cursor.item/wearable_apk \ + * -d content://com.google.android.clockwork.home.provider/host/com.google.android.wearable.app/wearable/com.google.android.gms/apk \ + * --es android.intent.extra.INSTALLER_PACKAGE_NAME com.google.android.gms \ + * --ez com.google.android.clockwork.EXTRA_CHECK_PERMS false \ + * --eu com.google.android.clockwork.EXTRA_PERM_URI content://com.google.android.clockwork.home.provider/host/com.google.android.wearable.app/permissions \ + * com.android.packageinstaller/com.android.packageinstaller.wear.WearPackageInstallerService + * + * Retry GMS: + * adb shell am startservice -a com.android.packageinstaller.wear.RETRY_GMS \ + * com.android.packageinstaller/com.android.packageinstaller.wear.WearPackageInstallerService + */ +public class WearPackageInstallerService extends Service { + private static final String TAG = "WearPkgInstallerService"; + + private static final String KEY_PERM_URI = + "com.google.android.clockwork.EXTRA_PERM_URI"; + private static final String KEY_CHECK_PERMS = + "com.google.android.clockwork.EXTRA_CHECK_PERMS"; + private static final String KEY_SKIP_IF_SAME_VERSION = + "com.google.android.clockwork.EXTRA_SKIP_IF_SAME_VERSION"; + private static final String KEY_COMPRESSION_ALG = + "com.google.android.clockwork.EXTRA_KEY_COMPRESSION_ALG"; + private static final String KEY_COMPANION_SDK_VERSION = + "com.google.android.clockwork.EXTRA_KEY_COMPANION_SDK_VERSION"; + private static final String KEY_COMPANION_DEVICE_VERSION = + "com.google.android.clockwork.EXTRA_KEY_COMPANION_DEVICE_VERSION"; + + private static final String KEY_PACKAGE_NAME = + "com.google.android.clockwork.EXTRA_PACKAGE_NAME"; + private static final String KEY_APP_LABEL = "com.google.android.clockwork.EXTRA_APP_LABEL"; + private static final String KEY_APP_ICON_URI = + "com.google.android.clockwork.EXTRA_APP_ICON_URI"; + private static final String KEY_PERMS_LIST = "com.google.android.clockwork.EXTRA_PERMS_LIST"; + private static final String KEY_HAS_LAUNCHER = + "com.google.android.clockwork.EXTRA_HAS_LAUNCHER"; + + private static final String HOME_APP_PACKAGE_NAME = "com.google.android.wearable.app"; + private static final String SHOW_PERMS_SERVICE_CLASS = + "com.google.android.clockwork.packagemanager.ShowPermsService"; + + private static final String ASSET_URI_ARG = "assetUri"; + private static final String PACKAGE_NAME_ARG = "packageName"; + private static final String PERM_URI_ARG = "permUri"; + private static final String START_ID_ARG = "startId"; + private static final String CHECK_PERMS_ARG = "checkPerms"; + private static final String SKIP_IF_SAME_VERSION_ARG = "skipIfSameVersion"; + private static final String COMPRESSION_ALG = "compressionAlg"; + private static final String COMPANION_SDK_VERSION = "companionSdkVersion"; + private static final String COMPANION_DEVICE_VERSION = "companionDeviceVersion"; + + /** + * Normally sent by the Play store (See http://go/playstore-gms_updated), we instead + * broadcast, ourselves. http://b/17387718 + */ + private static final String GMS_UPDATED_BROADCAST = "com.google.android.gms.GMS_UPDATED"; + public static final String GMS_PACKAGE_NAME = "com.google.android.gms"; + + private final int START_INSTALL = 1; + private final int START_UNINSTALL = 2; + + private final class ServiceHandler extends Handler { + public ServiceHandler(Looper looper) { + super(looper); + } + + public void handleMessage(Message msg) { + switch (msg.what) { + case START_INSTALL: + installPackage(msg.getData().getString(PACKAGE_NAME_ARG), + (Uri) msg.getData().getParcelable(ASSET_URI_ARG), + (Uri) msg.getData().getParcelable(PERM_URI_ARG), + msg.getData().getInt(START_ID_ARG), + msg.getData().getBoolean(CHECK_PERMS_ARG), + msg.getData().getBoolean(SKIP_IF_SAME_VERSION_ARG), + msg.getData().getString(COMPRESSION_ALG), + msg.getData().getInt(COMPANION_SDK_VERSION), + msg.getData().getInt(COMPANION_DEVICE_VERSION)); + break; + case START_UNINSTALL: + uninstallPackage(msg.getData().getString(PACKAGE_NAME_ARG), + msg.getData().getInt(START_ID_ARG)); + break; + } + } + } + private ServiceHandler mServiceHandler; + + private static volatile PowerManager.WakeLock lockStatic = null; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + super.onCreate(); + HandlerThread thread = new HandlerThread("PackageInstallerThread", + Process.THREAD_PRIORITY_BACKGROUND); + thread.start(); + + mServiceHandler = new ServiceHandler(thread.getLooper()); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (!WearPackageUtil.isWear(this)) { + Log.w(TAG, "Not running on wearable"); + return START_NOT_STICKY; + } + PowerManager.WakeLock lock = getLock(this.getApplicationContext()); + if (!lock.isHeld()) { + lock.acquire(); + } + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Got install/uninstall request " + intent); + } + if (intent != null) { + if (Intent.ACTION_INSTALL_PACKAGE.equals(intent.getAction())) { + final Message msg = mServiceHandler.obtainMessage(START_INSTALL); + final Bundle startInstallArgs = new Bundle(); + startInstallArgs.putParcelable(ASSET_URI_ARG, intent.getData()); + startInstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( + Intent.EXTRA_INSTALLER_PACKAGE_NAME)); + startInstallArgs.putInt(START_ID_ARG, startId); + Uri permUri = intent.getParcelableExtra(KEY_PERM_URI); + startInstallArgs.putParcelable(PERM_URI_ARG, permUri); + startInstallArgs.putBoolean(CHECK_PERMS_ARG, + intent.getBooleanExtra(KEY_CHECK_PERMS, true)); + startInstallArgs.putBoolean(SKIP_IF_SAME_VERSION_ARG, + intent.getBooleanExtra(KEY_SKIP_IF_SAME_VERSION, false)); + startInstallArgs.putString(COMPRESSION_ALG, + intent.getStringExtra(KEY_COMPRESSION_ALG)); + startInstallArgs.putInt(COMPANION_SDK_VERSION, + intent.getIntExtra(KEY_COMPANION_SDK_VERSION, 0)); + startInstallArgs.putInt(COMPANION_DEVICE_VERSION, + intent.getIntExtra(KEY_COMPANION_DEVICE_VERSION, 0)); + msg.setData(startInstallArgs); + mServiceHandler.sendMessage(msg); + } else if (Intent.ACTION_UNINSTALL_PACKAGE.equals(intent.getAction())) { + Message msg = mServiceHandler.obtainMessage(START_UNINSTALL); + Bundle startUninstallArgs = new Bundle(); + startUninstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( + Intent.EXTRA_INSTALLER_PACKAGE_NAME)); + startUninstallArgs.putInt(START_ID_ARG, startId); + msg.setData(startUninstallArgs); + mServiceHandler.sendMessage(msg); + } + } + return START_NOT_STICKY; + } + + private void installPackage(String packageName, Uri packageUri, Uri permUri, int startId, + boolean checkPerms, boolean skipIfSameVersion, @Nullable String compressionAlg, + int companionSdkVersion, int companionDeviceVersion) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Installing package: " + packageName + ", packageUri: " + packageUri + + ",permUri: " + permUri + ", startId: " + startId + ", checkPerms: " + + checkPerms + ", skipIfSameVersion: " + skipIfSameVersion + + ", compressionAlg: " + compressionAlg + ", companionSdkVersion: " + + companionSdkVersion + ", companionDeviceVersion: " + companionDeviceVersion); + } + final PackageManager pm = getPackageManager(); + File tempFile = null; + int installFlags = 0; + PowerManager.WakeLock lock = getLock(this.getApplicationContext()); + boolean messageSent = false; + try { + PackageInfo existingPkgInfo = null; + try { + existingPkgInfo = pm.getPackageInfo(packageName, + PackageManager.GET_UNINSTALLED_PACKAGES); + if(existingPkgInfo != null) { + installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; + } + } catch (PackageManager.NameNotFoundException e) { + // Ignore this exception. We could not find the package, will treat as a new + // installation. + } + if((installFlags & PackageManager.INSTALL_REPLACE_EXISTING )!= 0) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Replacing package:" + packageName); + } + } + ParcelFileDescriptor parcelFd = getContentResolver() + .openFileDescriptor(packageUri, "r"); + tempFile = WearPackageUtil.getFileFromFd(WearPackageInstallerService.this, + parcelFd, packageName, compressionAlg); + if (tempFile == null) { + Log.e(TAG, "Could not create a temp file from FD for " + packageName); + return; + } + PackageParser.Package pkg = PackageUtil.getPackageInfo(tempFile); + if (pkg == null) { + Log.e(TAG, "Could not parse apk information for " + packageName); + return; + } + + if (!pkg.packageName.equals(packageName)) { + Log.e(TAG, "Wearable Package Name has to match what is provided for " + + packageName); + return; + } + + List wearablePerms = pkg.requestedPermissions; + + // Log if the installed pkg has a higher version number. + if (existingPkgInfo != null) { + if (existingPkgInfo.versionCode == pkg.mVersionCode) { + if (skipIfSameVersion) { + Log.w(TAG, "Version number (" + pkg.mVersionCode + + ") of new app is equal to existing app for " + packageName + + "; not installing due to versionCheck"); + return; + } else { + Log.w(TAG, "Version number of new app (" + pkg.mVersionCode + + ") is equal to existing app for " + packageName); + } + } else if (existingPkgInfo.versionCode > pkg.mVersionCode) { + Log.w(TAG, "Version number of new app (" + pkg.mVersionCode + + ") is lower than existing app ( " + existingPkgInfo.versionCode + + ") for " + packageName); + } + + // Following the Android Phone model, we should only check for permissions for any + // newly defined perms. + if (existingPkgInfo.requestedPermissions != null) { + for (int i = 0; i < existingPkgInfo.requestedPermissions.length; ++i) { + // If the permission is granted, then we will not ask to request it again. + if ((existingPkgInfo.requestedPermissionsFlags[i] & + PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) { + wearablePerms.remove(existingPkgInfo.requestedPermissions[i]); + } + } + } + } + + // Check permissions on both the new wearable package and also on the already installed + // wearable package. + // If the app is targeting API level 23, we will also start a service in ClockworkHome + // which will ultimately prompt the user to accept/reject permissions. + if (checkPerms && !checkPermissions(pkg, companionSdkVersion, companionDeviceVersion, + permUri, wearablePerms, tempFile)) { + Log.w(TAG, "Wearable does not have enough permissions."); + return; + } + + // Check that the wearable has all the features. + boolean hasAllFeatures = true; + if (pkg.reqFeatures != null) { + for (FeatureInfo feature : pkg.reqFeatures) { + if (feature.name != null && !pm.hasSystemFeature(feature.name) && + (feature.flags & FeatureInfo.FLAG_REQUIRED) != 0) { + Log.e(TAG, "Wearable does not have required feature: " + feature + + " for " + packageName); + hasAllFeatures = false; + } + } + } + + if (!hasAllFeatures) { + return; + } + + // Finally install the package. + pm.installPackage(Uri.fromFile(tempFile), + new PackageInstallObserver(this, lock, startId), installFlags, packageName); + messageSent = true; + Log.i(TAG, "Sent installation request for " + packageName); + } catch (FileNotFoundException e) { + Log.e(TAG, "Could not find the file with URI " + packageUri, e); + } finally { + if (!messageSent) { + // Some error happened. If the message has been sent, we can wait for the observer + // which will finish the service. + if (tempFile != null) { + tempFile.delete(); + } + finishService(lock, startId); + } + } + } + + private void uninstallPackage(String packageName, int startId) { + final PackageManager pm = getPackageManager(); + PowerManager.WakeLock lock = getLock(this.getApplicationContext()); + pm.deletePackage(packageName, new PackageDeleteObserver(lock, startId), + PackageManager.DELETE_ALL_USERS); + startPermsServiceForUninstall(packageName); + Log.i(TAG, "Sent delete request for " + packageName); + } + + private boolean checkPermissions(PackageParser.Package pkg, int companionSdkVersion, + int companionDeviceVersion, Uri permUri, List wearablePermissions, + File apkFile) { + if (permUri == null) { + Log.e(TAG, "Permission URI is null"); + return false; + } + Cursor permCursor = getContentResolver().query(permUri, null, null, null, null); + if (permCursor == null) { + Log.e(TAG, "Could not get the cursor for the permissions"); + return false; + } + + final String packageName = pkg.packageName; + + Set grantedPerms = new HashSet<>(); + Set ungrantedPerms = new HashSet<>(); + while(permCursor.moveToNext()) { + // Make sure that the MatrixCursor returned by the ContentProvider has 2 columns and + // verify their types. + if (permCursor.getColumnCount() == 2 + && Cursor.FIELD_TYPE_STRING == permCursor.getType(0) + && Cursor.FIELD_TYPE_INTEGER == permCursor.getType(1)) { + String perm = permCursor.getString(0); + Integer granted = permCursor.getInt(1); + if (granted == 1) { + grantedPerms.add(perm); + } else { + ungrantedPerms.add(perm); + } + } + } + permCursor.close(); + + ArrayList unavailableWearablePerms = new ArrayList<>(); + for (String wearablePerm : wearablePermissions) { + if (!grantedPerms.contains(wearablePerm)) { + unavailableWearablePerms.add(wearablePerm); + if (!ungrantedPerms.contains(wearablePerm)) { + // This is an error condition. This means that the wearable has permissions that + // are not even declared in its host app. This is a developer error. + Log.e(TAG, "Wearable " + packageName + " has a permission \"" + wearablePerm + + "\" that is not defined in the host application's manifest."); + } else { + Log.w(TAG, "Wearable " + packageName + " has a permission \"" + wearablePerm + + "\" that is not granted in the host application."); + } + } + } + + + // If the Wear App is targeted for M-release, since the permission model has been changed, + // permissions may not be granted on the phone yet. We need a different flow for user to + // accept these permissions. + // + // Case 1: Companion App >= 23 (and running on M), Wear App targeting >= 23 + // - If Wear is running L (ie DMR1), show a dialog so that the user can accept all perms + // - If Wear is running M (ie E-release), use new permission model. + // Case 2: Companion App <= 22, Wear App targeting <= 22 + // - Default to old behavior. + // Case 3: Companion App <= 22, Wear App targeting >= 23 + // - If Wear is running L (ie DMR1), install the app as before. In effect, pretend + // like wear app is targeting 22. + // - If Wear is running M (ie E-release), use new permission model. + // Case 4: Companion App >= 23 (and running on M), Wear App targeting <= 22 + // - Show a warning below to the developer. + // - Show a dialog as in Case 1 with DMR1. This behavior will happen in E and DMR1. + // Case 5: We did not get Companion App's/Device's version (we have to guess here) + // - Show dialog if Wear App targeting >= 23 and Wear is not running M + if (unavailableWearablePerms.size() > 0) { + boolean isCompanionTargetingM = companionSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isCompanionRunningM = companionDeviceVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isWearTargetingM = + pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isWearRunningM = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; + + if (companionSdkVersion == 0 || companionDeviceVersion == 0) { // Case 5 + if (isWearTargetingM && !isWearRunningM) { + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } + } else if (isCompanionTargetingM && isCompanionRunningM) { + if (!isWearTargetingM) { // Case 4 + Log.w(TAG, "MNC: Wear app's targetSdkVersion should be at least 23, if phone " + + "app is targeting at least 23."); + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } else if (!isWearRunningM) { // Case 1, part 1 + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } + } // Else, nothing to do. See explanation above. + } + + return unavailableWearablePerms.size() == 0; + } + + private void finishService(PowerManager.WakeLock lock, int startId) { + if (lock.isHeld()) { + lock.release(); + } + stopSelf(startId); + } + + private synchronized PowerManager.WakeLock getLock(Context context) { + if (lockStatic == null) { + PowerManager mgr = + (PowerManager) context.getSystemService(Context.POWER_SERVICE); + lockStatic = mgr.newWakeLock( + PowerManager.PARTIAL_WAKE_LOCK, context.getClass().getSimpleName()); + lockStatic.setReferenceCounted(true); + } + return lockStatic; + } + + private void startPermsServiceForInstall(final PackageParser.Package pkg, final File apkFile, + ArrayList unavailableWearablePerms) { + final String packageName = pkg.packageName; + + Intent showPermsIntent = new Intent() + .setComponent(new ComponentName(HOME_APP_PACKAGE_NAME, SHOW_PERMS_SERVICE_CLASS)) + .setAction(Intent.ACTION_INSTALL_PACKAGE); + final PackageManager pm = getPackageManager(); + pkg.applicationInfo.publicSourceDir = apkFile.getPath(); + final CharSequence label = pkg.applicationInfo.loadLabel(pm); + final Uri iconUri = getIconFileUri(packageName, pkg.applicationInfo.loadIcon(pm)); + if (TextUtils.isEmpty(label) || iconUri == null) { + Log.e(TAG, "MNC: Could not launch service since either label " + label + + ", or icon Uri " + iconUri + " is invalid."); + } else { + showPermsIntent.putExtra(KEY_APP_LABEL, label); + showPermsIntent.putExtra(KEY_APP_ICON_URI, iconUri); + showPermsIntent.putExtra(KEY_PACKAGE_NAME, packageName); + showPermsIntent.putExtra(KEY_PERMS_LIST, + unavailableWearablePerms.toArray(new String[0])); + showPermsIntent.putExtra(KEY_HAS_LAUNCHER, WearPackageUtil.hasLauncherActivity(pkg)); + + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "MNC: Launching Intent " + showPermsIntent + " for " + packageName + + " with name " + label); + } + startService(showPermsIntent); + } + } + + private void startPermsServiceForUninstall(final String packageName) { + Intent showPermsIntent = new Intent() + .setComponent(new ComponentName(HOME_APP_PACKAGE_NAME, SHOW_PERMS_SERVICE_CLASS)) + .setAction(Intent.ACTION_UNINSTALL_PACKAGE); + showPermsIntent.putExtra(KEY_PACKAGE_NAME, packageName); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Launching Intent " + showPermsIntent + " for " + packageName); + } + startService(showPermsIntent); + } + + private Uri getIconFileUri(final String packageName, final Drawable d) { + if (d == null || !(d instanceof BitmapDrawable)) { + Log.e(TAG, "Drawable is not a BitmapDrawable for " + packageName); + return null; + } + File iconFile = WearPackageUtil.getIconFile(this, packageName); + + if (iconFile == null) { + Log.e(TAG, "Could not get icon file for " + packageName); + return null; + } + + FileOutputStream fos = null; + try { + // Convert bitmap to byte array + Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); + + // Write the bytes into the file + fos = new FileOutputStream(iconFile); + fos.write(bos.toByteArray()); + fos.flush(); + + return WearPackageIconProvider.getUriForPackage(packageName); + } catch (IOException e) { + Log.e(TAG, "Could not convert drawable to icon file for package " + packageName, e); + return null; + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + // ignore + } + } + } + } + + private class PackageInstallObserver extends IPackageInstallObserver.Stub { + private Context mContext; + private PowerManager.WakeLock mWakeLock; + private int mStartId; + private PackageInstallObserver(Context context, PowerManager.WakeLock wakeLock, + int startId) { + mContext = context; + mWakeLock = wakeLock; + mStartId = startId; + } + + public void packageInstalled(String packageName, int returnCode) { + if (returnCode >= 0) { + Log.i(TAG, "Package " + packageName + " was installed."); + } else { + Log.e(TAG, "Package install failed " + packageName + ", returnCode " + returnCode); + } + + // Delete tempFile from the file system. + File tempFile = WearPackageUtil.getTemporaryFile(mContext, packageName); + if (tempFile != null) { + tempFile.delete(); + } + + // Broadcast the "UPDATED" gmscore intent, normally sent by play store. + // TODO: Remove this broadcast if/when we get the play store to do this for us. + if (GMS_PACKAGE_NAME.equals(packageName)) { + Intent gmsInstalledIntent = new Intent(GMS_UPDATED_BROADCAST); + gmsInstalledIntent.setPackage(GMS_PACKAGE_NAME); + mContext.sendBroadcast(gmsInstalledIntent); + } + + finishService(mWakeLock, mStartId); + } + } + + private class PackageDeleteObserver extends IPackageDeleteObserver.Stub { + private PowerManager.WakeLock mWakeLock; + private int mStartId; + + private PackageDeleteObserver(PowerManager.WakeLock wakeLock, int startId) { + mWakeLock = wakeLock; + mStartId = startId; + } + + public void packageDeleted(String packageName, int returnCode) { + if (returnCode >= 0) { + Log.i(TAG, "Package " + packageName + " was uninstalled."); + } else { + Log.e(TAG, "Package uninstall failed " + packageName + ", returnCode " + + returnCode); + } + finishService(mWakeLock, mStartId); + } + } +} diff --git a/src/com/android/packageinstaller/wear/WearPackageUtil.java b/src/com/android/packageinstaller/wear/WearPackageUtil.java new file mode 100644 index 00000000..bec697d9 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageUtil.java @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.annotation.Nullable; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.PackageParser; +import android.os.ParcelFileDescriptor; +import android.system.ErrnoException; +import android.system.Os; +import android.text.TextUtils; +import android.util.Log; + +import org.tukaani.xz.LZMAInputStream; +import org.tukaani.xz.XZInputStream; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +public class WearPackageUtil { + private static final String TAG = "WearablePkgInstaller"; + + private static final String COMPRESSION_LZMA = "lzma"; + private static final String COMPRESSION_XZ = "xz"; + + public static File getTemporaryFile(Context context, String packageName) { + try { + File newFileDir = new File(context.getFilesDir(), "tmp"); + newFileDir.mkdirs(); + Os.chmod(newFileDir.getAbsolutePath(), 0771); + File newFile = new File(newFileDir, packageName + ".apk"); + return newFile; + } catch (ErrnoException e) { + Log.e(TAG, "Failed to open.", e); + return null; + } + } + + public static File getIconFile(final Context context, final String packageName) { + try { + File newFileDir = new File(context.getFilesDir(), "images/icons"); + newFileDir.mkdirs(); + Os.chmod(newFileDir.getAbsolutePath(), 0771); + return new File(newFileDir, packageName + ".icon"); + } catch (ErrnoException e) { + Log.e(TAG, "Failed to open.", e); + return null; + } + } + + /** + * In order to make sure that the Wearable Asset Manager has a reasonable apk that can be used + * by the PackageManager, we will parse it before sending it to the PackageManager. + * Unfortunately, PackageParser needs a file to parse. So, we have to temporarily convert the fd + * to a File. + * + * @param context + * @param fd FileDescriptor to convert to File + * @param packageName Name of package, will define the name of the file + * @param compressionAlg Can be null. For ALT mode the APK will be compressed. We will + * decompress it here + */ + public static File getFileFromFd(Context context, ParcelFileDescriptor fd, + String packageName, @Nullable String compressionAlg) { + File newFile = getTemporaryFile(context, packageName); + if (fd == null || fd.getFileDescriptor() == null) { + return null; + } + InputStream fr = new ParcelFileDescriptor.AutoCloseInputStream(fd); + try { + if (TextUtils.equals(compressionAlg, COMPRESSION_XZ)) { + fr = new XZInputStream(fr); + } else if (TextUtils.equals(compressionAlg, COMPRESSION_LZMA)) { + fr = new LZMAInputStream(fr); + } + } catch (IOException e) { + Log.e(TAG, "Compression was set to " + compressionAlg + ", but could not decode ", e); + return null; + } + + int nRead; + byte[] data = new byte[1024]; + try { + final FileOutputStream fo = new FileOutputStream(newFile); + while ((nRead = fr.read(data, 0, data.length)) != -1) { + fo.write(data, 0, nRead); + } + fo.flush(); + fo.close(); + Os.chmod(newFile.getAbsolutePath(), 0644); + return newFile; + } catch (IOException e) { + Log.e(TAG, "Reading from Asset FD or writing to temp file failed ", e); + return null; + } catch (ErrnoException e) { + Log.e(TAG, "Could not set permissions on file ", e); + return null; + } finally { + try { + fr.close(); + } catch (IOException e) { + Log.e(TAG, "Failed to close the file from FD ", e); + } + } + } + + public static boolean hasLauncherActivity(PackageParser.Package pkg) { + if (pkg == null || pkg.activities == null) { + return false; + } + + final int activityCount = pkg.activities.size(); + for (int i = 0; i < activityCount; ++i) { + if (pkg.activities.get(i).intents != null) { + ArrayList intents = + pkg.activities.get(i).intents; + final int intentsCount = intents.size(); + for (int j = 0; j < intentsCount; ++j) { + final PackageParser.ActivityIntentInfo intentInfo = intents.get(j); + if (intentInfo.hasAction(Intent.ACTION_MAIN)) { + if (intentInfo.hasCategory(Intent.CATEGORY_INFO) || + intentInfo .hasCategory(Intent.CATEGORY_LAUNCHER)) { + return true; + } + } + } + } + } + return false; + } + + public static boolean isWear(final Context context) { + return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); + } +} -- cgit v1.2.3 From 94df7bf72fef3bd0a03e463279cac65144ba1da4 Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Mon, 31 Aug 2015 19:30:51 -0700 Subject: Add an API for getting list of all apps that have runtime perms Bug: 23819535 - Used for Android Wear Change-Id: Ie42ce04453cb0cd7d2cf292065bbb1891fffbfa9 --- .../permission/model/PermissionStatusReceiver.java | 92 +++++++++++++++++----- .../packageinstaller/permission/utils/Utils.java | 6 ++ 2 files changed, 77 insertions(+), 21 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java b/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java index 7bae18f3..bd0f7ba0 100644 --- a/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java +++ b/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java @@ -18,6 +18,7 @@ package com.android.packageinstaller.permission.model; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; @@ -30,37 +31,52 @@ import com.android.packageinstaller.permission.utils.Utils; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class PermissionStatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - int[] counts = new int[3]; - ArrayList grantedGroups = new ArrayList<>(); - boolean succeeded = false; + if (Intent.ACTION_GET_PERMISSIONS_COUNT.equals(intent.getAction())) { + Intent responseIntent = new Intent(intent.getStringExtra( + Intent.EXTRA_GET_PERMISSIONS_RESPONSE_INTENT)); + responseIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); - boolean isForPackage = intent.hasExtra(Intent.EXTRA_PACKAGE_NAME); + int[] counts = new int[3]; + ArrayList grantedGroups = new ArrayList<>(); + boolean succeeded = false; - Intent responseIntent = new Intent(intent.getStringExtra( - Intent.EXTRA_GET_PERMISSIONS_RESPONSE_INTENT)); - responseIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); - - - if (isForPackage) { - String pkg = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); - succeeded = getPermissionsCount(context, pkg, counts, grantedGroups); - } else { - succeeded = getAppsWithPermissionsCount(context, counts); - } - if (succeeded) { - responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_COUNT_RESULT, counts); + boolean isForPackage = intent.hasExtra(Intent.EXTRA_PACKAGE_NAME); if (isForPackage) { - responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_GROUP_LIST_RESULT, - grantedGroups.toArray(new CharSequence[grantedGroups.size()])); + String pkg = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); + succeeded = getPermissionsCount(context, pkg, counts, grantedGroups); + } else { + succeeded = getAppsWithPermissionsCount(context, counts); } - } + if (succeeded) { + responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_COUNT_RESULT, counts); - context.sendBroadcast(responseIntent); + if (isForPackage) { + responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_GROUP_LIST_RESULT, + grantedGroups.toArray(new CharSequence[grantedGroups.size()])); + } + } + context.sendBroadcast(responseIntent); + } else if (Intent.ACTION_GET_PERMISSIONS_PACKAGES.equals(intent.getAction())) { + Intent responseIntent = new Intent(intent.getStringExtra( + Intent.EXTRA_GET_PERMISSIONS_PACKAGES_RESPONSE_INTENT)); + responseIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); + + List appsList = new ArrayList<>(); + List appLabelsList = new ArrayList<>(); + if (getAppsWithRuntimePermissions(context, appsList, appLabelsList)) { + responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_APP_LIST_RESULT, + appsList.toArray(new String[appsList.size()])); + responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_APP_LABEL_LIST_RESULT, + appLabelsList.toArray(new String[appLabelsList.size()])); + } + context.sendBroadcast(responseIntent); + } } public boolean getPermissionsCount(Context context, String pkg, int[] counts, @@ -105,6 +121,40 @@ public class PermissionStatusReceiver extends BroadcastReceiver { } } + public boolean getAppsWithRuntimePermissions(Context context, List appsList, + List appLabelsList) { + final List appInfos = Utils.getAllInstalledApplications(context); + if (appInfos == null) { + return false; + } + final int appInfosSize = appInfos.size(); + try { + for (int i = 0; i < appInfosSize; ++i) { + final String packageName = appInfos.get(i).packageName; + PackageInfo packageInfo = context.getPackageManager().getPackageInfo( + packageName, PackageManager.GET_PERMISSIONS); + AppPermissions appPermissions = + new AppPermissions(context, packageInfo, null, false, null); + + boolean shouldShow = false; + for (AppPermissionGroup group : appPermissions.getPermissionGroups()) { + if (Utils.shouldShowPermission(group, packageName)) { + shouldShow = true; + break; + } + } + if (shouldShow) { + appsList.add(packageName); + appLabelsList.add(appPermissions.getAppLabel()); + } + } + } catch (NameNotFoundException e) { + return false; + } + + return true; + } + public boolean getAppsWithPermissionsCount(Context context, int[] counts) { ArraySet launcherPkgs = Utils.getLauncherPackages(context); // Indexed by uid. diff --git a/src/com/android/packageinstaller/permission/utils/Utils.java b/src/com/android/packageinstaller/permission/utils/Utils.java index 2940a729..6715373a 100644 --- a/src/com/android/packageinstaller/permission/utils/Utils.java +++ b/src/com/android/packageinstaller/permission/utils/Utils.java @@ -33,6 +33,8 @@ import android.util.TypedValue; import com.android.packageinstaller.permission.model.AppPermissionGroup; import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; +import java.util.List; + public class Utils { private static final String LOG_TAG = "Utils"; @@ -127,6 +129,10 @@ public class Utils { return launcherPkgs; } + public static List getAllInstalledApplications(Context context) { + return context.getPackageManager().getInstalledApplications(0); + } + public static boolean isSystem(PermissionApp app, ArraySet launcherPkgs) { ApplicationInfo info = app.getAppInfo(); return info.isSystemApp() && (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 -- cgit v1.2.3 From 03dc824d37099b29acb5a0aa28e881d5b05cce0e Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Thu, 10 Sep 2015 09:42:27 -0700 Subject: Changing layout to make button animations work - Fix the Permissions Dialogs for round layout - Show animation for the buttons - Also fixed the names ellipsis issue. - Fixed the theming of the dialogs. Bug: 23118402 Change-Id: I385c827ac41b06222334c36bfda2c70b346232a2 --- src/com/android/packageinstaller/DeviceUtils.java | 32 +++++ .../permission/model/AppPermissions.java | 9 +- .../permission/ui/GrantPermissionsActivity.java | 10 +- .../ui/GrantPermissionsWatchViewHandler.java | 4 +- .../permission/ui/PermissionAppsFragment.java | 3 +- .../ui/PermissionConfirmationViewHandler.java | 132 ++++++++++++++++++++- .../permission/ui/PermissionsFrameFragment.java | 3 +- .../permission/ui/SettingsWithHeader.java | 3 +- .../packageinstaller/permission/utils/Utils.java | 5 - .../wear/WearPackageInstallerService.java | 3 +- .../packageinstaller/wear/WearPackageUtil.java | 4 - 11 files changed, 182 insertions(+), 26 deletions(-) create mode 100644 src/com/android/packageinstaller/DeviceUtils.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/DeviceUtils.java b/src/com/android/packageinstaller/DeviceUtils.java new file mode 100644 index 00000000..8e2d57ea --- /dev/null +++ b/src/com/android/packageinstaller/DeviceUtils.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015 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.packageinstaller; + +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.Configuration; + +public class DeviceUtils { + public static boolean isTelevision(Context context) { + int uiMode = context.getResources().getConfiguration().uiMode; + return (uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION; + } + + public static boolean isWear(final Context context) { + return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); + } +} diff --git a/src/com/android/packageinstaller/permission/model/AppPermissions.java b/src/com/android/packageinstaller/permission/model/AppPermissions.java index d465ee09..a0f23d64 100644 --- a/src/com/android/packageinstaller/permission/model/AppPermissions.java +++ b/src/com/android/packageinstaller/permission/model/AppPermissions.java @@ -23,6 +23,8 @@ import android.text.BidiFormatter; import android.text.TextPaint; import android.text.TextUtils; +import com.android.packageinstaller.DeviceUtils; + import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; @@ -165,9 +167,12 @@ public final class AppPermissions { private static CharSequence loadEllipsizedAppLabel(Context context, PackageInfo packageInfo) { String label = packageInfo.applicationInfo.loadLabel( context.getPackageManager()).toString(); - String noNewLineLabel = label.replace("\n", " "); - String ellipsizedLabel = TextUtils.ellipsize(noNewLineLabel, sAppLabelEllipsizePaint, + String ellipsizedLabel = label.replace("\n", " "); + if (!DeviceUtils.isWear(context)) { + // Only ellipsize for non-Wear devices. + ellipsizedLabel = TextUtils.ellipsize(ellipsizedLabel, sAppLabelEllipsizePaint, MAX_APP_LABEL_LENGTH_PIXELS, TextUtils.TruncateAt.END).toString(); + } return BidiFormatter.getInstance().unicodeWrap(ellipsizedLabel); } } diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java index 922f5430..bb4dde7e 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java @@ -38,6 +38,7 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.model.AppPermissionGroup; import com.android.packageinstaller.permission.model.AppPermissions; @@ -71,9 +72,9 @@ public class GrantPermissionsActivity extends OverlayTouchActivity setTitle(R.string.permission_request_title); - if (Utils.isTelevision(this)) { + if (DeviceUtils.isTelevision(this)) { mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this); - } else if (isWatch()) { + } else if (DeviceUtils.isWear(this)) { mViewHandler = new GrantPermissionsWatchViewHandler(this).setResultListener(this); } else { mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this); @@ -361,11 +362,6 @@ public class GrantPermissionsActivity extends OverlayTouchActivity SafetyNetLogger.logPermissionsRequested(mAppPermissions.getPackageInfo(), groups); } - private boolean isWatch() { - PackageManager pm = getPackageManager(); - return pm.hasSystemFeature(pm.FEATURE_WATCH); - } - private static final class GroupState { static final int STATE_UNKNOWN = 0; static final int STATE_ALLOWED = 1; diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java index ac573c43..a3d3b805 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java @@ -15,14 +15,14 @@ import com.android.packageinstaller.R; */ final class GrantPermissionsWatchViewHandler extends PermissionConfirmationViewHandler implements GrantPermissionsViewHandler { - private static final String TAG = "GrantPermissionsViewH"; + private static final String TAG = "GrantPermsWatchViewH"; private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; private final Context mContext; private ResultListener mResultListener; - + private String mGroupName; private boolean mShowDoNotAsk; diff --git a/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java index 8dacd037..1e588939 100644 --- a/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java +++ b/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java @@ -39,6 +39,7 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.model.AppPermissionGroup; import com.android.packageinstaller.permission.model.PermissionApps; @@ -185,7 +186,7 @@ public final class PermissionAppsFragment extends PermissionsFrameFragment imple return; } - boolean isTelevision = Utils.isTelevision(context); + boolean isTelevision = DeviceUtils.isTelevision(context); PreferenceScreen screen = getPreferenceScreen(); ArraySet preferencesToRemove = new ArraySet<>(); diff --git a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java index 63ed0a45..3e32c7f2 100644 --- a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java @@ -1,12 +1,18 @@ package com.android.packageinstaller.permission.ui; +import android.animation.ObjectAnimator; +import android.animation.PropertyValuesHolder; import android.content.Context; import android.graphics.drawable.Icon; import android.os.Handler; +import android.os.Message; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.view.animation.AnimationUtils; +import android.view.animation.Interpolator; import android.widget.Button; import android.widget.ImageView; import android.widget.ScrollView; @@ -15,10 +21,15 @@ import android.widget.TextView; import com.android.packageinstaller.R; public abstract class PermissionConfirmationViewHandler implements - View.OnClickListener { + Handler.Callback, + View.OnClickListener, + ViewTreeObserver.OnScrollChangedListener { public static final int MODE_HORIZONTAL_BUTTONS = 0; public static final int MODE_VERTICAL_BUTTONS = 1; + private static final int MSG_HIDE_BUTTON_BAR = 1001; + private static final long HIDE_ANIM_DURATION = 500; + private View mRoot; private TextView mCurrentPageText; private ImageView mIcon; @@ -34,6 +45,13 @@ public abstract class PermissionConfirmationViewHandler implements private Context mContext; + private Handler mHideHandler; + private Interpolator mInterpolator; + private float mButtonBarFloatingHeight; + private ObjectAnimator mButtonBarAnimator; + private float mCurrentTranslation; + private boolean mHiddenBefore; + // TODO: Move these into a builder public abstract void onAllow(); public abstract void onDeny(); @@ -74,6 +92,12 @@ public abstract class PermissionConfirmationViewHandler implements mVerticalDeny.setOnClickListener(this); mVerticalDenyDoNotAskAgain.setOnClickListener(this); + mInterpolator = AnimationUtils.loadInterpolator(mContext, + android.R.interpolator.fast_out_slow_in); + mButtonBarFloatingHeight = mContext.getResources().getDimension( + R.dimen.conf_diag_floating_height); + mHideHandler = new Handler(this); + return mRoot; } @@ -102,7 +126,6 @@ public abstract class PermissionConfirmationViewHandler implements } else { mIcon.setVisibility(View.INVISIBLE); } - mMessage.setText(getMessage()); switch (getButtonBarMode()) { @@ -127,6 +150,43 @@ public abstract class PermissionConfirmationViewHandler implements } mScrollingContainer.scrollTo(0, 0); + + mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this); + + mRoot.getViewTreeObserver().addOnGlobalLayoutListener( + new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + // Setup Button animation. + // pop the button bar back to full height, stop all animation + if (mButtonBarAnimator != null) { + mButtonBarAnimator.cancel(); + } + + // In order to fake the buttons peeking at the bottom, need to do set the + // padding properly. + if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { + mContent.setPadding(0, 0, 0, mButtonBarContainer.getHeight()); + } + + // stop any calls to hide the button bar in the future + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + mHiddenBefore = false; + + // determine which mode the scrolling should work at. + if (mContent.getHeight() > mScrollingContainer.getHeight()) { + mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); + mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); + generateButtonBarAnimator(mButtonBarContainer.getHeight(), 0, 0, + mButtonBarFloatingHeight, 1000); + } else { + mButtonBarContainer.setTranslationY(0); + mButtonBarContainer.setTranslationZ(0); + } + mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + } @Override @@ -146,4 +206,72 @@ public abstract class PermissionConfirmationViewHandler implements break; } } + + @Override + public boolean handleMessage (Message msg) { + switch (msg.what) { + case MSG_HIDE_BUTTON_BAR: + hideButtonBar(); + return true; + } + return false; + } + + @Override + public void onScrollChanged() { + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + hideButtonBar(); + } + + private void hideButtonBar() { + // get the offset to the top of the button bar + int offset = mScrollingContainer.getHeight() + mButtonBarContainer.getHeight() - + mContent.getHeight() + Math.max(mScrollingContainer.getScrollY(), 0); + int translationY = offset > 0 ? mButtonBarContainer.getHeight() - offset : + mButtonBarContainer.getHeight(); + + if (!mHiddenBefore || mButtonBarAnimator == null) { + // hasn't hidden the bar yet, just hide now to the right height + generateButtonBarAnimator( + mButtonBarContainer.getTranslationY(), translationY, + mButtonBarFloatingHeight, 0, HIDE_ANIM_DURATION); + } else if (mButtonBarAnimator.isRunning()) { + // we are animating the button bar closing, change to animate to the right place + if (Math.abs(mCurrentTranslation - translationY) > 1e-2f) { + mButtonBarAnimator.cancel(); // stop current animation + + if (Math.abs(mButtonBarContainer.getTranslationY() - translationY) > 1e-2f) { + long duration = Math.max((long) ( + (float) HIDE_ANIM_DURATION + * (translationY - mButtonBarContainer.getTranslationY()) + / mButtonBarContainer.getHeight()), 0); + generateButtonBarAnimator( + mButtonBarContainer.getTranslationY(), translationY, + mButtonBarFloatingHeight, 0, duration); + } else { + mButtonBarContainer.setTranslationY(translationY); + mButtonBarContainer.setTranslationZ(0); + } + } + } else { + // not currently animating, have already hidden, snap to the right offset + mButtonBarContainer.setTranslationY(translationY); + mButtonBarContainer.setTranslationZ(0); + } + + mHiddenBefore = true; + } + + private void generateButtonBarAnimator( + float startY, float endY, float startZ, float endZ, long duration) { + mButtonBarAnimator = + ObjectAnimator.ofPropertyValuesHolder( + mButtonBarContainer, + PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, startY, endY), + PropertyValuesHolder.ofFloat(View.TRANSLATION_Z, startZ, endZ)); + mCurrentTranslation = endY; + mButtonBarAnimator.setDuration(duration); + mButtonBarAnimator.setInterpolator(mInterpolator); + mButtonBarAnimator.start(); + } } diff --git a/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java index 40058f6d..35b6f1af 100644 --- a/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java +++ b/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java @@ -15,6 +15,7 @@ import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; @@ -117,7 +118,7 @@ public abstract class PermissionsFrameFragment extends PreferenceFragment { @Override public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { - if (Utils.isTelevision(getContext())) { + if (DeviceUtils.isTelevision(getContext())) { mGridView = (VerticalGridView) inflater.inflate( R.layout.leanback_preferences_list, parent, false); mGridView.setWindowAlignmentOffset(0); diff --git a/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java index 7b58fed1..976fee11 100644 --- a/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java +++ b/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java @@ -26,6 +26,7 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; @@ -42,7 +43,7 @@ public abstract class SettingsWithHeader extends PermissionsFrameFragment Bundle savedInstanceState) { ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); - if (!Utils.isTelevision(getContext())) { + if (!DeviceUtils.isTelevision(getContext())) { mHeader = inflater.inflate(R.layout.header, root, false); getPreferencesContainer().addView(mHeader, 0); updateHeader(); diff --git a/src/com/android/packageinstaller/permission/utils/Utils.java b/src/com/android/packageinstaller/permission/utils/Utils.java index 2940a729..7ff5c965 100644 --- a/src/com/android/packageinstaller/permission/utils/Utils.java +++ b/src/com/android/packageinstaller/permission/utils/Utils.java @@ -132,9 +132,4 @@ public class Utils { return info.isSystemApp() && (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 && !launcherPkgs.contains(info.packageName); } - - public static boolean isTelevision(Context context) { - int uiMode = context.getResources().getConfiguration().uiMode; - return (uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION; - } } diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java index e9ea21e1..5ce0b9a1 100644 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -45,6 +45,7 @@ import android.os.Process; import android.text.TextUtils; import android.util.Log; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.PackageUtil; import java.io.ByteArrayOutputStream; @@ -172,7 +173,7 @@ public class WearPackageInstallerService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - if (!WearPackageUtil.isWear(this)) { + if (!DeviceUtils.isWear(this)) { Log.w(TAG, "Not running on wearable"); return START_NOT_STICKY; } diff --git a/src/com/android/packageinstaller/wear/WearPackageUtil.java b/src/com/android/packageinstaller/wear/WearPackageUtil.java index bec697d9..dc420757 100644 --- a/src/com/android/packageinstaller/wear/WearPackageUtil.java +++ b/src/com/android/packageinstaller/wear/WearPackageUtil.java @@ -147,8 +147,4 @@ public class WearPackageUtil { } return false; } - - public static boolean isWear(final Context context) { - return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); - } } -- cgit v1.2.3 From 6412dc4a8f47b134af6c5abb5f7f30321bb3ce68 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Mon, 14 Sep 2015 15:43:25 -0700 Subject: Permissions Watch settings UI port This CL contains all the UI elements needed to build the Wear version of the Settings UI for permissions. The actual implementation for the Wear version of SettingsUI will be done in a follow up bug. This is basically a straight port from our Settings app and wearable-support lib. I tweaked two strings and changed the package paths to make it easier to group the files together. BUG: 23080561 Change-Id: I6d72d29dd1926ad07d63f85b52ccf9f72ff70df1 --- .../ExtendedOnCenterProximityListener.java | 30 +++ .../ui/wear/settings/ExtendedViewHolder.java | 84 ++++++ .../ui/wear/settings/SettingsAdapter.java | 295 +++++++++++++++++++++ .../permission/ui/wear/settings/ViewUtils.java | 48 ++++ 4 files changed, 457 insertions(+) create mode 100644 src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedOnCenterProximityListener.java create mode 100644 src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedViewHolder.java create mode 100644 src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java create mode 100644 src/com/android/packageinstaller/permission/ui/wear/settings/ViewUtils.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedOnCenterProximityListener.java b/src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedOnCenterProximityListener.java new file mode 100644 index 00000000..02c203b3 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedOnCenterProximityListener.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.wear.settings; + +import android.support.wearable.view.WearableListView; + +public interface ExtendedOnCenterProximityListener + extends WearableListView.OnCenterProximityListener { + float getProximityMinValue(); + + float getProximityMaxValue(); + + float getCurrentProximityValue(); + + void setScalingAnimatorValue(float value); +} diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedViewHolder.java b/src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedViewHolder.java new file mode 100644 index 00000000..6b725419 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/ExtendedViewHolder.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.wear.settings; + +import android.animation.ObjectAnimator; +import android.support.wearable.view.WearableListView; +import android.view.View; + + +public class ExtendedViewHolder extends WearableListView.ViewHolder { + public static final long DEFAULT_ANIMATION_DURATION = 150; + + private ObjectAnimator mScalingUpAnimator; + + private ObjectAnimator mScalingDownAnimator; + + private float mMinValue; + + private float mMaxValue; + + public ExtendedViewHolder(View itemView) { + super(itemView); + if (itemView instanceof ExtendedOnCenterProximityListener) { + ExtendedOnCenterProximityListener item = + (ExtendedOnCenterProximityListener) itemView; + mMinValue = item.getProximityMinValue(); + item.setScalingAnimatorValue(mMinValue); + mMaxValue = item.getProximityMaxValue(); + mScalingUpAnimator = ObjectAnimator.ofFloat(item, "scalingAnimatorValue", mMinValue, + mMaxValue); + mScalingUpAnimator.setDuration(DEFAULT_ANIMATION_DURATION); + mScalingDownAnimator = ObjectAnimator.ofFloat(item, "scalingAnimatorValue", + mMaxValue, mMinValue); + mScalingDownAnimator.setDuration(DEFAULT_ANIMATION_DURATION); + } + } + + public void onCenterProximity(boolean isCentralItem, boolean animate) { + if (!(itemView instanceof ExtendedOnCenterProximityListener)) { + return; + } + ExtendedOnCenterProximityListener item = (ExtendedOnCenterProximityListener) itemView; + if (isCentralItem) { + if (animate) { + mScalingDownAnimator.cancel(); + if (!mScalingUpAnimator.isRunning()) { + mScalingUpAnimator.setFloatValues(item.getCurrentProximityValue(), + mMaxValue); + mScalingUpAnimator.start(); + } + } else { + mScalingUpAnimator.cancel(); + item.setScalingAnimatorValue(item.getProximityMaxValue()); + } + } else { + mScalingUpAnimator.cancel(); + if (animate) { + if (!mScalingDownAnimator.isRunning()) { + mScalingDownAnimator.setFloatValues(item.getCurrentProximityValue(), + mMinValue); + mScalingDownAnimator.start(); + } + } else { + mScalingDownAnimator.cancel(); + item.setScalingAnimatorValue(item.getProximityMinValue()); + } + } + super.onCenterProximity(isCentralItem, animate); + } +} diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java b/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java new file mode 100644 index 00000000..9216cbbf --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java @@ -0,0 +1,295 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.wear.settings; + +import android.content.Context; +import android.support.wearable.view.CircledImageView; +import android.support.wearable.view.WearableListView; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.style.TextAppearanceSpan; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.TextView; + +import com.android.packageinstaller.R; + +import java.util.ArrayList; + +/** + * Common adapter for settings views. Maintains a list of 'Settings', consisting of a name, + * icon and optional activity-specific data. + */ +public class SettingsAdapter extends WearableListView.Adapter { + private static final String TAG = "SettingsAdapter"; + private final Context mContext; + + protected static CharSequence generateLabelWithState( + Context context, int labelId, boolean enabled) { + return generateLabelWithState(context, labelId, R.string.generic_enabled, enabled); + } + + protected static CharSequence generateLabelWithState( + Context context, int labelId, int onId, boolean enabled) { + SpannableStringBuilder ssb = new SpannableStringBuilder(context.getString(labelId)); + ssb.append('\n'); + ssb.append( + context.getString(enabled ? onId : R.string.generic_disabled), + new TextAppearanceSpan(context, R.style.TextAppearance_Settings_Label_Large), + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return ssb; + } + + public static final class Setting { + public static final int ID_INVALID = -1; + + public final int id; + public int nameResourceId; + public CharSequence name; + public int iconResource; + public boolean inProgress; + public S data; + + public Setting(CharSequence name, int iconResource, S data) { + this(name, iconResource, data, ID_INVALID); + } + + public Setting(CharSequence name, int iconResource, S data, int id) { + this.name = name; + this.iconResource = iconResource; + this.data = data; + this.inProgress = false; + this.id = id; + } + + public Setting(int nameResource, int iconResource, S data, int id) { + this.nameResourceId = nameResource; + this.iconResource = iconResource; + this.data = data; + this.inProgress = false; + this.id = id; + } + + public Setting(int nameResource, int iconResource, int id) { + this.nameResourceId = nameResource; + this.iconResource = iconResource; + this.data = null; + this.inProgress = false; + this.id = id; + } + + public Setting(CharSequence name, int iconResource, int id) { + this(name, iconResource, null, id); + } + + } + + private final int mItemLayoutId; + private final float mDefaultCircleRadiusPercent; + private final float mSelectedCircleRadiusPercent; + + protected ArrayList> mSettings = new ArrayList>(); + + public SettingsAdapter(Context context, int itemLayoutId) { + mContext = context; + mItemLayoutId = itemLayoutId; + mDefaultCircleRadiusPercent = context.getResources().getFraction( + R.dimen.default_settings_circle_radius_percent, 1, 1); + mSelectedCircleRadiusPercent = context.getResources().getFraction( + R.dimen.selected_settings_circle_radius_percent, 1, 1); + } + + @Override + public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new SettingsItemHolder(new SettingsItem(parent.getContext())); + } + + @Override + public void onBindViewHolder(WearableListView.ViewHolder holder, int position) { + Setting setting = mSettings.get(position); + if (setting.iconResource == -1) { + ((SettingsItemHolder) holder).imageView.setVisibility(View.GONE); + } else { + ((SettingsItemHolder) holder).imageView.setVisibility(View.VISIBLE); + ((SettingsItemHolder) holder).imageView.setImageResource( + mSettings.get(position).iconResource); + } + Log.d(TAG, "onBindViewHolder " + setting.name + " " + setting.id + " " + setting + .nameResourceId); + if (setting.name == null && setting.nameResourceId != 0) { + setting.name = mContext.getString(setting.nameResourceId); + } + ((SettingsItemHolder) holder).textView.setText(setting.name); + } + + @Override + public int getItemCount() { + return mSettings.size(); + } + + public void addSetting(CharSequence name, int iconResource) { + addSetting(name, iconResource, null); + } + + public void addSetting(CharSequence name, int iconResource, T intent) { + addSetting(mSettings.size(), name, iconResource, intent); + } + + public void addSetting(int index, CharSequence name, int iconResource, T intent) { + addSetting(Setting.ID_INVALID, index, name, iconResource, intent); + } + + public void addSetting(int id, int index, CharSequence name, int iconResource, T intent) { + mSettings.add(index, new Setting(name, iconResource, intent, id)); + notifyItemInserted(index); + } + + public void addSettingDontNotify(Setting setting) { + mSettings.add(setting); + } + + public void addSetting(Setting setting) { + mSettings.add(setting); + notifyItemInserted(mSettings.size() - 1); + } + + public void addSetting(int index, Setting setting) { + mSettings.add(index, setting); + notifyItemInserted(index); + } + + /** + * Returns the index of the setting in the adapter based on the ID supplied when it was + * originally added. + * @param id the setting's id + * @return index in the adapter of the setting. -1 if not found. + */ + public int findSetting(int id) { + for (int i = mSettings.size() - 1; i >= 0; --i) { + Setting setting = mSettings.get(i); + + if (setting.id == id) { + return i; + } + } + + return -1; + } + + /** + * Removes a setting at the given index. + * @param index the index of the setting to be removed + */ + public void removeSetting(int index) { + mSettings.remove(index); + notifyDataSetChanged(); + } + + public void clearSettings() { + mSettings.clear(); + notifyDataSetChanged(); + } + + /** + * Updates a setting in place. + * @param index the index of the setting + * @param name the updated setting name + * @param iconResource the update setting icon + * @param intent the updated intent for the setting + */ + public void updateSetting(int index, CharSequence name, int iconResource, T intent) { + Setting setting = mSettings.get(index); + setting.iconResource = iconResource; + setting.name = name; + setting.data = intent; + notifyItemChanged(index); + } + + public Setting get(int position) { + return mSettings.get(position); + } + + protected static class SettingsItemHolder extends ExtendedViewHolder { + public final CircledImageView imageView; + final TextView textView; + + public SettingsItemHolder(View itemView) { + super(itemView); + + imageView = ((CircledImageView) itemView.findViewById(R.id.image)); + textView = ((TextView) itemView.findViewById(R.id.text)); + } + } + + protected class SettingsItem extends FrameLayout implements ExtendedOnCenterProximityListener { + + private final CircledImageView mImage; + private final TextView mText; + + public SettingsItem(Context context) { + super(context); + View view = View.inflate(context, mItemLayoutId, null); + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( + FrameLayout.LayoutParams.MATCH_PARENT, + FrameLayout.LayoutParams.MATCH_PARENT); + params.gravity = Gravity.CENTER_VERTICAL; + addView(view, params); + mImage = (CircledImageView) findViewById(R.id.image); + mText = (TextView) findViewById(R.id.text); + } + + @Override + public float getProximityMinValue() { + return mDefaultCircleRadiusPercent; + } + + @Override + public float getProximityMaxValue() { + return mSelectedCircleRadiusPercent; + } + + @Override + public float getCurrentProximityValue() { + return mImage.getCircleRadiusPressedPercent(); + } + + @Override + public void setScalingAnimatorValue(float value) { + mImage.setCircleRadiusPercent(value); + mImage.setCircleRadiusPressedPercent(value); + } + + @Override + public void onCenterPosition(boolean animate) { + mImage.setAlpha(1f); + mText.setAlpha(1f); + } + + @Override + public void onNonCenterPosition(boolean animate) { + mImage.setAlpha(0.5f); + mText.setAlpha(0.5f); + } + + TextView getTextView() { + return mText; + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/ViewUtils.java b/src/com/android/packageinstaller/permission/ui/wear/settings/ViewUtils.java new file mode 100644 index 00000000..cf1c0fd0 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/ViewUtils.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.wear.settings; + +import android.content.Context; +import android.view.View; +import android.view.ViewGroup; + +/** + * Utility to determine screen shape + */ +public class ViewUtils { + + public static boolean getIsCircular(Context context) { + return context.getResources().getConfiguration().isScreenRound(); + } + + /** + * Set the given {@code view} and all descendants to the given {@code enabled} state. + * + * @param view the parent view of a subtree of components whose enabled state must be set + * @param enabled the new enabled state of the subtree of components + */ + public static void setEnabled(View view, boolean enabled) { + view.setEnabled(enabled); + + if (view instanceof ViewGroup) { + final ViewGroup viewGroup = (ViewGroup) view; + for (int i = 0; i < viewGroup.getChildCount(); i++) { + setEnabled(viewGroup.getChildAt(i), enabled); + } + } + } +} -- cgit v1.2.3 From df4d4542c2cf2fa0a7539412af2a99d60b1c2a15 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Tue, 15 Sep 2015 18:48:16 -0700 Subject: Build initial Permissions Settings UI page for Wear This is a first pass to build a functional settings UI page for Permissions on Android Wear. The user flow is a little different than the phone version, so there are some nuances that need to be adjusted from the normal flow. I have forked off of the AppPermissionsFragment and created a Wear version for us to use. Rather than try to link the Wearable-Support lib into AOSP code, I have made a duplicate of the few files I needed. This change adds a Wear layout version of the Settings UI page. The UI is fully functional for most cases, but there are some tweaks still needed for full parity. The tweaks are primarily around UI and special edge case functions. BUG: 23080561 Change-Id: I8477f6b966cacaae9e77aa0fb61b4b1e621a9ead --- .../permission/ui/ManagePermissionsActivity.java | 10 +- .../ui/wear/AppPermissionsFragmentWear.java | 262 +++++++++++++++++++++ .../permission/ui/wear/TitledSettingsFragment.java | 241 +++++++++++++++++++ .../wear/settings/PermissionsSettingsAdapter.java | 85 +++++++ .../ui/wear/settings/SettingsAdapter.java | 2 +- 5 files changed, 598 insertions(+), 2 deletions(-) create mode 100644 src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java create mode 100644 src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java index 8ba6b127..a61a862d 100644 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java @@ -21,6 +21,9 @@ import android.content.Intent; import android.os.Bundle; import android.util.Log; +import com.android.packageinstaller.permission.ui.wear.AppPermissionsFragmentWear; +import com.android.packageinstaller.DeviceUtils; + public final class ManagePermissionsActivity extends OverlayTouchActivity { private static final String LOG_TAG = "ManagePermissionsActivity"; @@ -47,7 +50,12 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { finish(); return; } - fragment = AppPermissionsFragment.newInstance(packageName); + + if (DeviceUtils.isWatch(this)) { + fragment = AppPermissionsFragmentWear.newInstance(packageName); + } else { + fragment = AppPermissionsFragment.newInstance(packageName); + } } break; case Intent.ACTION_MANAGE_PERMISSION_APPS: { diff --git a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java new file mode 100644 index 00000000..9e54c7d5 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java @@ -0,0 +1,262 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.wear; + +import android.annotation.Nullable; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.wearable.view.WearableListView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.AppPermissions; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.ui.wear.settings.PermissionsSettingsAdapter; +import com.android.packageinstaller.permission.ui.wear.settings.SettingsAdapter; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class AppPermissionsFragmentWear extends TitledSettingsFragment { + + private static final String LOG_TAG = "ManagePermsFragment"; + + private List mToggledGroups; + private AppPermissions mAppPermissions; + private PermissionsSettingsAdapter mAdapter; + + private boolean mHasConfirmedRevoke; + + public static AppPermissionsFragmentWear newInstance(String packageName) { + return setPackageName(new AppPermissionsFragmentWear(), packageName); + } + + private static T setPackageName(T fragment, String packageName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + fragment.setArguments(arguments); + return fragment; + + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + Activity activity = getActivity(); + PackageManager pm = activity.getPackageManager(); + PackageInfo packageInfo; + + try { + packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); + } catch (PackageManager.NameNotFoundException e) { + Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); + packageInfo = null; + } + + if (packageInfo == null) { + Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); + activity.finish(); + return; + } + + mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); + + mAdapter = new PermissionsSettingsAdapter(getContext()); + + initializePermissionGroupList(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.settings, container, false); + } + + @Override + public void onResume() { + super.onResume(); + mAppPermissions.refresh(); + + // Also refresh the UI + final int count = mAdapter.getItemCount(); + for (int i = 0; i < count; ++i) { + updatePermissionGroupSetting(i); + } + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (mAppPermissions != null) { + initializeLayout(mAdapter); + bindUi(mAppPermissions.getPackageInfo()); + } + } + + private void bindUi(PackageInfo packageInfo) { + Activity activity = getActivity(); + PackageManager pm = activity.getPackageManager(); + ApplicationInfo appInfo = packageInfo.applicationInfo; + CharSequence label = appInfo.loadLabel(pm); + mHeader.setText(label); + } + + private void initializePermissionGroupList() { + final String packageName = mAppPermissions.getPackageInfo().packageName; + List groups = mAppPermissions.getPermissionGroups(); + final int count = groups.size(); + for (int i = 0; i < count; ++i) { + final AppPermissionGroup group = groups.get(i); + if (!Utils.shouldShowPermission(group, packageName)) { + continue; + } + + SettingsAdapter.Setting setting = + new SettingsAdapter.Setting( + group.getLabel(), + getPermissionGroupIcon(group), + i); + setting.data = group; + mAdapter.addSetting(setting); + } + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + @Override + public void onClick(WearableListView.ViewHolder view) { + final int index = view.getPosition(); + SettingsAdapter.Setting setting = mAdapter.get(index); + final AppPermissionGroup group = setting.data; + + if (group == null) { + Log.e(LOG_TAG, "Error: AppPermissionGroup is null"); + return; + } + + // The way WearableListView is designed, there is no way to avoid this click handler + // Since the policy is fixed, ignore the click as the user is not able to change the state + // of this permission group + if (group.isPolicyFixed()) { + return; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return; + } + + addToggledGroup(group); + + if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { + LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); + return; + } + + if (!group.areRuntimePermissionsGranted()) { + group.grantRuntimePermissions(false); + } else { + final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); + if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + group.revokeRuntimePermissions(false); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + + updatePermissionGroupSetting(index); + } + }) + .show(); + } else { + group.revokeRuntimePermissions(false); + } + } + + updatePermissionGroupSetting(index); + } + + private void updatePermissionGroupSetting(int index) { + SettingsAdapter.Setting setting = mAdapter.get(index); + AppPermissionGroup group = setting.data; + mAdapter.updateSetting( + index, + group.getLabel(), + getPermissionGroupIcon(group), + group); + } + + private void addToggledGroup(AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayList<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.contains(group)) { + mToggledGroups.remove(group); + } else { + mToggledGroups.add(group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + String packageName = mAppPermissions.getPackageInfo().packageName; + SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); + mToggledGroups = null; + } + } + + private int getPermissionGroupIcon(AppPermissionGroup group) { + // TODO: Return the correct icon based on if permissions are granted + return group.getIconResId(); + } +} diff --git a/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java b/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java new file mode 100644 index 00000000..a738decd --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.wear; + +import android.app.Fragment; +import android.content.Context; +import android.os.Bundle; +import android.support.v7.widget.RecyclerView; +import android.support.wearable.view.WearableListView; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.view.WindowInsets; +import android.widget.FrameLayout; +import android.widget.TextView; + +import com.android.packageinstaller.permission.ui.wear.settings.ViewUtils; +import com.android.packageinstaller.R; + +/** + * Base settings Fragment that shows a title at the top of the page. + */ +public abstract class TitledSettingsFragment extends Fragment implements + View.OnLayoutChangeListener, WearableListView.ClickListener { + + private static final int ITEM_CHANGE_DURATION_MS = 120; + + private static final String TAG = "TitledSettingsFragment"; + private int mInitialHeaderHeight; + + protected TextView mHeader; + protected WearableListView mWheel; + + private static boolean sInitialized; + private static int sCharLimitShortTitle; + private static int sCharLimitLine; + private static int mChinOffset; + + private TextWatcher mHeaderTextWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) {} + + @Override + public void afterTextChanged(Editable editable) { + adjustHeaderSize(); + } + + }; + + private void adjustHeaderTranslation() { + int translation = 0; + if (mWheel.getChildCount() > 0) { + translation = mWheel.getCentralViewTop() - mWheel.getChildAt(0).getTop(); + } + + float newTranslation = Math.min(Math.max(-mInitialHeaderHeight, -translation), 0); + + int position = mWheel.getChildAdapterPosition(mWheel.getChildAt(0)); + if (position == 0 || newTranslation < 0) { + mHeader.setTranslationY(newTranslation); + } + } + + @Override + public void onTopEmptyRegionClick() { + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (!sInitialized) { + sCharLimitShortTitle = getResources().getInteger(R.integer.short_title_length); + sCharLimitLine = getResources().getInteger(R.integer.char_limit_per_line); + sInitialized = true; + } + } + + @Override + public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, + int oldTop, int oldRight, int oldBottom) { + if (view == mHeader) { + mInitialHeaderHeight = bottom - top; + if (ViewUtils.getIsCircular(getContext())) { + // We are adding more margin on circular screens, so we need to account for it and use + // it for hiding the header. + mInitialHeaderHeight += + ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).topMargin; + } + } else if (view == mWheel) { + adjustHeaderTranslation(); + } + } + + protected void initializeLayout(RecyclerView.Adapter adapter) { + View v = getView(); + mWheel = (WearableListView) v.findViewById(R.id.wheel); + + mHeader = (TextView) v.findViewById(R.id.header); + mHeader.addOnLayoutChangeListener(this); + mHeader.addTextChangedListener(mHeaderTextWatcher); + + mWheel.setAdapter(adapter); + mWheel.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + } + + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + adjustHeaderTranslation(); + } + }); + mWheel.setClickListener(this); + mWheel.addOnLayoutChangeListener(this); + + // Decrease item change animation duration to approximately half of the default duration. + RecyclerView.ItemAnimator itemAnimator = mWheel.getItemAnimator(); + itemAnimator.setChangeDuration(ITEM_CHANGE_DURATION_MS); + + adjustHeaderSize(); + + positionOnCircular(getContext(), mHeader, mWheel); + } + + public static void positionOnCircular(Context context, View header, final ViewGroup wheel) { + if (ViewUtils.getIsCircular(context)) { + FrameLayout.LayoutParams params = + (FrameLayout.LayoutParams) header.getLayoutParams(); + params.topMargin = (int) context.getResources().getDimension( + R.dimen.settings_header_top_margin_circular); + // Note that the margins are made symmetrical here. Since they're symmetrical we choose + // the smaller value to maximize usable width. + final int margin = (int) Math.min(context.getResources().getDimension( + R.dimen.round_content_padding_left), context.getResources().getDimension( + R.dimen.round_content_padding_right)); + params.leftMargin = margin; + params.rightMargin = margin; + params.gravity = Gravity.CENTER_HORIZONTAL; + header.setLayoutParams(params); + + if (header instanceof TextView) { + ((TextView) header).setGravity(Gravity.CENTER); + } + + final int leftPadding = (int) context.getResources().getDimension( + R.dimen.round_content_padding_left); + final int rightPadding = (int) context.getResources().getDimension( + R.dimen.round_content_padding_right); + final int topPadding = (int) context.getResources().getDimension( + R.dimen.settings_wearable_list_view_vertical_padding_round); + final int bottomPadding = (int) context.getResources().getDimension( + R.dimen.settings_wearable_list_view_vertical_padding_round); + wheel.setPadding(leftPadding, topPadding, rightPadding, mChinOffset + bottomPadding); + wheel.setClipToPadding(false); + + wheel.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { + @Override + public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { + mChinOffset = insets.getSystemWindowInsetBottom(); + wheel.setPadding(leftPadding, topPadding, rightPadding, + mChinOffset + bottomPadding); + // This listener is invoked after each time we navigate to SettingsActivity and + // it keeps adding padding. We need to disable it after the first update. + v.setOnApplyWindowInsetsListener(null); + return insets.consumeSystemWindowInsets(); + } + }); + } else { + int leftPadding = (int) context.getResources().getDimension( + R.dimen.content_padding_left); + wheel.setPadding(leftPadding, wheel.getPaddingTop(), wheel.getPaddingRight(), + wheel.getPaddingBottom()); + } + } + + private void adjustHeaderSize() { + int length = mHeader.length(); + + if (length <= sCharLimitShortTitle) { + mHeader.setTextSize(TypedValue.COMPLEX_UNIT_PX, + getResources().getDimensionPixelSize( + R.dimen.setting_short_header_text_size)); + } else { + mHeader.setTextSize(TypedValue.COMPLEX_UNIT_PX, + getResources().getDimensionPixelSize( + R.dimen.setting_long_header_text_size)); + } + + boolean singleLine = length <= sCharLimitLine; + + float height = getResources().getDimension(R.dimen.settings_header_base_height); + if (!singleLine) { + height += getResources().getDimension(R.dimen.setting_header_extra_line_height); + } + + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mHeader.getLayoutParams(); + params.height = (int) height; + final Context context = getContext(); + if (!singleLine) { + // Make the top margin a little bit smaller so there is more space for the title. + if (ViewUtils.getIsCircular(context)) { + params.topMargin = getResources().getDimensionPixelSize( + R.dimen.settings_header_top_margin_circular_multiline); + } else { + params.topMargin = getResources().getDimensionPixelSize( + R.dimen.settings_header_top_margin_multiline); + } + } else { + if (ViewUtils.getIsCircular(context)) { + params.topMargin = getResources().getDimensionPixelSize( + R.dimen.settings_header_top_margin_circular); + } else { + params.topMargin = getResources().getDimensionPixelSize( + R.dimen.settings_header_top_margin); + } + } + mHeader.setLayoutParams(params); + } + + +} diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java b/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java new file mode 100644 index 00000000..1e6a3795 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.wear.settings; + +import android.content.Context; +import android.content.res.Resources; +import android.support.wearable.view.CircledImageView; +import android.support.wearable.view.WearableListView; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.style.TextAppearanceSpan; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; + +import java.util.ArrayList; + +public final class PermissionsSettingsAdapter extends SettingsAdapter { + private Resources mRes; + + public PermissionsSettingsAdapter(Context context) { + super(context, R.layout.permissions_settings_item); + mRes = context.getResources(); + } + + @Override + public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new PermissionsViewHolder(new SettingsAdapter.SettingsItem(parent.getContext())); + } + + @Override + public void onBindViewHolder(WearableListView.ViewHolder holder, int position) { + super.onBindViewHolder(holder, position); + PermissionsViewHolder viewHolder = (PermissionsViewHolder) holder; + AppPermissionGroup group = get(position).data; + + if (group.isPolicyFixed()) { + viewHolder.imageView.setEnabled(false); + viewHolder.textView.setEnabled(false); + viewHolder.state.setEnabled(false); + viewHolder.state.setText( + mRes.getString(R.string.permission_summary_enforced_by_policy)); + } else { + viewHolder.imageView.setEnabled(true); + viewHolder.textView.setEnabled(true); + viewHolder.state.setEnabled(true); + + if (group.areRuntimePermissionsGranted()) { + viewHolder.state.setText(R.string.generic_enabled); + } else { + viewHolder.state.setText(R.string.generic_disabled); + } + } + } + + private static final class PermissionsViewHolder extends SettingsAdapter.SettingsItemHolder { + public final TextView state; + + public PermissionsViewHolder(View view) { + super(view); + state = (TextView) view.findViewById(R.id.state); + } + } +} + diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java b/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java index 9216cbbf..2ef7a2a4 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/SettingsAdapter.java @@ -228,7 +228,7 @@ public class SettingsAdapter extends WearableListView.Adapter { protected static class SettingsItemHolder extends ExtendedViewHolder { public final CircledImageView imageView; - final TextView textView; + public final TextView textView; public SettingsItemHolder(View itemView) { super(itemView); -- cgit v1.2.3 From eaa6612490e571cb7cd6f090d027202f27ade74c Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Wed, 16 Sep 2015 00:50:55 -0700 Subject: Fix build break Another bad merge. Util was changed to isWear, not isWatch Change-Id: If15036edbb09114a17fd5993c145433b5c274ec8 --- .../packageinstaller/permission/ui/ManagePermissionsActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java index a61a862d..f7fcec5e 100644 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java @@ -51,7 +51,7 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { return; } - if (DeviceUtils.isWatch(this)) { + if (DeviceUtils.isWear(this)) { fragment = AppPermissionsFragmentWear.newInstance(packageName); } else { fragment = AppPermissionsFragment.newInstance(packageName); -- cgit v1.2.3 From 736c82ba577f8f972ea4277357ae440b192dcd13 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Wed, 16 Sep 2015 17:51:10 -0700 Subject: Permissions Settings UI updates Changes: - Moved non-system permission groups to the end of the list - Updated "disabled" look to match non-center looked - Fixed state string alpha to match other alphas when not centered - Minor code clean ups BUG: 24132393 BUG: 24132470 BUG: 24131983 Change-Id: I65a64d59e6fff8e0cbdaaa3da4e4a79987d6e624 --- .../ui/wear/AppPermissionsFragmentWear.java | 26 +++++++++++++--- .../permission/ui/wear/TitledSettingsFragment.java | 23 +++++--------- .../wear/settings/PermissionsSettingsAdapter.java | 36 ++++++++++++++++------ .../ui/wear/settings/SettingsAdapter.java | 23 ++------------ 4 files changed, 57 insertions(+), 51 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java index 9e54c7d5..1969c20d 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java +++ b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java @@ -66,7 +66,6 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); fragment.setArguments(arguments); return fragment; - } @Override @@ -126,11 +125,11 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { super.onViewCreated(view, savedInstanceState); if (mAppPermissions != null) { initializeLayout(mAdapter); - bindUi(mAppPermissions.getPackageInfo()); + bindHeader(mAppPermissions.getPackageInfo()); } } - private void bindUi(PackageInfo packageInfo) { + private void bindHeader(PackageInfo packageInfo) { Activity activity = getActivity(); PackageManager pm = activity.getPackageManager(); ApplicationInfo appInfo = packageInfo.applicationInfo; @@ -141,6 +140,8 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { private void initializePermissionGroupList() { final String packageName = mAppPermissions.getPackageInfo().packageName; List groups = mAppPermissions.getPermissionGroups(); + List> nonSystemGroups = new ArrayList<>(); + final int count = groups.size(); for (int i = 0; i < count; ++i) { final AppPermissionGroup group = groups.get(i); @@ -148,12 +149,27 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { continue; } + boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); + SettingsAdapter.Setting setting = new SettingsAdapter.Setting( group.getLabel(), getPermissionGroupIcon(group), i); setting.data = group; + + // The UI shows System settings first, then non-system settings + if (isPlatform) { + mAdapter.addSetting(setting); + } else { + nonSystemGroups.add(setting); + } + } + + // Now add the non-system settings to the end of the list + final int nonSystemCount = nonSystemGroups.size(); + for (int i = 0; i < nonSystemCount; ++i) { + final SettingsAdapter.Setting setting = nonSystemGroups.get(i); mAdapter.addSetting(setting); } } @@ -161,7 +177,7 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { @Override public void onPause() { super.onPause(); - logToggledGroups(); + logAndClearToggledGroups(); } @Override @@ -247,7 +263,7 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { } } - private void logToggledGroups() { + private void logAndClearToggledGroups() { if (mToggledGroups != null) { String packageName = mAppPermissions.getPackageInfo().packageName; SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); diff --git a/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java b/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java index a738decd..64f42d0d 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java +++ b/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java @@ -48,10 +48,9 @@ public abstract class TitledSettingsFragment extends Fragment implements protected TextView mHeader; protected WearableListView mWheel; - private static boolean sInitialized; - private static int sCharLimitShortTitle; - private static int sCharLimitLine; - private static int mChinOffset; + private int mCharLimitShortTitle; + private int mCharLimitLine; + private int mChinOffset; private TextWatcher mHeaderTextWatcher = new TextWatcher() { @Override @@ -64,7 +63,6 @@ public abstract class TitledSettingsFragment extends Fragment implements public void afterTextChanged(Editable editable) { adjustHeaderSize(); } - }; private void adjustHeaderTranslation() { @@ -88,11 +86,8 @@ public abstract class TitledSettingsFragment extends Fragment implements @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (!sInitialized) { - sCharLimitShortTitle = getResources().getInteger(R.integer.short_title_length); - sCharLimitLine = getResources().getInteger(R.integer.char_limit_per_line); - sInitialized = true; - } + mCharLimitShortTitle = getResources().getInteger(R.integer.short_title_length); + mCharLimitLine = getResources().getInteger(R.integer.char_limit_per_line); } @Override @@ -142,7 +137,7 @@ public abstract class TitledSettingsFragment extends Fragment implements positionOnCircular(getContext(), mHeader, mWheel); } - public static void positionOnCircular(Context context, View header, final ViewGroup wheel) { + public void positionOnCircular(Context context, View header, final ViewGroup wheel) { if (ViewUtils.getIsCircular(context)) { FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) header.getLayoutParams(); @@ -196,7 +191,7 @@ public abstract class TitledSettingsFragment extends Fragment implements private void adjustHeaderSize() { int length = mHeader.length(); - if (length <= sCharLimitShortTitle) { + if (length <= mCharLimitShortTitle) { mHeader.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize( R.dimen.setting_short_header_text_size)); @@ -206,7 +201,7 @@ public abstract class TitledSettingsFragment extends Fragment implements R.dimen.setting_long_header_text_size)); } - boolean singleLine = length <= sCharLimitLine; + boolean singleLine = length <= mCharLimitLine; float height = getResources().getDimension(R.dimen.settings_header_base_height); if (!singleLine) { @@ -236,6 +231,4 @@ public abstract class TitledSettingsFragment extends Fragment implements } mHeader.setLayoutParams(params); } - - } diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java b/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java index 1e6a3795..69629f01 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java @@ -18,23 +18,14 @@ package com.android.packageinstaller.permission.ui.wear.settings; import android.content.Context; import android.content.res.Resources; -import android.support.wearable.view.CircledImageView; import android.support.wearable.view.WearableListView; -import android.text.SpannableStringBuilder; -import android.text.Spanned; -import android.text.style.TextAppearanceSpan; -import android.util.Log; -import android.view.Gravity; import android.view.View; import android.view.ViewGroup; -import android.widget.FrameLayout; import android.widget.TextView; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.model.AppPermissionGroup; -import java.util.ArrayList; - public final class PermissionsSettingsAdapter extends SettingsAdapter { private Resources mRes; @@ -45,7 +36,7 @@ public final class PermissionsSettingsAdapter extends SettingsAdapter extends WearableListView.Adapter { private static final String TAG = "SettingsAdapter"; private final Context mContext; - protected static CharSequence generateLabelWithState( - Context context, int labelId, boolean enabled) { - return generateLabelWithState(context, labelId, R.string.generic_enabled, enabled); - } - - protected static CharSequence generateLabelWithState( - Context context, int labelId, int onId, boolean enabled) { - SpannableStringBuilder ssb = new SpannableStringBuilder(context.getString(labelId)); - ssb.append('\n'); - ssb.append( - context.getString(enabled ? onId : R.string.generic_disabled), - new TextAppearanceSpan(context, R.style.TextAppearance_Settings_Label_Large), - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - return ssb; - } - public static final class Setting { public static final int ID_INVALID = -1; @@ -240,8 +221,8 @@ public class SettingsAdapter extends WearableListView.Adapter { protected class SettingsItem extends FrameLayout implements ExtendedOnCenterProximityListener { - private final CircledImageView mImage; - private final TextView mText; + protected final CircledImageView mImage; + protected final TextView mText; public SettingsItem(Context context) { super(context); -- cgit v1.2.3 From 6161f39f583cf202969125757d78010bdc06457c Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Fri, 18 Sep 2015 17:18:10 -0700 Subject: Update Grant Permissions dialog to redlines Changes: - Refactor the ViewHandler a bit to be more generic. This should help with another upcoming Wear dialog. - Update assets for buttons - Add deny asset - Fix margins and other UI number tweaks BUG: 24133550 Change-Id: I330bdf3217fa5c554f71d9105a0475106b6f3e12 --- .../ui/GrantPermissionsWatchViewHandler.java | 53 ++-- .../ui/PermissionConfirmationViewHandler.java | 277 -------------------- .../ui/wear/ConfirmationViewHandler.java | 285 +++++++++++++++++++++ .../wear/settings/PermissionsSettingsAdapter.java | 2 +- 4 files changed, 321 insertions(+), 296 deletions(-) delete mode 100644 src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java create mode 100644 src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java index a3d3b805..242d0bce 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java @@ -2,6 +2,7 @@ package com.android.packageinstaller.permission.ui; import android.content.Context; import android.graphics.PixelFormat; +import android.graphics.drawable.Drawable; import android.graphics.drawable.Icon; import android.os.Bundle; import android.util.Log; @@ -9,18 +10,19 @@ import android.view.View; import android.view.WindowManager; import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.ui.wear.ConfirmationViewHandler; /** * Watch-specific view handler for the grant permissions activity. */ -final class GrantPermissionsWatchViewHandler extends PermissionConfirmationViewHandler +final class GrantPermissionsWatchViewHandler extends ConfirmationViewHandler implements GrantPermissionsViewHandler { private static final String TAG = "GrantPermsWatchViewH"; private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; private final Context mContext; - + private ResultListener mResultListener; private String mGroupName; @@ -101,56 +103,71 @@ final class GrantPermissionsWatchViewHandler extends PermissionConfirmationViewH } } - @Override // PermissionConfirmationViewHandler - public void onAllow() { + @Override // ConfirmationViewHandler + public void onButton1() { onClick(true /* granted */, false /* doNotAskAgain */); } - @Override // PermissionConfirmationViewHandler - public void onDeny() { + @Override // ConfirmationViewHandler + public void onButton2() { onClick(false /* granted */, false /* doNotAskAgain */); } - @Override // PermissionConfirmationViewHandler - public void onDenyDoNotAskAgain() { + @Override // ConfirmationViewHandler + public void onButton3() { onClick(false /* granted */, true /* doNotAskAgain */); } - @Override // PermissionConfirmationViewHandler + @Override // ConfirmationViewHandler public CharSequence getCurrentPageText() { return mCurrentPageText; } - @Override // PermissionConfirmationViewHandler + @Override // ConfirmationViewHandler public Icon getPermissionIcon() { return mIcon; } - @Override // PermissionConfirmationViewHandler + @Override // ConfirmationViewHandler public CharSequence getMessage() { return mMessage; } - @Override // PermissionConfirmationViewHandler + @Override // ConfirmationViewHandler public int getButtonBarMode() { return mShowDoNotAsk ? MODE_VERTICAL_BUTTONS : MODE_HORIZONTAL_BUTTONS; } - @Override // PermissionConfirmationViewHandler - public CharSequence getVerticalAllowText() { + @Override // ConfirmationViewHandler + public CharSequence getVerticalButton1Text() { return mContext.getString(R.string.grant_dialog_button_allow); } - @Override // PermissionConfirmationViewHandler - public CharSequence getVerticalDenyText() { + @Override // ConfirmationViewHandler + public CharSequence getVerticalButton2Text() { return mContext.getString(R.string.grant_dialog_button_deny); } - @Override // PermissionConfirmationViewHandler - public CharSequence getVerticalDenyDoNotAskAgainText() { + @Override // ConfirmationViewHandler + public CharSequence getVerticalButton3Text() { return mContext.getString(R.string.grant_dialog_button_deny_dont_ask_again); } + @Override // ConfirmationViewHandler + public Drawable getVerticalButton1Icon(){ + return mContext.getDrawable(R.drawable.confirm_button); + } + + @Override // ConfirmationViewHandler + public Drawable getVerticalButton2Icon(){ + return mContext.getDrawable(R.drawable.cancel_button); + } + + @Override // ConfirmationViewHandler + public Drawable getVerticalButton3Icon(){ + return mContext.getDrawable(R.drawable.deny_button); + } + private void onClick(boolean granted, boolean doNotAskAgain) { if (mResultListener != null) { mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); diff --git a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java deleted file mode 100644 index 3e32c7f2..00000000 --- a/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java +++ /dev/null @@ -1,277 +0,0 @@ -package com.android.packageinstaller.permission.ui; - -import android.animation.ObjectAnimator; -import android.animation.PropertyValuesHolder; -import android.content.Context; -import android.graphics.drawable.Icon; -import android.os.Handler; -import android.os.Message; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewTreeObserver; -import android.view.animation.AnimationUtils; -import android.view.animation.Interpolator; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.ScrollView; -import android.widget.TextView; - -import com.android.packageinstaller.R; - -public abstract class PermissionConfirmationViewHandler implements - Handler.Callback, - View.OnClickListener, - ViewTreeObserver.OnScrollChangedListener { - public static final int MODE_HORIZONTAL_BUTTONS = 0; - public static final int MODE_VERTICAL_BUTTONS = 1; - - private static final int MSG_HIDE_BUTTON_BAR = 1001; - private static final long HIDE_ANIM_DURATION = 500; - - private View mRoot; - private TextView mCurrentPageText; - private ImageView mIcon; - private TextView mMessage; - private ScrollView mScrollingContainer; - private ViewGroup mContent; - private ViewGroup mHorizontalButtonBar; - private ViewGroup mVerticalButtonBar; - private Button mVerticalAllow; - private Button mVerticalDeny; - private Button mVerticalDenyDoNotAskAgain; - private View mButtonBarContainer; - - private Context mContext; - - private Handler mHideHandler; - private Interpolator mInterpolator; - private float mButtonBarFloatingHeight; - private ObjectAnimator mButtonBarAnimator; - private float mCurrentTranslation; - private boolean mHiddenBefore; - - // TODO: Move these into a builder - public abstract void onAllow(); - public abstract void onDeny(); - public abstract void onDenyDoNotAskAgain(); - public abstract CharSequence getVerticalAllowText(); - public abstract CharSequence getVerticalDenyText(); - public abstract CharSequence getVerticalDenyDoNotAskAgainText(); - public abstract CharSequence getCurrentPageText(); - public abstract Icon getPermissionIcon(); - public abstract CharSequence getMessage(); - - public PermissionConfirmationViewHandler(Context context) { - mContext = context; - } - - public View createView() { - mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null); - - mMessage = (TextView) mRoot.findViewById(R.id.message); - mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text); - mIcon = (ImageView) mRoot.findViewById(R.id.icon); - mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container); - mContent = (ViewGroup) mRoot.findViewById(R.id.content); - mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container); - mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar); - mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar); - - Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button); - Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button); - horizontalAllow.setOnClickListener(this); - horizontalDeny.setOnClickListener(this); - - mVerticalAllow = (Button) mRoot.findViewById(R.id.vertical_allow_button); - mVerticalDeny = (Button) mRoot.findViewById(R.id.vertical_deny_button); - mVerticalDenyDoNotAskAgain = - (Button) mRoot.findViewById(R.id.vertical_deny_do_not_ask_again_button); - mVerticalAllow.setOnClickListener(this); - mVerticalDeny.setOnClickListener(this); - mVerticalDenyDoNotAskAgain.setOnClickListener(this); - - mInterpolator = AnimationUtils.loadInterpolator(mContext, - android.R.interpolator.fast_out_slow_in); - mButtonBarFloatingHeight = mContext.getResources().getDimension( - R.dimen.conf_diag_floating_height); - mHideHandler = new Handler(this); - - return mRoot; - } - - /** - * Child class should override this for other modes. Call invalidate() to update the UI to the - * new button mode. - * @return The current mode the layout should use for the buttons - */ - public int getButtonBarMode() { - return MODE_HORIZONTAL_BUTTONS; - } - - public void invalidate() { - CharSequence currentPageText = getCurrentPageText(); - if (!TextUtils.isEmpty(currentPageText)) { - mCurrentPageText.setText(currentPageText); - mCurrentPageText.setVisibility(View.VISIBLE); - } else { - mCurrentPageText.setVisibility(View.INVISIBLE); - } - - Icon icon = getPermissionIcon(); - if (icon != null) { - mIcon.setImageIcon(icon); - mIcon.setVisibility(View.VISIBLE); - } else { - mIcon.setVisibility(View.INVISIBLE); - } - mMessage.setText(getMessage()); - - switch (getButtonBarMode()) { - case MODE_HORIZONTAL_BUTTONS: - mHorizontalButtonBar.setVisibility(View.VISIBLE); - mVerticalButtonBar.setVisibility(View.GONE); - break; - case MODE_VERTICAL_BUTTONS: - mHorizontalButtonBar.setVisibility(View.GONE); - mVerticalButtonBar.setVisibility(View.VISIBLE); - mVerticalAllow.setText(getVerticalAllowText()); - mVerticalDeny.setText(getVerticalDenyText()); - mVerticalDenyDoNotAskAgain.setText(getVerticalDenyDoNotAskAgainText()); - - mVerticalAllow.setCompoundDrawablesWithIntrinsicBounds( - mContext.getDrawable(R.drawable.confirm_button), null, null, null); - mVerticalDeny.setCompoundDrawablesWithIntrinsicBounds( - mContext.getDrawable(R.drawable.cancel_button), null, null, null); - mVerticalDenyDoNotAskAgain.setCompoundDrawablesWithIntrinsicBounds( - mContext.getDrawable(R.drawable.cancel_button), null, null, null); - break; - } - - mScrollingContainer.scrollTo(0, 0); - - mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this); - - mRoot.getViewTreeObserver().addOnGlobalLayoutListener( - new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - // Setup Button animation. - // pop the button bar back to full height, stop all animation - if (mButtonBarAnimator != null) { - mButtonBarAnimator.cancel(); - } - - // In order to fake the buttons peeking at the bottom, need to do set the - // padding properly. - if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { - mContent.setPadding(0, 0, 0, mButtonBarContainer.getHeight()); - } - - // stop any calls to hide the button bar in the future - mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); - mHiddenBefore = false; - - // determine which mode the scrolling should work at. - if (mContent.getHeight() > mScrollingContainer.getHeight()) { - mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); - mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); - generateButtonBarAnimator(mButtonBarContainer.getHeight(), 0, 0, - mButtonBarFloatingHeight, 1000); - } else { - mButtonBarContainer.setTranslationY(0); - mButtonBarContainer.setTranslationZ(0); - } - mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this); - } - }); - - } - - @Override - public void onClick(View v) { - int id = v.getId(); - switch (id) { - case R.id.horizontal_allow_button: - case R.id.vertical_allow_button: - onAllow(); - break; - case R.id.horizontal_deny_button: - case R.id.vertical_deny_button: - onDeny(); - break; - case R.id.vertical_deny_do_not_ask_again_button: - onDenyDoNotAskAgain(); - break; - } - } - - @Override - public boolean handleMessage (Message msg) { - switch (msg.what) { - case MSG_HIDE_BUTTON_BAR: - hideButtonBar(); - return true; - } - return false; - } - - @Override - public void onScrollChanged() { - mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); - hideButtonBar(); - } - - private void hideButtonBar() { - // get the offset to the top of the button bar - int offset = mScrollingContainer.getHeight() + mButtonBarContainer.getHeight() - - mContent.getHeight() + Math.max(mScrollingContainer.getScrollY(), 0); - int translationY = offset > 0 ? mButtonBarContainer.getHeight() - offset : - mButtonBarContainer.getHeight(); - - if (!mHiddenBefore || mButtonBarAnimator == null) { - // hasn't hidden the bar yet, just hide now to the right height - generateButtonBarAnimator( - mButtonBarContainer.getTranslationY(), translationY, - mButtonBarFloatingHeight, 0, HIDE_ANIM_DURATION); - } else if (mButtonBarAnimator.isRunning()) { - // we are animating the button bar closing, change to animate to the right place - if (Math.abs(mCurrentTranslation - translationY) > 1e-2f) { - mButtonBarAnimator.cancel(); // stop current animation - - if (Math.abs(mButtonBarContainer.getTranslationY() - translationY) > 1e-2f) { - long duration = Math.max((long) ( - (float) HIDE_ANIM_DURATION - * (translationY - mButtonBarContainer.getTranslationY()) - / mButtonBarContainer.getHeight()), 0); - generateButtonBarAnimator( - mButtonBarContainer.getTranslationY(), translationY, - mButtonBarFloatingHeight, 0, duration); - } else { - mButtonBarContainer.setTranslationY(translationY); - mButtonBarContainer.setTranslationZ(0); - } - } - } else { - // not currently animating, have already hidden, snap to the right offset - mButtonBarContainer.setTranslationY(translationY); - mButtonBarContainer.setTranslationZ(0); - } - - mHiddenBefore = true; - } - - private void generateButtonBarAnimator( - float startY, float endY, float startZ, float endZ, long duration) { - mButtonBarAnimator = - ObjectAnimator.ofPropertyValuesHolder( - mButtonBarContainer, - PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, startY, endY), - PropertyValuesHolder.ofFloat(View.TRANSLATION_Z, startZ, endZ)); - mCurrentTranslation = endY; - mButtonBarAnimator.setDuration(duration); - mButtonBarAnimator.setInterpolator(mInterpolator); - mButtonBarAnimator.start(); - } -} diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java new file mode 100644 index 00000000..d28866ef --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -0,0 +1,285 @@ +package com.android.packageinstaller.permission.ui.wear; + +import android.animation.ObjectAnimator; +import android.animation.PropertyValuesHolder; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.Icon; +import android.os.Handler; +import android.os.Message; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.view.animation.AnimationUtils; +import android.view.animation.Interpolator; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ScrollView; +import android.widget.TextView; + +import com.android.packageinstaller.R; + +public abstract class ConfirmationViewHandler implements + Handler.Callback, + View.OnClickListener, + ViewTreeObserver.OnScrollChangedListener { + public static final int MODE_HORIZONTAL_BUTTONS = 0; + public static final int MODE_VERTICAL_BUTTONS = 1; + + private static final int MSG_HIDE_BUTTON_BAR = 1001; + private static final long HIDE_ANIM_DURATION = 500; + + private View mRoot; + private TextView mCurrentPageText; + private ImageView mIcon; + private TextView mMessage; + private ScrollView mScrollingContainer; + private ViewGroup mContent; + private ViewGroup mHorizontalButtonBar; + private ViewGroup mVerticalButtonBar; + private Button mVerticalButton1; + private Button mVerticalButton2; + private Button mVerticalButton3; + private View mButtonBarContainer; + + private Context mContext; + + private Handler mHideHandler; + private Interpolator mInterpolator; + private float mButtonBarFloatingHeight; + private ObjectAnimator mButtonBarAnimator; + private float mCurrentTranslation; + private boolean mHiddenBefore; + + // TODO: Move these into a builder + /** In the 2 button layout, this is allow button */ + public abstract void onButton1(); + /** In the 2 button layout, this is deny button */ + public abstract void onButton2(); + public abstract void onButton3(); + public abstract CharSequence getVerticalButton1Text(); + public abstract CharSequence getVerticalButton2Text(); + public abstract CharSequence getVerticalButton3Text(); + public abstract Drawable getVerticalButton1Icon(); + public abstract Drawable getVerticalButton2Icon(); + public abstract Drawable getVerticalButton3Icon(); + public abstract CharSequence getCurrentPageText(); + public abstract Icon getPermissionIcon(); + public abstract CharSequence getMessage(); + + public ConfirmationViewHandler(Context context) { + mContext = context; + } + + public View createView() { + mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null); + + mMessage = (TextView) mRoot.findViewById(R.id.message); + mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text); + mIcon = (ImageView) mRoot.findViewById(R.id.icon); + mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container); + mContent = (ViewGroup) mRoot.findViewById(R.id.content); + mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container); + mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar); + mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar); + + Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button); + Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button); + horizontalAllow.setOnClickListener(this); + horizontalDeny.setOnClickListener(this); + + mVerticalButton1 = (Button) mRoot.findViewById(R.id.vertical_button1); + mVerticalButton2 = (Button) mRoot.findViewById(R.id.vertical_button2); + mVerticalButton3 = (Button) mRoot.findViewById(R.id.vertical_button3); + mVerticalButton1.setOnClickListener(this); + mVerticalButton2.setOnClickListener(this); + mVerticalButton3.setOnClickListener(this); + + mInterpolator = AnimationUtils.loadInterpolator(mContext, + android.R.interpolator.fast_out_slow_in); + mButtonBarFloatingHeight = mContext.getResources().getDimension( + R.dimen.conf_diag_floating_height); + mHideHandler = new Handler(this); + + return mRoot; + } + + /** + * Child class should override this for other modes. Call invalidate() to update the UI to the + * new button mode. + * @return The current mode the layout should use for the buttons + */ + public int getButtonBarMode() { + return MODE_HORIZONTAL_BUTTONS; + } + + public void invalidate() { + CharSequence currentPageText = getCurrentPageText(); + if (!TextUtils.isEmpty(currentPageText)) { + mCurrentPageText.setText(currentPageText); + mCurrentPageText.setVisibility(View.VISIBLE); + } else { + mCurrentPageText.setVisibility(View.GONE); + } + + Icon icon = getPermissionIcon(); + if (icon != null) { + mIcon.setImageIcon(icon); + mIcon.setVisibility(View.VISIBLE); + } else { + mIcon.setVisibility(View.GONE); + } + mMessage.setText(getMessage()); + + switch (getButtonBarMode()) { + case MODE_HORIZONTAL_BUTTONS: + mHorizontalButtonBar.setVisibility(View.VISIBLE); + mVerticalButtonBar.setVisibility(View.GONE); + break; + case MODE_VERTICAL_BUTTONS: + mHorizontalButtonBar.setVisibility(View.GONE); + mVerticalButtonBar.setVisibility(View.VISIBLE); + mVerticalButton1.setText(getVerticalButton1Text()); + mVerticalButton2.setText(getVerticalButton2Text()); + mVerticalButton3.setText(getVerticalButton3Text()); + + mVerticalButton1.setCompoundDrawablesWithIntrinsicBounds( + getVerticalButton1Icon(), null, null, null); + mVerticalButton2.setCompoundDrawablesWithIntrinsicBounds( + getVerticalButton2Icon(), null, null, null); + mVerticalButton3.setCompoundDrawablesWithIntrinsicBounds( + getVerticalButton3Icon(), null, null, null); + break; + } + + mScrollingContainer.scrollTo(0, 0); + + mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this); + + mRoot.getViewTreeObserver().addOnGlobalLayoutListener( + new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + // Setup Button animation. + // pop the button bar back to full height, stop all animation + if (mButtonBarAnimator != null) { + mButtonBarAnimator.cancel(); + } + + // In order to fake the buttons peeking at the bottom, need to do set the + // padding properly. + if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { + mContent.setPadding(0, 0, 0, mButtonBarContainer.getHeight()); + } + + // stop any calls to hide the button bar in the future + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + mHiddenBefore = false; + + // determine which mode the scrolling should work at. + if (mContent.getHeight() > mScrollingContainer.getHeight()) { + mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); + mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); + generateButtonBarAnimator(mButtonBarContainer.getHeight(), 0, 0, + mButtonBarFloatingHeight, 1000); + } else { + mButtonBarContainer.setTranslationY(0); + mButtonBarContainer.setTranslationZ(0); + } + mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + + } + + @Override + public void onClick(View v) { + int id = v.getId(); + switch (id) { + case R.id.horizontal_allow_button: + case R.id.vertical_button1: + onButton1(); + break; + case R.id.horizontal_deny_button: + case R.id.vertical_button2: + onButton2(); + break; + case R.id.vertical_button3: + onButton3(); + break; + } + } + + @Override + public boolean handleMessage (Message msg) { + switch (msg.what) { + case MSG_HIDE_BUTTON_BAR: + hideButtonBar(); + return true; + } + return false; + } + + @Override + public void onScrollChanged() { + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + hideButtonBar(); + } + + private void hideButtonBar() { + // get the offset to the top of the button bar + int offset = mScrollingContainer.getHeight() + mButtonBarContainer.getHeight() - + mContent.getHeight() + Math.max(mScrollingContainer.getScrollY(), 0); + // The desired margin space between the button bar and the bottom of the dialog text + int topMargin = mContext.getResources().getDimensionPixelSize( + R.dimen.conf_diag_button_container_top_margin); + int translationY = topMargin + (offset > 0 ? + mButtonBarContainer.getHeight() - offset : mButtonBarContainer.getHeight()); + + if (!mHiddenBefore || mButtonBarAnimator == null) { + // hasn't hidden the bar yet, just hide now to the right height + generateButtonBarAnimator( + mButtonBarContainer.getTranslationY(), translationY, + mButtonBarFloatingHeight, 0, HIDE_ANIM_DURATION); + } else if (mButtonBarAnimator.isRunning()) { + // we are animating the button bar closing, change to animate to the right place + if (Math.abs(mCurrentTranslation - translationY) > 1e-2f) { + mButtonBarAnimator.cancel(); // stop current animation + + if (Math.abs(mButtonBarContainer.getTranslationY() - translationY) > 1e-2f) { + long duration = Math.max((long) ( + (float) HIDE_ANIM_DURATION + * (translationY - mButtonBarContainer.getTranslationY()) + / mButtonBarContainer.getHeight()), 0); + generateButtonBarAnimator( + mButtonBarContainer.getTranslationY(), translationY, + mButtonBarFloatingHeight, 0, duration); + } else { + mButtonBarContainer.setTranslationY(translationY); + mButtonBarContainer.setTranslationZ(0); + } + } + } else { + // not currently animating, have already hidden, snap to the right offset + mButtonBarContainer.setTranslationY(translationY); + mButtonBarContainer.setTranslationZ(0); + } + + mHiddenBefore = true; + } + + private void generateButtonBarAnimator( + float startY, float endY, float startZ, float endZ, long duration) { + mButtonBarAnimator = + ObjectAnimator.ofPropertyValuesHolder( + mButtonBarContainer, + PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, startY, endY), + PropertyValuesHolder.ofFloat(View.TRANSLATION_Z, startZ, endZ)); + mCurrentTranslation = endY; + mButtonBarAnimator.setDuration(duration); + mButtonBarAnimator.setInterpolator(mInterpolator); + mButtonBarAnimator.start(); + } +} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java b/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java index 69629f01..0e0adcbb 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java +++ b/src/com/android/packageinstaller/permission/ui/wear/settings/PermissionsSettingsAdapter.java @@ -77,7 +77,7 @@ public final class PermissionsSettingsAdapter extends SettingsAdapter Date: Tue, 22 Sep 2015 12:11:06 -0700 Subject: MNC Perms: Button bar animate to max half window height - Make sure that the button bar only animates to half way (max). - Also make the buttons scrollable Bug: 24265941 Bug: 24208967 Change-Id: I68d01c72658c0e7aff35b899b638da123c44d62e --- .../permission/ui/wear/ConfirmationViewHandler.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index d28866ef..b1ff52bc 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -182,8 +182,14 @@ public abstract class ConfirmationViewHandler implements if (mContent.getHeight() > mScrollingContainer.getHeight()) { mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); - generateButtonBarAnimator(mButtonBarContainer.getHeight(), 0, 0, - mButtonBarFloatingHeight, 1000); + int maxButtonBarHeight = 0; + if (mButtonBarContainer.getHeight() >= mRoot.getHeight() / 2) { + // If the ButtonBar is bigger than half the screen, then don't + // animate all the way. + maxButtonBarHeight = mRoot.getHeight() / 2; + } + generateButtonBarAnimator(mButtonBarContainer.getHeight(), + maxButtonBarHeight, 0, mButtonBarFloatingHeight, 1000); } else { mButtonBarContainer.setTranslationY(0); mButtonBarContainer.setTranslationZ(0); @@ -282,4 +288,4 @@ public abstract class ConfirmationViewHandler implements mButtonBarAnimator.setInterpolator(mInterpolator); mButtonBarAnimator.start(); } -} \ No newline at end of file +} -- cgit v1.2.3 From 4b7427563d334dab50eb77028a78e709ea58880b Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Mon, 21 Sep 2015 15:49:08 -0700 Subject: Add new dialog for warning confirmations This change updates the Wear warning dialog for when users try to change permissions for system apps and apps that are targeting old SDKs. Because Wear does not have a common dialog yet, I needed to create a new activity to hold our confirmation view. Changes: - Update logic to show Wear confirmation screen instead of using AlertDialog - Added WarningConfirmationActivity - Change font size for confirmation dialog - Make ConfirmationViewHandler support hiding one of the 3 vertical buttons - rename grant_permissions.xml to more generic confirmation_dialog BUG: 24132857 Change-Id: Id225e663f0a2cd7b99fb177f780a6a3d0da15214 --- .../ui/wear/AppPermissionsFragmentWear.java | 49 +++++---- .../ui/wear/ConfirmationViewHandler.java | 16 ++- .../ui/wear/WarningConfirmationActivity.java | 110 +++++++++++++++++++++ 3 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java index 1969c20d..6f1273bb 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java +++ b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java @@ -18,10 +18,7 @@ package com.android.packageinstaller.permission.ui.wear; import android.annotation.Nullable; import android.app.Activity; -import android.app.AlertDialog; import android.app.Fragment; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; @@ -51,11 +48,13 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { private static final String LOG_TAG = "ManagePermsFragment"; + private static final int WARNING_CONFIRMATION_REQUEST = 252; private List mToggledGroups; private AppPermissions mAppPermissions; private PermissionsSettingsAdapter mAdapter; private boolean mHasConfirmedRevoke; + private int mPendingPermGroupIndex = -1; public static AppPermissionsFragmentWear newInstance(String packageName) { return setPackageName(new AppPermissionsFragmentWear(), packageName); @@ -216,23 +215,12 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { } else { final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { - new AlertDialog.Builder(getContext()) - .setMessage(grantedByDefault ? R.string.system_warning - : R.string.old_sdk_deny_warning) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.grant_dialog_button_deny, - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - group.revokeRuntimePermissions(false); - if (!grantedByDefault) { - mHasConfirmedRevoke = true; - } - - updatePermissionGroupSetting(index); - } - }) - .show(); + mPendingPermGroupIndex = index; + Intent intent = new Intent(getActivity(), WarningConfirmationActivity.class); + intent.putExtra(WarningConfirmationActivity.EXTRA_WARNING_MESSAGE, + getString(grantedByDefault ? + R.string.system_warning : R.string.old_sdk_deny_warning)); + startActivityForResult(intent, WARNING_CONFIRMATION_REQUEST); } else { group.revokeRuntimePermissions(false); } @@ -241,6 +229,27 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { updatePermissionGroupSetting(index); } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == WARNING_CONFIRMATION_REQUEST) { + if (resultCode == Activity.RESULT_OK) { + SettingsAdapter.Setting setting = + mAdapter.get(mPendingPermGroupIndex); + final AppPermissionGroup group = setting.data; + group.revokeRuntimePermissions(false); + if (!group.hasGrantedByDefaultPermission()) { + mHasConfirmedRevoke = true; + } + + updatePermissionGroupSetting(mPendingPermGroupIndex); + } + + mPendingPermGroupIndex = -1; + } else { + super.onActivityResult(requestCode, resultCode, data); + } + } + private void updatePermissionGroupSetting(int index) { SettingsAdapter.Setting setting = mAdapter.get(index); AppPermissionGroup group = setting.data; diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index d28866ef..7cfffa00 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -74,7 +74,7 @@ public abstract class ConfirmationViewHandler implements } public View createView() { - mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null); + mRoot = LayoutInflater.from(mContext).inflate(R.layout.confirmation_dialog, null); mMessage = (TextView) mRoot.findViewById(R.id.message); mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text); @@ -141,16 +141,24 @@ public abstract class ConfirmationViewHandler implements case MODE_VERTICAL_BUTTONS: mHorizontalButtonBar.setVisibility(View.GONE); mVerticalButtonBar.setVisibility(View.VISIBLE); + mVerticalButton1.setText(getVerticalButton1Text()); mVerticalButton2.setText(getVerticalButton2Text()); - mVerticalButton3.setText(getVerticalButton3Text()); mVerticalButton1.setCompoundDrawablesWithIntrinsicBounds( getVerticalButton1Icon(), null, null, null); mVerticalButton2.setCompoundDrawablesWithIntrinsicBounds( getVerticalButton2Icon(), null, null, null); - mVerticalButton3.setCompoundDrawablesWithIntrinsicBounds( - getVerticalButton3Icon(), null, null, null); + + CharSequence verticalButton3Text = getVerticalButton3Text(); + if (TextUtils.isEmpty(verticalButton3Text)) { + mVerticalButton3.setVisibility(View.GONE); + } else { + mVerticalButton3.setText(getVerticalButton3Text()); + mVerticalButton3.setCompoundDrawablesWithIntrinsicBounds( + getVerticalButton3Icon(), null, null, null); + } + break; } diff --git a/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java b/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java new file mode 100644 index 00000000..e26dabd6 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java @@ -0,0 +1,110 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.wear; + +import android.app.Activity; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.Icon; +import android.os.Bundle; + +import com.android.packageinstaller.R; + +public final class WarningConfirmationActivity extends Activity { + public final static String EXTRA_WARNING_MESSAGE = "EXTRA_WARNING_MESSAGE"; + + private ConfirmationViewHandler mViewHandler; + private String mMessage; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mMessage = getIntent().getStringExtra(EXTRA_WARNING_MESSAGE); + + mViewHandler = new ConfirmationViewHandler(this) { + @Override // ConfirmationViewHandler + public int getButtonBarMode() { + return MODE_VERTICAL_BUTTONS; + } + + @Override + public void onButton1() { + setResult(Activity.RESULT_CANCELED); + finish(); + } + + @Override + public void onButton2() { + setResult(Activity.RESULT_OK); + finish(); + } + + @Override + public void onButton3() { + // no-op + } + + @Override + public CharSequence getVerticalButton1Text() { + return getString(R.string.cancel); + } + + @Override + public CharSequence getVerticalButton2Text() { + return getString(R.string.grant_dialog_button_deny); + } + + @Override + public CharSequence getVerticalButton3Text() { + return null; + } + + @Override + public Drawable getVerticalButton1Icon() { + return getDrawable(R.drawable.cancel_button); + } + + @Override + public Drawable getVerticalButton2Icon() { + return getDrawable(R.drawable.confirm_button); + } + + @Override + public Drawable getVerticalButton3Icon() { + return null; + } + + @Override + public CharSequence getCurrentPageText() { + return null; + } + + @Override + public Icon getPermissionIcon() { + return null; + } + + @Override + public CharSequence getMessage() { + return mMessage; + } + }; + + setContentView(mViewHandler.createView()); + mViewHandler.invalidate(); + } +} -- cgit v1.2.3 From 0c49c07f1c83e9998fa0af2cd157633644358611 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Tue, 22 Sep 2015 17:05:02 -0700 Subject: Add enabled/disabled icons for permissions on Wear Adds proper enabled/disabled icons for permissions on Wear and adds the logic to flip between the two states when the permissions are toggled. BUG: 24132450 BUG: 24131441 BUG: 24277624 BUG: 24131983 Change-Id: Ie654c84fe3d648e47a76142a6f82c4348bb7fc0c --- .../ui/wear/AppPermissionsFragmentWear.java | 50 +++++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java index 6f1273bb..18db94c1 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java +++ b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java @@ -16,6 +16,7 @@ package com.android.packageinstaller.permission.ui.wear; +import android.Manifest; import android.annotation.Nullable; import android.app.Activity; import android.app.Fragment; @@ -281,7 +282,52 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { } private int getPermissionGroupIcon(AppPermissionGroup group) { - // TODO: Return the correct icon based on if permissions are granted - return group.getIconResId(); + String groupName = group.getName(); + boolean isEnabled = group.areRuntimePermissionsGranted(); + int resId; + + switch (groupName) { + case Manifest.permission_group.CALENDAR: + resId = isEnabled ? R.drawable.ic_permission_calendar + : R.drawable.ic_permission_calendardisable; + break; + case Manifest.permission_group.CAMERA: + resId = isEnabled ? R.drawable.ic_permission_camera + : R.drawable.ic_permission_cameradisable; + break; + case Manifest.permission_group.CONTACTS: + resId = isEnabled ? R.drawable.ic_permission_contact + : R.drawable.ic_permission_contactdisable; + break; + case Manifest.permission_group.LOCATION: + resId = isEnabled ? R.drawable.ic_permission_location + : R.drawable.ic_permission_locationdisable; + break; + case Manifest.permission_group.MICROPHONE: + resId = isEnabled ? R.drawable.ic_permission_mic + : R.drawable.ic_permission_micdisable; + break; + case Manifest.permission_group.PHONE: + resId = isEnabled ? R.drawable.ic_permission_call + : R.drawable.ic_permission_calldisable; + break; + case Manifest.permission_group.SENSORS: + resId = isEnabled ? R.drawable.ic_permission_sensor + : R.drawable.ic_permission_sensordisable; + break; + case Manifest.permission_group.SMS: + resId = isEnabled ? R.drawable.ic_permission_sms + : R.drawable.ic_permission_smsdisable; + break; + case Manifest.permission_group.STORAGE: + resId = isEnabled ? R.drawable.ic_permission_storage + : R.drawable.ic_permission_storagedisable; + break; + default: + resId = isEnabled ? R.drawable.ic_permission_shield + : R.drawable.ic_permission_shielddisable; + } + + return resId; } } -- cgit v1.2.3 From 0d0e466a67a4d73089286c4cb7ea7412cf193686 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Thu, 24 Sep 2015 16:23:55 -0700 Subject: Fix CTS test by renaming button ids to match BUG: 24264373 Change-Id: I9f04ff68dcd11f48df9331abf2f1775c95f31f99 --- .../permission/ui/wear/ConfirmationViewHandler.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index d4ed8327..277bada9 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -85,8 +85,8 @@ public abstract class ConfirmationViewHandler implements mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar); mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar); - Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button); - Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button); + Button horizontalAllow = (Button) mRoot.findViewById(R.id.permission_allow_button); + Button horizontalDeny = (Button) mRoot.findViewById(R.id.permission_deny_button); horizontalAllow.setOnClickListener(this); horizontalDeny.setOnClickListener(this); @@ -212,11 +212,11 @@ public abstract class ConfirmationViewHandler implements public void onClick(View v) { int id = v.getId(); switch (id) { - case R.id.horizontal_allow_button: + case R.id.permission_allow_button: case R.id.vertical_button1: onButton1(); break; - case R.id.horizontal_deny_button: + case R.id.permission_deny_button: case R.id.vertical_button2: onButton2(); break; -- cgit v1.2.3 From eb9a1b659375d01d4276db622358dafb68b9d4fc Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Mon, 28 Sep 2015 13:29:25 -0700 Subject: Block Install Intent for Wear devices - We did not support this feature on Android Wear before - The impact is unknown. For example, the Setting for UNKNOWN_SOURCES is not implemented in Android Wear Settings App. Bug: 24336381 Change-Id: I0fb4006d4769291c872e8f21407383a7c4a77b93 --- .../packageinstaller/InstallFlowAnalytics.java | 7 ++++++- .../packageinstaller/PackageInstallerActivity.java | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/InstallFlowAnalytics.java b/src/com/android/packageinstaller/InstallFlowAnalytics.java index 2fc6db37..4591f31c 100644 --- a/src/com/android/packageinstaller/InstallFlowAnalytics.java +++ b/src/com/android/packageinstaller/InstallFlowAnalytics.java @@ -85,6 +85,11 @@ public class InstallFlowAnalytics implements Parcelable { */ static final byte RESULT_PACKAGE_MANAGER_INSTALL_FAILED = 6; + /** + * Installation blocked since this feature is not allowed on Android Wear devices yet. + */ + static final byte RESULT_NOT_ALLOWED_ON_WEAR = 7; + private static final int FLAG_INSTALLS_FROM_UNKNOWN_SOURCES_PERMITTED = 1 << 0; private static final int FLAG_INSTALL_REQUEST_FROM_UNKNOWN_SOURCE = 1 << 1; private static final int FLAG_VERIFY_APPS_ENABLED = 1 << 2; @@ -600,4 +605,4 @@ public class InstallFlowAnalytics implements Parcelable { } return digest.digest(); } -} \ No newline at end of file +} diff --git a/src/com/android/packageinstaller/PackageInstallerActivity.java b/src/com/android/packageinstaller/PackageInstallerActivity.java index 6bcd80e4..868872a9 100644 --- a/src/com/android/packageinstaller/PackageInstallerActivity.java +++ b/src/com/android/packageinstaller/PackageInstallerActivity.java @@ -110,6 +110,7 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen private static final int DLG_INSTALL_ERROR = DLG_BASE + 4; private static final int DLG_ALLOW_SOURCE = DLG_BASE + 5; private static final int DLG_ADMIN_RESTRICTS_UNKNOWN_SOURCES = DLG_BASE + 6; + private static final int DLG_NOT_SUPPORTED_ON_WEAR = DLG_BASE + 7; private void startInstallConfirm() { TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); @@ -293,7 +294,7 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen Log.i(TAG, "Canceling installation"); finish(); } - }) + }) .setOnCancelListener(this) .create(); case DLG_INSTALL_ERROR : @@ -333,6 +334,18 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen }) .setOnCancelListener(this) .create(); + case DLG_NOT_SUPPORTED_ON_WEAR: + return new AlertDialog.Builder(this) + .setTitle(R.string.wear_not_allowed_dlg_title) + .setMessage(R.string.wear_not_allowed_dlg_text) + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + setResult(RESULT_OK); + finish(); + } + }) + .setOnCancelListener(this) + .create(); } return null; } @@ -478,6 +491,13 @@ public class PackageInstallerActivity extends Activity implements OnCancelListen mInstallFlowAnalytics.setAppVerifierInstalled(isAppVerifierInstalled()); mInstallFlowAnalytics.setPackageUri(mPackageURI.toString()); + if (DeviceUtils.isWear(this)) { + showDialogInner(DLG_NOT_SUPPORTED_ON_WEAR); + mInstallFlowAnalytics.setFlowFinished( + InstallFlowAnalytics.RESULT_NOT_ALLOWED_ON_WEAR); + return; + } + final String scheme = mPackageURI.getScheme(); if (scheme != null && !"file".equals(scheme) && !"package".equals(scheme)) { Log.w(TAG, "Unsupported scheme " + scheme); -- cgit v1.2.3 From 6a5762884adc64ea95bd206c46d60031428e977c Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Mon, 5 Oct 2015 12:28:07 -0700 Subject: Permissions redline fixes This fixes the button bar animation to work on round devices and also fixes the max height animation to make sure the vertical button layout animates to max height when there are only two buttons. Also adjust some redlines like margins on the buttons and updated some files that were missing our licensing header. Change-Id: Ifad4d0f92605be04db2d3c991b4e8a26eadb4b14 --- .../ui/GrantPermissionsWatchViewHandler.java | 2 +- .../ui/wear/ConfirmationViewHandler.java | 147 ++++++++++++++------- 2 files changed, 99 insertions(+), 50 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java index 242d0bce..21042f00 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsWatchViewHandler.java @@ -57,7 +57,7 @@ final class GrantPermissionsWatchViewHandler extends ConfirmationViewHandler @Override public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; - outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; + outLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; outLayoutParams.format = PixelFormat.OPAQUE; outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index 277bada9..db3340f6 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -8,6 +8,7 @@ import android.graphics.drawable.Icon; import android.os.Handler; import android.os.Message; import android.text.TextUtils; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -24,11 +25,15 @@ import com.android.packageinstaller.R; public abstract class ConfirmationViewHandler implements Handler.Callback, View.OnClickListener, - ViewTreeObserver.OnScrollChangedListener { + ViewTreeObserver.OnScrollChangedListener, + ViewTreeObserver.OnGlobalLayoutListener { + private static final String TAG = "ConfirmationViewHandler"; + public static final int MODE_HORIZONTAL_BUTTONS = 0; public static final int MODE_VERTICAL_BUTTONS = 1; - private static final int MSG_HIDE_BUTTON_BAR = 1001; + private static final int MSG_SHOW_BUTTON_BAR = 1001; + private static final int MSG_HIDE_BUTTON_BAR = 1002; private static final long HIDE_ANIM_DURATION = 500; private View mRoot; @@ -103,6 +108,9 @@ public abstract class ConfirmationViewHandler implements R.dimen.conf_diag_floating_height); mHideHandler = new Handler(this); + mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this); + mRoot.getViewTreeObserver().addOnGlobalLayoutListener(this); + return mRoot; } @@ -158,54 +166,41 @@ public abstract class ConfirmationViewHandler implements mVerticalButton3.setCompoundDrawablesWithIntrinsicBounds( getVerticalButton3Icon(), null, null, null); } - break; } mScrollingContainer.scrollTo(0, 0); - mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this); + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + mHideHandler.removeMessages(MSG_SHOW_BUTTON_BAR); + } + + @Override + public void onGlobalLayout() { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "onGlobalLayout"); + Log.d(TAG, " contentHeight: " + mContent.getHeight()); + } + + if (mButtonBarAnimator != null) { + mButtonBarAnimator.cancel(); + } + + // In order to fake the buttons peeking at the bottom, need to do set the + // padding properly. + if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { + mContent.setPadding(0, 0, 0, mButtonBarContainer.getHeight()); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, " set mContent.PaddingBottom: " + mButtonBarContainer.getHeight()); + } + } - mRoot.getViewTreeObserver().addOnGlobalLayoutListener( - new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - // Setup Button animation. - // pop the button bar back to full height, stop all animation - if (mButtonBarAnimator != null) { - mButtonBarAnimator.cancel(); - } - - // In order to fake the buttons peeking at the bottom, need to do set the - // padding properly. - if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { - mContent.setPadding(0, 0, 0, mButtonBarContainer.getHeight()); - } - - // stop any calls to hide the button bar in the future - mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); - mHiddenBefore = false; - - // determine which mode the scrolling should work at. - if (mContent.getHeight() > mScrollingContainer.getHeight()) { - mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); - mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); - int maxButtonBarHeight = 0; - if (mButtonBarContainer.getHeight() >= mRoot.getHeight() / 2) { - // If the ButtonBar is bigger than half the screen, then don't - // animate all the way. - maxButtonBarHeight = mRoot.getHeight() / 2; - } - generateButtonBarAnimator(mButtonBarContainer.getHeight(), - maxButtonBarHeight, 0, mButtonBarFloatingHeight, 1000); - } else { - mButtonBarContainer.setTranslationY(0); - mButtonBarContainer.setTranslationZ(0); - } - mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this); - } - }); + mButtonBarContainer.setTranslationY(mButtonBarContainer.getHeight()); + // Give everything a chance to render + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + mHideHandler.removeMessages(MSG_SHOW_BUTTON_BAR); + mHideHandler.sendEmptyMessageDelayed(MSG_SHOW_BUTTON_BAR, 50); } @Override @@ -229,6 +224,9 @@ public abstract class ConfirmationViewHandler implements @Override public boolean handleMessage (Message msg) { switch (msg.what) { + case MSG_SHOW_BUTTON_BAR: + showButtonBar(); + return true; case MSG_HIDE_BUTTON_BAR: hideButtonBar(); return true; @@ -237,21 +235,72 @@ public abstract class ConfirmationViewHandler implements } @Override - public void onScrollChanged() { + public void onScrollChanged () { mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); hideButtonBar(); } + private void showButtonBar() { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "showButtonBar"); + } + + // Setup Button animation. + // pop the button bar back to full height, stop all animation + if (mButtonBarAnimator != null) { + mButtonBarAnimator.cancel(); + } + + // stop any calls to hide the button bar in the future + mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); + mHiddenBefore = false; + + // Evaluate the max height the button bar can go + final int screenHeight = mRoot.getHeight(); + final int buttonBarHeight = mButtonBarContainer.getHeight(); + final int buttonBarMaxHeight = + Math.min(buttonBarHeight, screenHeight / 2); + + if (Log.isLoggable(TAG, Log.DEBUG)) { + final int contentHeight = mContent.getHeight() - buttonBarHeight; + Log.d(TAG, " screenHeight: " + screenHeight); + Log.d(TAG, " contentHeight: " + contentHeight); + Log.d(TAG, " buttonBarHeight: " + buttonBarHeight); + Log.d(TAG, " buttonBarMaxHeight: " + buttonBarMaxHeight); + } + + mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); + mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); + + generateButtonBarAnimator(buttonBarHeight, + buttonBarHeight - buttonBarMaxHeight, 0, mButtonBarFloatingHeight, 1000); + } + private void hideButtonBar() { - // get the offset to the top of the button bar - int offset = mScrollingContainer.getHeight() + mButtonBarContainer.getHeight() - - mContent.getHeight() + Math.max(mScrollingContainer.getScrollY(), 0); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "hideButtonBar"); + } + // The desired margin space between the button bar and the bottom of the dialog text - int topMargin = mContext.getResources().getDimensionPixelSize( + final int topMargin = mContext.getResources().getDimensionPixelSize( R.dimen.conf_diag_button_container_top_margin); - int translationY = topMargin + (offset > 0 ? + final int contentHeight = mContent.getHeight() + topMargin; + final int screenHeight = mRoot.getHeight(); + final int buttonBarHeight = mButtonBarContainer.getHeight(); + + final int offset = screenHeight + buttonBarHeight + - contentHeight + Math.max(mScrollingContainer.getScrollY(), 0); + final int translationY = (offset > 0 ? mButtonBarContainer.getHeight() - offset : mButtonBarContainer.getHeight()); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, " contentHeight: " + contentHeight); + Log.d(TAG, " buttonBarHeight: " + buttonBarHeight); + Log.d(TAG, " mContent.getPaddingBottom(): " + mContent.getPaddingBottom()); + Log.d(TAG, " mScrollingContainer.getScrollY(): " + mScrollingContainer.getScrollY()); + Log.d(TAG, " translationY: " + translationY); + } + if (!mHiddenBefore || mButtonBarAnimator == null) { // hasn't hidden the bar yet, just hide now to the right height generateButtonBarAnimator( -- cgit v1.2.3 From 0e3c7dbdfc98fb9e79c4b2564be3fbade8896acf Mon Sep 17 00:00:00 2001 From: Alex Hills Date: Tue, 13 Oct 2015 13:04:57 -0400 Subject: Modifies wear Permission title height to avoid clipping We were explicitly setting the height of the permission screen title, which wasn't 100% guaranteed to work in all languages. This change sets minHeight (to maintain UI consistency) instead, and changes the height to wrap_content to enable it to expand further if necessary. Bug:24774285 Bug:24391658 Change-Id: Icf25ad6a176537b0f34a30f53220c73fccec2d24 --- .../packageinstaller/permission/ui/wear/TitledSettingsFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java b/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java index 64f42d0d..ef7efb28 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java +++ b/src/com/android/packageinstaller/permission/ui/wear/TitledSettingsFragment.java @@ -207,9 +207,9 @@ public abstract class TitledSettingsFragment extends Fragment implements if (!singleLine) { height += getResources().getDimension(R.dimen.setting_header_extra_line_height); } + mHeader.setMinHeight((int) height); FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mHeader.getLayoutParams(); - params.height = (int) height; final Context context = getContext(); if (!singleLine) { // Make the top margin a little bit smaller so there is more space for the title. -- cgit v1.2.3 From 1ba9d112450b0f4ec8fcb6d1eb9e1d81d2278967 Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Mon, 12 Oct 2015 17:46:54 -0700 Subject: MNC Perms: Missed some cases for package installation - If {Companion App was targeting M, Companion was running L, watch is running M, watch app is targeting M}, we were not installing the app before this fix. - If {Companion App was targeting M, Companion was running M, watch is running M, watch app is targeting M}, we were not installing the app before this fix. Bug: 24813602 Bug: 23378733 Change-Id: I96f06adad2372517e57467d4af8d87dcb6068445 --- .../wear/WearPackageInstallerService.java | 112 ++++++++++++--------- 1 file changed, 64 insertions(+), 48 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java index 5ce0b9a1..ba83ea28 100644 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -362,18 +362,76 @@ public class WearPackageInstallerService extends Service { private boolean checkPermissions(PackageParser.Package pkg, int companionSdkVersion, int companionDeviceVersion, Uri permUri, List wearablePermissions, File apkFile) { + // If the Wear App is targeted for M-release, since the permission model has been changed, + // permissions may not be granted on the phone yet. We need a different flow for user to + // accept these permissions. + // + // Assumption: Code is running on E-release, so Wear is always running M. + // - Case 1: If the Wear App(WA) is targeting 23, always choose the M model (4 cases) + // - Case 2: Else if the Phone App(PA) is targeting 23 and Phone App(P) is running on M, + // show a Dialog so that the user can accept all perms (1 case) + // - Also show a warning to the developer if the watch is targeting M + // - Case 3: If Case 2 is false, then the behavior on the phone is pre-M. Stick to pre-M + // behavior on watch (as long as we don't hit case 1). + // - 3a: WA(22) PA(22) P(22) -> watch app is not targeting 23 + // - 3b: WA(22) PA(22) P(23) -> watch app is not targeting 23 + // - 3c: WA(22) PA(23) P(22) -> watch app is not targeting 23 + // - Case 4: We did not get Companion App's/Device's version, always show dialog to user to + // accept permissions. (This happens if the AndroidWear Companion App is really old). + boolean isWearTargetingM = + pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + if (isWearTargetingM) { // Case 1 + // Install the app if Wear App is ready for the new perms model. + return true; + } + + List unavailableWearablePerms = getWearPermsNotGrantedOnPhone(pkg.packageName, + permUri, wearablePermissions); + if (unavailableWearablePerms == null) { + return false; + } + + if (unavailableWearablePerms.size() == 0) { + // All permissions requested by the watch are already granted on the phone, no need + // to do anything. + return true; + } + + // Cases 2 and 4. + boolean isCompanionTargetingM = companionSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + boolean isCompanionRunningM = companionDeviceVersion > Build.VERSION_CODES.LOLLIPOP_MR1; + if (isCompanionTargetingM) { // Case 2 Warning + Log.w(TAG, "MNC: Wear app's targetSdkVersion should be at least 23, if " + + "phone app is targeting at least 23, will continue."); + } + if ((isCompanionTargetingM && isCompanionRunningM) || // Case 2 + companionSdkVersion == 0 || companionDeviceVersion == 0) { // Case 4 + startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); + } + + // Case 3a-3c. + return false; + } + + /** + * Given a {@string packageName} corresponding to a phone app, query the provider for all the + * perms that are granted. + * @return null if there is an error retrieving this info + * else, a list of all the wearable perms that are not in the list of granted perms of + * the phone. + */ + private List getWearPermsNotGrantedOnPhone(String packageName, Uri permUri, + List wearablePermissions) { if (permUri == null) { Log.e(TAG, "Permission URI is null"); - return false; + return null; } Cursor permCursor = getContentResolver().query(permUri, null, null, null, null); if (permCursor == null) { Log.e(TAG, "Could not get the cursor for the permissions"); - return false; + return null; } - final String packageName = pkg.packageName; - Set grantedPerms = new HashSet<>(); Set ungrantedPerms = new HashSet<>(); while(permCursor.moveToNext()) { @@ -408,49 +466,7 @@ public class WearPackageInstallerService extends Service { } } } - - - // If the Wear App is targeted for M-release, since the permission model has been changed, - // permissions may not be granted on the phone yet. We need a different flow for user to - // accept these permissions. - // - // Case 1: Companion App >= 23 (and running on M), Wear App targeting >= 23 - // - If Wear is running L (ie DMR1), show a dialog so that the user can accept all perms - // - If Wear is running M (ie E-release), use new permission model. - // Case 2: Companion App <= 22, Wear App targeting <= 22 - // - Default to old behavior. - // Case 3: Companion App <= 22, Wear App targeting >= 23 - // - If Wear is running L (ie DMR1), install the app as before. In effect, pretend - // like wear app is targeting 22. - // - If Wear is running M (ie E-release), use new permission model. - // Case 4: Companion App >= 23 (and running on M), Wear App targeting <= 22 - // - Show a warning below to the developer. - // - Show a dialog as in Case 1 with DMR1. This behavior will happen in E and DMR1. - // Case 5: We did not get Companion App's/Device's version (we have to guess here) - // - Show dialog if Wear App targeting >= 23 and Wear is not running M - if (unavailableWearablePerms.size() > 0) { - boolean isCompanionTargetingM = companionSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; - boolean isCompanionRunningM = companionDeviceVersion > Build.VERSION_CODES.LOLLIPOP_MR1; - boolean isWearTargetingM = - pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; - boolean isWearRunningM = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; - - if (companionSdkVersion == 0 || companionDeviceVersion == 0) { // Case 5 - if (isWearTargetingM && !isWearRunningM) { - startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); - } - } else if (isCompanionTargetingM && isCompanionRunningM) { - if (!isWearTargetingM) { // Case 4 - Log.w(TAG, "MNC: Wear app's targetSdkVersion should be at least 23, if phone " + - "app is targeting at least 23."); - startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); - } else if (!isWearRunningM) { // Case 1, part 1 - startPermsServiceForInstall(pkg, apkFile, unavailableWearablePerms); - } - } // Else, nothing to do. See explanation above. - } - - return unavailableWearablePerms.size() == 0; + return unavailableWearablePerms; } private void finishService(PowerManager.WakeLock lock, int startId) { @@ -472,7 +488,7 @@ public class WearPackageInstallerService extends Service { } private void startPermsServiceForInstall(final PackageParser.Package pkg, final File apkFile, - ArrayList unavailableWearablePerms) { + List unavailableWearablePerms) { final String packageName = pkg.packageName; Intent showPermsIntent = new Intent() -- cgit v1.2.3 From dc40bd77b1ab709de9d705c88f56ff61d618ed5c Mon Sep 17 00:00:00 2001 From: Danny Epstein Date: Mon, 19 Oct 2015 16:52:06 -0700 Subject: Add top padding to grant permission content. Add top padding to the content inside the scroll view for the grant permission dialog so that you can read the beginning of the text on round watches. Bug: 24961379 Change-Id: I9311fbaabd72bd8f8aee4f71845c1ae11bfeab8c --- .../packageinstaller/permission/ui/wear/ConfirmationViewHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index db3340f6..abaf3e48 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -189,7 +189,7 @@ public abstract class ConfirmationViewHandler implements // In order to fake the buttons peeking at the bottom, need to do set the // padding properly. if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { - mContent.setPadding(0, 0, 0, mButtonBarContainer.getHeight()); + mContent.setPadding(0, mContent.getPaddingTop(), 0, mButtonBarContainer.getHeight()); if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, " set mContent.PaddingBottom: " + mButtonBarContainer.getHeight()); } -- cgit v1.2.3 From 51f646b19dfbe663fcd2ea78d10fd2813c4d20f8 Mon Sep 17 00:00:00 2001 From: Danny Epstein Date: Tue, 20 Oct 2015 10:04:27 -0700 Subject: Reduce the top padding when message isn't at top. If either the page number or an icon is shown above the message, reduce the top padding on round screens. Bug: 24961379 Change-Id: Iec9193e3bbdd8ac607743cd0010f2331edbebc81 --- .../packageinstaller/permission/ui/wear/ConfirmationViewHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index abaf3e48..adc831d8 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -189,7 +189,8 @@ public abstract class ConfirmationViewHandler implements // In order to fake the buttons peeking at the bottom, need to do set the // padding properly. if (mContent.getPaddingBottom() != mButtonBarContainer.getHeight()) { - mContent.setPadding(0, mContent.getPaddingTop(), 0, mButtonBarContainer.getHeight()); + mContent.setPadding(mContent.getPaddingLeft(), mContent.getPaddingTop(), + mContent.getPaddingRight(), mButtonBarContainer.getHeight()); if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, " set mContent.PaddingBottom: " + mButtonBarContainer.getHeight()); } -- cgit v1.2.3 From 851c6ea6ee8c32d4c09c6b4f18b095e5da0541e5 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Tue, 20 Oct 2015 14:45:39 -0700 Subject: Fix Permissions Dialog animations Make handler run on UI thread to avoid race conditions. Update some logic to prevent animation from running when it shouldn't be. BUG: 24954323 Change-Id: Ia1635cbb3c9128080a8e4a9c3ce6ee935c4d957e --- .../ui/wear/ConfirmationViewHandler.java | 40 +++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index adc831d8..954d7e96 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -6,6 +6,7 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.Icon; import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.text.TextUtils; import android.util.Log; @@ -106,7 +107,7 @@ public abstract class ConfirmationViewHandler implements android.R.interpolator.fast_out_slow_in); mButtonBarFloatingHeight = mContext.getResources().getDimension( R.dimen.conf_diag_floating_height); - mHideHandler = new Handler(this); + mHideHandler = new Handler(Looper.getMainLooper(), this); mScrollingContainer.getViewTreeObserver().addOnScrollChangedListener(this); mRoot.getViewTreeObserver().addOnGlobalLayoutListener(this); @@ -237,6 +238,9 @@ public abstract class ConfirmationViewHandler implements @Override public void onScrollChanged () { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "onScrollChanged"); + } mHideHandler.removeMessages(MSG_HIDE_BUTTON_BAR); hideButtonBar(); } @@ -258,12 +262,13 @@ public abstract class ConfirmationViewHandler implements // Evaluate the max height the button bar can go final int screenHeight = mRoot.getHeight(); + final int halfScreenHeight = screenHeight / 2; final int buttonBarHeight = mButtonBarContainer.getHeight(); + final int contentHeight = mContent.getHeight() - buttonBarHeight; final int buttonBarMaxHeight = - Math.min(buttonBarHeight, screenHeight / 2); + Math.min(buttonBarHeight, halfScreenHeight); if (Log.isLoggable(TAG, Log.DEBUG)) { - final int contentHeight = mContent.getHeight() - buttonBarHeight; Log.d(TAG, " screenHeight: " + screenHeight); Log.d(TAG, " contentHeight: " + contentHeight); Log.d(TAG, " buttonBarHeight: " + buttonBarHeight); @@ -271,7 +276,13 @@ public abstract class ConfirmationViewHandler implements } mButtonBarContainer.setTranslationZ(mButtonBarFloatingHeight); - mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); + + // Only hide the button bar if it is occluding the content or the button bar is bigger than + // half the screen + if (contentHeight > halfScreenHeight + || buttonBarHeight > halfScreenHeight) { + mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); + } generateButtonBarAnimator(buttonBarHeight, buttonBarHeight - buttonBarMaxHeight, 0, mButtonBarFloatingHeight, 1000); @@ -295,7 +306,10 @@ public abstract class ConfirmationViewHandler implements mButtonBarContainer.getHeight() - offset : mButtonBarContainer.getHeight()); if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, " topMargin: " + topMargin); Log.d(TAG, " contentHeight: " + contentHeight); + Log.d(TAG, " screenHeight: " + screenHeight); + Log.d(TAG, " offset: " + offset); Log.d(TAG, " buttonBarHeight: " + buttonBarHeight); Log.d(TAG, " mContent.getPaddingBottom(): " + mContent.getPaddingBottom()); Log.d(TAG, " mScrollingContainer.getScrollY(): " + mScrollingContainer.getScrollY()); @@ -303,6 +317,14 @@ public abstract class ConfirmationViewHandler implements } if (!mHiddenBefore || mButtonBarAnimator == null) { + // Remove previous call to MSG_SHOW_BUTTON_BAR if the user scrolled or something before + // the animation got a chance to play + mHideHandler.removeMessages(MSG_SHOW_BUTTON_BAR); + + if(mButtonBarAnimator != null) { + mButtonBarAnimator.cancel(); // stop current animation if there is one playing + } + // hasn't hidden the bar yet, just hide now to the right height generateButtonBarAnimator( mButtonBarContainer.getTranslationY(), translationY, @@ -317,6 +339,7 @@ public abstract class ConfirmationViewHandler implements (float) HIDE_ANIM_DURATION * (translationY - mButtonBarContainer.getTranslationY()) / mButtonBarContainer.getHeight()), 0); + generateButtonBarAnimator( mButtonBarContainer.getTranslationY(), translationY, mButtonBarFloatingHeight, 0, duration); @@ -336,6 +359,15 @@ public abstract class ConfirmationViewHandler implements private void generateButtonBarAnimator( float startY, float endY, float startZ, float endZ, long duration) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "generateButtonBarAnimator"); + Log.d(TAG, " startY: " + startY); + Log.d(TAG, " endY: " + endY); + Log.d(TAG, " startZ: " + startZ); + Log.d(TAG, " endZ: " + endZ); + Log.d(TAG, " duration: " + duration); + } + mButtonBarAnimator = ObjectAnimator.ofPropertyValuesHolder( mButtonBarContainer, -- cgit v1.2.3 From e4cd4f8bb6035fd5463c5a44c267b0dd196c17fe Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Mon, 26 Oct 2015 17:50:00 -0700 Subject: Add mechanism for determining if apps are system apps This is being added to help identify system apps so that the UI can filter on that type. BUG: 24955055 Change-Id: I8d843bae2d81329009c8cda8c25355d08ab9d1d7 --- .../permission/model/PermissionStatusReceiver.java | 19 +++++++++++++++++-- .../packageinstaller/permission/utils/Utils.java | 10 +++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java b/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java index 3604cd71..810ae8ec 100644 --- a/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java +++ b/src/com/android/packageinstaller/permission/model/PermissionStatusReceiver.java @@ -69,11 +69,14 @@ public class PermissionStatusReceiver extends BroadcastReceiver { List appsList = new ArrayList<>(); List appLabelsList = new ArrayList<>(); - if (getAppsWithRuntimePermissions(context, appsList, appLabelsList)) { + List isSystemAppList = new ArrayList<>(); + if (getAppsWithRuntimePermissions(context, appsList, appLabelsList, isSystemAppList)) { responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_APP_LIST_RESULT, appsList.toArray(new String[appsList.size()])); responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_APP_LABEL_LIST_RESULT, appLabelsList.toArray(new String[appLabelsList.size()])); + responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_IS_SYSTEM_APP_LIST_RESULT, + toPrimitiveBoolArray(isSystemAppList)); } context.sendBroadcast(responseIntent); } @@ -122,13 +125,14 @@ public class PermissionStatusReceiver extends BroadcastReceiver { } public boolean getAppsWithRuntimePermissions(Context context, List appsList, - List appLabelsList) { + List appLabelsList, List isSystemAppList) { final List appInfos = Utils.getAllInstalledApplications(context); if (appInfos == null) { return false; } final int appInfosSize = appInfos.size(); try { + ArraySet launcherPackages = Utils.getLauncherPackages(context); for (int i = 0; i < appInfosSize; ++i) { final String packageName = appInfos.get(i).packageName; PackageInfo packageInfo = context.getPackageManager().getPackageInfo( @@ -146,6 +150,7 @@ public class PermissionStatusReceiver extends BroadcastReceiver { if (shouldShow) { appsList.add(packageName); appLabelsList.add(appPermissions.getAppLabel()); + isSystemAppList.add(Utils.isSystem(appPermissions, launcherPackages)); } } } catch (NameNotFoundException e) { @@ -180,4 +185,14 @@ public class PermissionStatusReceiver extends BroadcastReceiver { counts[1] = allApps.size(); return true; } + + private boolean[] toPrimitiveBoolArray(final List list) { + final int count = list.size(); + final boolean[] result = new boolean[count]; + for (int i = 0; i < count; ++i) { + result[i] = list.get(i); + } + + return result; + } } diff --git a/src/com/android/packageinstaller/permission/utils/Utils.java b/src/com/android/packageinstaller/permission/utils/Utils.java index 2cc5d8aa..21830378 100644 --- a/src/com/android/packageinstaller/permission/utils/Utils.java +++ b/src/com/android/packageinstaller/permission/utils/Utils.java @@ -31,6 +31,7 @@ import android.util.Log; import android.util.TypedValue; import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.AppPermissions; import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; import java.util.List; @@ -134,7 +135,14 @@ public class Utils { } public static boolean isSystem(PermissionApp app, ArraySet launcherPkgs) { - ApplicationInfo info = app.getAppInfo(); + return isSystem(app.getAppInfo(), launcherPkgs); + } + + public static boolean isSystem(AppPermissions app, ArraySet launcherPkgs) { + return isSystem(app.getPackageInfo().applicationInfo, launcherPkgs); + } + + public static boolean isSystem(ApplicationInfo info, ArraySet launcherPkgs) { return info.isSystemApp() && (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 && !launcherPkgs.contains(info.packageName); } -- cgit v1.2.3 From e18bfc22019f44ae60674fc6d34616ce097e05f4 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Wed, 28 Oct 2015 16:46:15 -0700 Subject: Fix out of bounds exception It looks like the cached index had become invalid at some point. It's unclear why, but the logs suggest that a bunch of Home services crashed. Rather than cache the index within the Activity, I have changed the code to send the index along with the dialog activity launch. This way when the dialog returns, the index will be saved within the callback. The index should be enough because the permission set should not change for the app between screens and the permission set is alphabetized. BUG: 25334674 Change-Id: I8f9189960aa7fc9b09cc25f594397523550ac626 --- .../permission/ui/wear/AppPermissionsFragmentWear.java | 16 +++++++++------- .../permission/ui/wear/WarningConfirmationActivity.java | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java index 18db94c1..aba97fc8 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java +++ b/src/com/android/packageinstaller/permission/ui/wear/AppPermissionsFragmentWear.java @@ -55,7 +55,6 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { private PermissionsSettingsAdapter mAdapter; private boolean mHasConfirmedRevoke; - private int mPendingPermGroupIndex = -1; public static AppPermissionsFragmentWear newInstance(String packageName) { return setPackageName(new AppPermissionsFragmentWear(), packageName); @@ -216,11 +215,11 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { } else { final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { - mPendingPermGroupIndex = index; Intent intent = new Intent(getActivity(), WarningConfirmationActivity.class); intent.putExtra(WarningConfirmationActivity.EXTRA_WARNING_MESSAGE, getString(grantedByDefault ? R.string.system_warning : R.string.old_sdk_deny_warning)); + intent.putExtra(WarningConfirmationActivity.EXTRA_INDEX, index); startActivityForResult(intent, WARNING_CONFIRMATION_REQUEST); } else { group.revokeRuntimePermissions(false); @@ -234,18 +233,21 @@ public final class AppPermissionsFragmentWear extends TitledSettingsFragment { public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == WARNING_CONFIRMATION_REQUEST) { if (resultCode == Activity.RESULT_OK) { - SettingsAdapter.Setting setting = - mAdapter.get(mPendingPermGroupIndex); + int index = data.getIntExtra(WarningConfirmationActivity.EXTRA_INDEX, -1); + if (index == -1) { + Log.e(LOG_TAG, "Warning confirmation request came back with no index."); + return; + } + + SettingsAdapter.Setting setting = mAdapter.get(index); final AppPermissionGroup group = setting.data; group.revokeRuntimePermissions(false); if (!group.hasGrantedByDefaultPermission()) { mHasConfirmedRevoke = true; } - updatePermissionGroupSetting(mPendingPermGroupIndex); + updatePermissionGroupSetting(index); } - - mPendingPermGroupIndex = -1; } else { super.onActivityResult(requestCode, resultCode, data); } diff --git a/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java b/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java index e26dabd6..03713419 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java +++ b/src/com/android/packageinstaller/permission/ui/wear/WarningConfirmationActivity.java @@ -17,6 +17,7 @@ package com.android.packageinstaller.permission.ui.wear; import android.app.Activity; +import android.content.Intent; import android.graphics.drawable.Drawable; import android.graphics.drawable.Icon; import android.os.Bundle; @@ -25,6 +26,8 @@ import com.android.packageinstaller.R; public final class WarningConfirmationActivity extends Activity { public final static String EXTRA_WARNING_MESSAGE = "EXTRA_WARNING_MESSAGE"; + // Saved index that will be returned in the onActivityResult() callback + public final static String EXTRA_INDEX = "EXTRA_INDEX"; private ConfirmationViewHandler mViewHandler; private String mMessage; @@ -43,14 +46,12 @@ public final class WarningConfirmationActivity extends Activity { @Override public void onButton1() { - setResult(Activity.RESULT_CANCELED); - finish(); + setResultAndFinish(Activity.RESULT_CANCELED); } @Override public void onButton2() { - setResult(Activity.RESULT_OK); - finish(); + setResultAndFinish(Activity.RESULT_OK); } @Override @@ -107,4 +108,11 @@ public final class WarningConfirmationActivity extends Activity { setContentView(mViewHandler.createView()); mViewHandler.invalidate(); } + + private void setResultAndFinish(int result) { + Intent intent = new Intent(); + intent.putExtra(EXTRA_INDEX, getIntent().getIntExtra(EXTRA_INDEX, -1)); + setResult(result, intent); + finish(); + } } -- cgit v1.2.3 From 914243ab7dd019daa1ae8fcbb538142555597acd Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Mon, 2 Nov 2015 13:16:08 -0800 Subject: Button bar animating when it isn't supposed to There was an incorrect calculation for determining when the content was occluded by the button bar or not. BUG: 25164689 Change-Id: I121f6e0232da2d1536e0fbbf697e00a4f9c1841f --- .../packageinstaller/permission/ui/wear/ConfirmationViewHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java index 954d7e96..1c55e1bd 100644 --- a/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/wear/ConfirmationViewHandler.java @@ -279,7 +279,7 @@ public abstract class ConfirmationViewHandler implements // Only hide the button bar if it is occluding the content or the button bar is bigger than // half the screen - if (contentHeight > halfScreenHeight + if (contentHeight > (screenHeight - buttonBarHeight) || buttonBarHeight > halfScreenHeight) { mHideHandler.sendEmptyMessageDelayed(MSG_HIDE_BUTTON_BAR, 3000); } -- cgit v1.2.3 From 012a557f593bd2862756839eeade4b892b05422e Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Sat, 7 Nov 2015 09:33:17 -0800 Subject: resolve merge conflicts of e8c6801367 to cw-e-dev. Change-Id: I9c5a0be4e18cc27e163da60ba9dd81ae71588bef --- .../android/packageinstaller/wear/WearPackageInstallerService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java index ba83ea28..d57236e1 100644 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -238,7 +238,7 @@ public class WearPackageInstallerService extends Service { PackageInfo existingPkgInfo = null; try { existingPkgInfo = pm.getPackageInfo(packageName, - PackageManager.GET_UNINSTALLED_PACKAGES); + PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_PERMISSIONS); if(existingPkgInfo != null) { installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; } @@ -298,6 +298,10 @@ public class WearPackageInstallerService extends Service { // If the permission is granted, then we will not ask to request it again. if ((existingPkgInfo.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, existingPkgInfo.requestedPermissions[i] + + " is already granted for " + packageName); + } wearablePerms.remove(existingPkgInfo.requestedPermissions[i]); } } -- cgit v1.2.3 From 9c78316fe6baa4f7fd7d5108349ecf8a2532b047 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Wed, 11 Nov 2015 15:06:17 -0800 Subject: Remove app from "needs permission" state if installation failed This adds code to remove an app that is in the "needs permission" state from the ShowPermStore if the app fails to install. b/25721625 tracks creating a new intent to handle this case for F. BUG: 25363020 Change-Id: Ifed00024d7e329fb3185a4607a347e972f697fcd --- .../wear/WearPackageInstallerService.java | 19 +++++++++++++------ .../packageinstaller/wear/WearPackageUtil.java | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java index d57236e1..6dc5aa70 100644 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -337,7 +337,8 @@ public class WearPackageInstallerService extends Service { // Finally install the package. pm.installPackage(Uri.fromFile(tempFile), - new PackageInstallObserver(this, lock, startId), installFlags, packageName); + new PackageInstallObserver(this, lock, startId, packageName), + installFlags, packageName); messageSent = true; Log.i(TAG, "Sent installation request for " + packageName); } catch (FileNotFoundException e) { @@ -575,20 +576,26 @@ public class WearPackageInstallerService extends Service { private Context mContext; private PowerManager.WakeLock mWakeLock; private int mStartId; + private String mApplicationPackageName; private PackageInstallObserver(Context context, PowerManager.WakeLock wakeLock, - int startId) { + int startId, String applicationPackageName) { mContext = context; mWakeLock = wakeLock; mStartId = startId; + mApplicationPackageName = applicationPackageName; } public void packageInstalled(String packageName, int returnCode) { - if (returnCode >= 0) { - Log.i(TAG, "Package " + packageName + " was installed."); - } else { - Log.e(TAG, "Package install failed " + packageName + ", returnCode " + returnCode); + // If installation failed, bail out and remove the ShowPermsStore entry + if (returnCode < 0) { + Log.e(TAG, "Package install failed " + mApplicationPackageName + + ", returnCode " + returnCode); + WearPackageUtil.removeFromPermStore(mContext, mApplicationPackageName); + return; } + Log.i(TAG, "Package " + packageName + " was installed."); + // Delete tempFile from the file system. File tempFile = WearPackageUtil.getTemporaryFile(mContext, packageName); if (tempFile != null) { diff --git a/src/com/android/packageinstaller/wear/WearPackageUtil.java b/src/com/android/packageinstaller/wear/WearPackageUtil.java index dc420757..688d6167 100644 --- a/src/com/android/packageinstaller/wear/WearPackageUtil.java +++ b/src/com/android/packageinstaller/wear/WearPackageUtil.java @@ -17,9 +17,9 @@ package com.android.packageinstaller.wear; import android.annotation.Nullable; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; import android.content.pm.PackageParser; import android.os.ParcelFileDescriptor; import android.system.ErrnoException; @@ -42,6 +42,12 @@ public class WearPackageUtil { private static final String COMPRESSION_LZMA = "lzma"; private static final String COMPRESSION_XZ = "xz"; + private static final String SHOW_PERMS_SERVICE_PKG_NAME = "com.google.android.wearable.app"; + private static final String SHOW_PERMS_SERVICE_CLASS_NAME = + "com.google.android.clockwork.packagemanager.ShowPermsService"; + private static final String EXTRA_PACKAGE_NAME + = "com.google.android.clockwork.EXTRA_PACKAGE_NAME"; + public static File getTemporaryFile(Context context, String packageName) { try { File newFileDir = new File(context.getFilesDir(), "tmp"); @@ -147,4 +153,15 @@ public class WearPackageUtil { } return false; } + + public static void removeFromPermStore(Context context, String wearablePackageName) { + Intent newIntent = new Intent() + .setComponent(new ComponentName( + SHOW_PERMS_SERVICE_PKG_NAME, SHOW_PERMS_SERVICE_CLASS_NAME)) + .setAction(Intent.ACTION_UNINSTALL_PACKAGE); + newIntent.putExtra(EXTRA_PACKAGE_NAME, wearablePackageName); + Log.i(TAG, "Sending removeFromPermStore to ShowPermsService " + newIntent + + " for " + wearablePackageName); + context.startService(newIntent); + } } -- cgit v1.2.3 From 2cf17ddcef3c8dd260bd3d174123842c81a7d025 Mon Sep 17 00:00:00 2001 From: Svetoslav Date: Thu, 29 Oct 2015 13:14:27 -0700 Subject: Clean up package installer styling Package installer should be using exclusively the device defaut themes on handhelds (phones and tables) to allow OEMs customize its look and feel. Also for handhelds the installer should use the same preference framework as the settings app to ensure it looks like the settings app on an OEMs device. This change needs to be picked up by a GMS build to deliver to partners ensuring the installer UI is consistent with the device UI. bug:24286616 Change-Id: I92e39fd1488e76b0b23b7f1efa13e04ed5bbc7ba --- .../permission/model/PermissionApps.java | 3 +- .../permission/ui/AllAppPermissionsFragment.java | 217 ---------- .../permission/ui/AppPermissionsFragment.java | 403 ------------------ .../permission/ui/ButtonBarLayout.java | 117 ++++++ .../permission/ui/GrantPermissionsActivity.java | 12 +- .../ui/GrantPermissionsDefaultViewHandler.java | 462 --------------------- .../ui/GrantPermissionsTvViewHandler.java | 130 ------ .../permission/ui/GrantPermissionsViewHandler.java | 2 +- .../permission/ui/ManagePermissionsActivity.java | 25 +- .../permission/ui/ManagePermissionsFragment.java | 267 ------------ .../permission/ui/OverlayWarningDialog.java | 1 + .../permission/ui/PermissionAppsFragment.java | 433 ------------------- .../permission/ui/PermissionsFrameFragment.java | 187 --------- .../permission/ui/PreferenceImageView.java | 69 +++ .../permission/ui/SettingsWithHeader.java | 85 ---- .../ui/handheld/AllAppPermissionsFragment.java | 214 ++++++++++ .../ui/handheld/AppPermissionsFragment.java | 404 ++++++++++++++++++ .../handheld/GrantPermissionsViewHandlerImpl.java | 462 +++++++++++++++++++++ .../ui/handheld/ManagePermissionsFragment.java | 268 ++++++++++++ .../ui/handheld/PermissionAppsFragment.java | 427 +++++++++++++++++++ .../ui/handheld/PermissionsFrameFragment.java | 121 ++++++ .../permission/ui/handheld/SettingsWithHeader.java | 84 ++++ .../ui/television/AllAppPermissionsFragment.java | 217 ++++++++++ .../ui/television/AppPermissionsFragment.java | 404 ++++++++++++++++++ .../GrantPermissionsViewHandlerImpl.java | 131 ++++++ .../ui/television/ManagePermissionsFragment.java | 267 ++++++++++++ .../ui/television/PermissionAppsFragment.java | 434 +++++++++++++++++++ .../ui/television/PermissionsFrameFragment.java | 203 +++++++++ .../ui/television/SettingsWithHeader.java | 85 ++++ .../permission/utils/LocationUtils.java | 17 +- 30 files changed, 3941 insertions(+), 2210 deletions(-) delete mode 100644 src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java delete mode 100644 src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java delete mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java delete mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java delete mode 100644 src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java delete mode 100644 src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java delete mode 100644 src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/PreferenceImageView.java delete mode 100644 src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/model/PermissionApps.java b/src/com/android/packageinstaller/permission/model/PermissionApps.java index 9365bf13..e5d96d55 100644 --- a/src/com/android/packageinstaller/permission/model/PermissionApps.java +++ b/src/com/android/packageinstaller/permission/model/PermissionApps.java @@ -31,6 +31,7 @@ import android.util.ArraySet; import android.util.Log; import android.util.SparseArray; +import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; import java.util.ArrayList; @@ -275,7 +276,7 @@ public class PermissionApps { if (info.icon != 0) { mIcon = info.loadUnbadgedIcon(mPm); } else { - mIcon = mContext.getDrawable(com.android.internal.R.drawable.ic_perm_device_info); + mIcon = mContext.getDrawable(R.drawable.ic_perm_device_info); } mIcon = Utils.applyTint(mContext, mIcon, android.R.attr.colorControlNormal); } diff --git a/src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java deleted file mode 100644 index 2fb9a510..00000000 --- a/src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java +++ /dev/null @@ -1,217 +0,0 @@ -/* -* Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.app.ActionBar; -import android.app.AlertDialog; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageItemInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.content.pm.PermissionGroupInfo; -import android.content.pm.PermissionInfo; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Bundle; -import android.provider.Settings; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceCategory; -import android.support.v7.preference.PreferenceGroup; -import android.util.Log; -import android.view.MenuItem; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; - -public final class AllAppPermissionsFragment extends SettingsWithHeader { - - private static final String LOG_TAG = "AllAppPermissionsFragment"; - - private static final String KEY_OTHER = "other_perms"; - - public static AllAppPermissionsFragment newInstance(String packageName) { - AllAppPermissionsFragment instance = new AllAppPermissionsFragment(); - Bundle arguments = new Bundle(); - arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); - instance.setArguments(arguments); - return instance; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setTitle(R.string.all_permissions); - ab.setDisplayHomeAsUpEnabled(true); - } - } - - @Override - public void onResume() { - super.onResume(); - updateUi(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: { - getFragmentManager().popBackStack(); - return true; - } - } - return super.onOptionsItemSelected(item); - } - - private void updateUi() { - if (getPreferenceScreen() != null) { - getPreferenceScreen().removeAll(); - } - addPreferencesFromResource(R.xml.all_permissions); - PreferenceGroup otherGroup = (PreferenceGroup) findPreference(KEY_OTHER); - ArrayList prefs = new ArrayList<>(); // Used for sorting. - prefs.add(otherGroup); - String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); - otherGroup.removeAll(); - PackageManager pm = getContext().getPackageManager(); - - try { - PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS); - - ApplicationInfo appInfo = info.applicationInfo; - final Drawable icon = appInfo.loadIcon(pm); - final CharSequence label = appInfo.loadLabel(pm); - Intent infoIntent = null; - if (!getActivity().getIntent().getBooleanExtra( - AppPermissionsFragment.EXTRA_HIDE_INFO_BUTTON, false)) { - infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - .setData(Uri.fromParts("package", pkg, null)); - } - setHeader(icon, label, infoIntent); - - if (info.requestedPermissions != null) { - for (int i = 0; i < info.requestedPermissions.length; i++) { - PermissionInfo perm; - try { - perm = pm.getPermissionInfo(info.requestedPermissions[i], 0); - } catch (NameNotFoundException e) { - Log.e(LOG_TAG, - "Can't get permission info for " + info.requestedPermissions[i], e); - continue; - } - - if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0 - || (perm.flags & PermissionInfo.FLAG_HIDDEN) != 0) { - continue; - } - - if (perm.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { - PermissionGroupInfo group = getGroup(perm.group, pm); - PreferenceGroup pref = - findOrCreate(group != null ? group : perm, pm, prefs); - pref.addPreference(getPreference(perm, group, pm)); - } else if (perm.protectionLevel == PermissionInfo.PROTECTION_NORMAL) { - PermissionGroupInfo group = getGroup(perm.group, pm); - otherGroup.addPreference(getPreference(perm, group, pm)); - } - } - } - } catch (NameNotFoundException e) { - Log.e(LOG_TAG, "Problem getting package info for " + pkg, e); - } - // Sort an ArrayList of the groups and then set the order from the sorting. - Collections.sort(prefs, new Comparator() { - @Override - public int compare(Preference lhs, Preference rhs) { - String lKey = lhs.getKey(); - String rKey = rhs.getKey(); - if (lKey.equals(KEY_OTHER)) { - return 1; - } else if (rKey.equals(KEY_OTHER)) { - return -1; - } else if (Utils.isModernPermissionGroup(lKey) - != Utils.isModernPermissionGroup(rKey)) { - return Utils.isModernPermissionGroup(lKey) ? -1 : 1; - } - return lhs.getTitle().toString().compareTo(rhs.getTitle().toString()); - } - }); - for (int i = 0; i < prefs.size(); i++) { - prefs.get(i).setOrder(i); - } - } - - private PermissionGroupInfo getGroup(String group, PackageManager pm) { - try { - return pm.getPermissionGroupInfo(group, 0); - } catch (NameNotFoundException e) { - return null; - } - } - - private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm, - ArrayList prefs) { - PreferenceGroup pref = (PreferenceGroup) findPreference(group.name); - if (pref == null) { - pref = new PreferenceCategory(getContext()); - pref.setKey(group.name); - pref.setLayoutResource(R.layout.preference_category_material); - pref.setTitle(group.loadLabel(pm)); - prefs.add(pref); - getPreferenceScreen().addPreference(pref); - } - return pref; - } - - private Preference getPreference(PermissionInfo perm, PermissionGroupInfo group, - PackageManager pm) { - Preference pref = new Preference(getContext()); - pref.setLayoutResource(R.layout.preference_permissions); - Drawable icon = null; - if (perm.icon != 0) { - icon = perm.loadIcon(pm); - } else if (group != null && group.icon != 0) { - icon = group.loadIcon(pm); - } else { - icon = getContext().getDrawable(R.drawable.ic_perm_device_info); - } - pref.setIcon(Utils.applyTint(getContext(), icon, android.R.attr.colorControlNormal)); - pref.setTitle(perm.loadLabel(pm)); - final CharSequence desc = perm.loadDescription(pm); - pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - new AlertDialog.Builder(getContext()) - .setMessage(desc) - .setPositiveButton(android.R.string.ok, null) - .show(); - return true; - } - }); - - return pref; - } -} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java deleted file mode 100644 index 6396c61e..00000000 --- a/src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java +++ /dev/null @@ -1,403 +0,0 @@ -/* -* Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.annotation.Nullable; -import android.app.ActionBar; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Bundle; -import android.provider.Settings; -import android.support.v14.preference.SwitchPreference; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceChangeListener; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceScreen; -import android.util.Log; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.Toast; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.model.AppPermissionGroup; -import com.android.packageinstaller.permission.model.AppPermissions; -import com.android.packageinstaller.permission.utils.LocationUtils; -import com.android.packageinstaller.permission.utils.SafetyNetLogger; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.ArrayList; -import java.util.List; - -public final class AppPermissionsFragment extends SettingsWithHeader - implements OnPreferenceChangeListener { - - private static final String LOG_TAG = "ManagePermsFragment"; - - static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"; - - private static final int MENU_ALL_PERMS = 0; - - private List mToggledGroups; - private AppPermissions mAppPermissions; - private PreferenceScreen mExtraScreen; - - private boolean mHasConfirmedRevoke; - - public static AppPermissionsFragment newInstance(String packageName) { - return setPackageName(new AppPermissionsFragment(), packageName); - } - - private static T setPackageName(T fragment, String packageName) { - Bundle arguments = new Bundle(); - arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); - fragment.setArguments(arguments); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setLoading(true /* loading */, false /* animate */); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - } - - String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); - Activity activity = getActivity(); - PackageInfo packageInfo = getPackageInfo(activity, packageName); - if (packageInfo == null) { - Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); - activity.finish(); - return; - } - - mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { - @Override - public void run() { - getActivity().finish(); - } - }); - loadPreferences(); - } - - @Override - public void onResume() { - super.onResume(); - mAppPermissions.refresh(); - setPreferencesCheckedState(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: { - getActivity().finish(); - return true; - } - - case MENU_ALL_PERMS: { - Fragment frag = AllAppPermissionsFragment.newInstance( - getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, frag) - .addToBackStack("AllPerms") - .commit(); - return true; - } - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - if (mAppPermissions != null) { - bindUi(this, mAppPermissions.getPackageInfo()); - } - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - super.onCreateOptionsMenu(menu, inflater); - menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions); - } - - private static void bindUi(SettingsWithHeader fragment, PackageInfo packageInfo) { - Activity activity = fragment.getActivity(); - PackageManager pm = activity.getPackageManager(); - ApplicationInfo appInfo = packageInfo.applicationInfo; - Intent infoIntent = null; - if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) { - infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - .setData(Uri.fromParts("package", packageInfo.packageName, null)); - } - - Drawable icon = appInfo.loadIcon(pm); - CharSequence label = appInfo.loadLabel(pm); - fragment.setHeader(icon, label, infoIntent); - - ActionBar ab = activity.getActionBar(); - if (ab != null) { - ab.setTitle(R.string.app_permissions); - } - - ViewGroup rootView = (ViewGroup) fragment.getView(); - ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); - if (iconView != null) { - iconView.setImageDrawable(icon); - } - TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); - if (titleView != null) { - titleView.setText(R.string.app_permissions); - } - TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); - if (breadcrumbView != null) { - breadcrumbView.setText(label); - } - } - - private void loadPreferences() { - Context context = getPreferenceManager().getContext(); - if (context == null) { - return; - } - - PreferenceScreen screen = getPreferenceScreen(); - screen.removeAll(); - - if (mExtraScreen != null) { - mExtraScreen.removeAll(); - } - - final Preference extraPerms = new Preference(context); - extraPerms.setIcon(R.drawable.ic_toc); - extraPerms.setTitle(R.string.additional_permissions); - - for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { - if (!Utils.shouldShowPermission(group, mAppPermissions.getPackageInfo().packageName)) { - continue; - } - - boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); - - SwitchPreference preference = new SwitchPreference(context); - preference.setOnPreferenceChangeListener(this); - preference.setKey(group.getName()); - Drawable icon = Utils.loadDrawable(context.getPackageManager(), - group.getIconPkg(), group.getIconResId()); - preference.setIcon(Utils.applyTint(getContext(), icon, - android.R.attr.colorControlNormal)); - preference.setTitle(group.getLabel()); - if (group.isPolicyFixed()) { - preference.setSummary(getString(R.string.permission_summary_enforced_by_policy)); - } - preference.setPersistent(false); - preference.setEnabled(!group.isPolicyFixed()); - preference.setChecked(group.areRuntimePermissionsGranted()); - - if (isPlatform) { - screen.addPreference(preference); - } else { - if (mExtraScreen == null) { - mExtraScreen = getPreferenceManager().createPreferenceScreen(context); - } - mExtraScreen.addPreference(preference); - } - } - - if (mExtraScreen != null) { - extraPerms.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); - setPackageName(frag, getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); - frag.setTargetFragment(AppPermissionsFragment.this, 0); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, frag) - .addToBackStack(null) - .commit(); - return true; - } - }); - int count = mExtraScreen.getPreferenceCount(); - extraPerms.setSummary(getResources().getQuantityString( - R.plurals.additional_permissions_more, count, count)); - screen.addPreference(extraPerms); - } - - setLoading(false /* loading */, true /* animate */); - } - - @Override - public boolean onPreferenceChange(final Preference preference, Object newValue) { - String groupName = preference.getKey(); - final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName); - - if (group == null) { - return false; - } - - OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); - if (activity.isObscuredTouch()) { - activity.showOverlayDialog(); - return false; - } - - addToggledGroup(group); - - if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { - LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); - return false; - } - if (newValue == Boolean.TRUE) { - group.grantRuntimePermissions(false); - } else { - final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); - if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { - new AlertDialog.Builder(getContext()) - .setMessage(grantedByDefault ? R.string.system_warning - : R.string.old_sdk_deny_warning) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.grant_dialog_button_deny, - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - ((SwitchPreference) preference).setChecked(false); - group.revokeRuntimePermissions(false); - if (!grantedByDefault) { - mHasConfirmedRevoke = true; - } - } - }) - .show(); - return false; - } else { - group.revokeRuntimePermissions(false); - } - } - - return true; - } - - @Override - public void onPause() { - super.onPause(); - logToggledGroups(); - } - - private void addToggledGroup(AppPermissionGroup group) { - if (mToggledGroups == null) { - mToggledGroups = new ArrayList<>(); - } - // Double toggle is back to initial state. - if (mToggledGroups.contains(group)) { - mToggledGroups.remove(group); - } else { - mToggledGroups.add(group); - } - } - - private void logToggledGroups() { - if (mToggledGroups != null) { - String packageName = mAppPermissions.getPackageInfo().packageName; - SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); - mToggledGroups = null; - } - } - - private void setPreferencesCheckedState() { - setPreferencesCheckedState(getPreferenceScreen()); - if (mExtraScreen != null) { - setPreferencesCheckedState(mExtraScreen); - } - } - - private void setPreferencesCheckedState(PreferenceScreen screen) { - int preferenceCount = screen.getPreferenceCount(); - for (int i = 0; i < preferenceCount; i++) { - Preference preference = screen.getPreference(i); - if (preference instanceof SwitchPreference) { - SwitchPreference switchPref = (SwitchPreference) preference; - AppPermissionGroup group = mAppPermissions.getPermissionGroup(switchPref.getKey()); - if (group != null) { - switchPref.setChecked(group.areRuntimePermissionsGranted()); - } - } - } - } - - private static PackageInfo getPackageInfo(Activity activity, String packageName) { - try { - return activity.getPackageManager().getPackageInfo( - packageName, PackageManager.GET_PERMISSIONS); - } catch (PackageManager.NameNotFoundException e) { - Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); - return null; - } - } - - public static class AdditionalPermissionsFragment extends SettingsWithHeader { - AppPermissionsFragment mOuterFragment; - - @Override - public void onCreate(Bundle savedInstanceState) { - mOuterFragment = (AppPermissionsFragment) getTargetFragment(); - super.onCreate(savedInstanceState); - setHeader(mOuterFragment.mIcon, mOuterFragment.mLabel, mOuterFragment.mInfoIntent); - setHasOptionsMenu(true); - } - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferenceScreen(mOuterFragment.mExtraScreen); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); - bindUi(this, getPackageInfo(getActivity(), packageName)); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getFragmentManager().popBackStack(); - return true; - } - return super.onOptionsItemSelected(item); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java b/src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java new file mode 100644 index 00000000..59e54707 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.View; +import android.widget.LinearLayout; +import com.android.packageinstaller.R; + +/** + * An extension of LinearLayout that automatically switches to vertical + * orientation when it can't fit its child views horizontally. + */ +public class ButtonBarLayout extends LinearLayout { + /** Whether the current configuration allows stacking. */ + private boolean mAllowStacking; + + private int mLastWidthSize = -1; + + public ButtonBarLayout(Context context, AttributeSet attrs) { + super(context, attrs); + mAllowStacking = true; + } + + public void setAllowStacking(boolean allowStacking) { + if (mAllowStacking != allowStacking) { + mAllowStacking = allowStacking; + if (!mAllowStacking && getOrientation() == LinearLayout.VERTICAL) { + setStacked(false); + } + requestLayout(); + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + final int widthSize = MeasureSpec.getSize(widthMeasureSpec); + + if (mAllowStacking) { + if (widthSize > mLastWidthSize && isStacked()) { + // We're being measured wider this time, try un-stacking. + setStacked(false); + } + + mLastWidthSize = widthSize; + } + + boolean needsRemeasure = false; + + // If we're not stacked, make sure the measure spec is AT_MOST rather + // than EXACTLY. This ensures that we'll still get TOO_SMALL so that we + // know to stack the buttons. + final int initialWidthMeasureSpec; + if (!isStacked() && MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) { + initialWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.AT_MOST); + + // We'll need to remeasure again to fill excess space. + needsRemeasure = true; + } else { + initialWidthMeasureSpec = widthMeasureSpec; + } + + super.onMeasure(initialWidthMeasureSpec, heightMeasureSpec); + + if (mAllowStacking && !isStacked()) { + final int measuredWidth = getMeasuredWidthAndState(); + final int measuredWidthState = measuredWidth & MEASURED_STATE_MASK; + if (measuredWidthState == MEASURED_STATE_TOO_SMALL) { + setStacked(true); + + // Measure again in the new orientation. + needsRemeasure = true; + } + } + + if (needsRemeasure) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + + private void setStacked(boolean stacked) { + setOrientation(stacked ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL); + setGravity(stacked ? Gravity.RIGHT : Gravity.BOTTOM); + + final View spacer = findViewById(R.id.spacer); + if (spacer != null) { + spacer.setVisibility(stacked ? View.GONE : View.INVISIBLE); + } + + // Reverse the child order. This is specific to the Material button + // bar's layout XML and will probably not generalize. + final int childCount = getChildCount(); + for (int i = childCount - 2; i >= 0; i--) { + bringChildToFront(getChildAt(i)); + } + } + + private boolean isStacked() { + return getOrientation() == LinearLayout.VERTICAL; + } +} diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java index aaa65f9e..44bbcc9c 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java @@ -26,11 +26,12 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PermissionInfo; import android.content.res.Resources; +import android.graphics.Typeface; import android.graphics.drawable.Icon; import android.hardware.camera2.utils.ArrayUtils; import android.os.Bundle; import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; +import android.text.style.StyleSpan; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; @@ -72,9 +73,11 @@ public class GrantPermissionsActivity extends OverlayTouchActivity setTitle(R.string.permission_request_title); if (Utils.isTelevision(this)) { - mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this); + mViewHandler = new com.android.packageinstaller.permission.ui.television + .GrantPermissionsViewHandlerImpl(this).setResultListener(this); } else { - mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this); + mViewHandler = new com.android.packageinstaller.permission.ui.handheld + .GrantPermissionsViewHandlerImpl(this).setResultListener(this); } mRequestedPermissions = getIntent().getStringArrayExtra( @@ -190,8 +193,7 @@ public class GrantPermissionsActivity extends OverlayTouchActivity // Color the app name. int appLabelStart = message.toString().indexOf(appLabel.toString(), 0); int appLabelLength = appLabel.length(); - int color = getColor(R.color.grant_permissions_app_color); - message.setSpan(new ForegroundColorSpan(color), appLabelStart, + message.setSpan(new StyleSpan(Typeface.BOLD), appLabelStart, appLabelStart + appLabelLength, 0); // Set the new grant view diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java deleted file mode 100644 index c5d78784..00000000 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java +++ /dev/null @@ -1,462 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; -import android.animation.ObjectAnimator; -import android.content.Context; -import android.content.res.Resources; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.util.SparseArray; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.View.OnLayoutChangeListener; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.view.ViewRootImpl; -import android.view.WindowManager.LayoutParams; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; -import android.view.animation.Interpolator; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.internal.widget.ButtonBarLayout; -import com.android.packageinstaller.R; - -import java.util.ArrayList; - -final class GrantPermissionsDefaultViewHandler - implements GrantPermissionsViewHandler, OnClickListener { - - public static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; - public static final String ARG_GROUP_COUNT = "ARG_GROUP_COUNT"; - public static final String ARG_GROUP_INDEX = "ARG_GROUP_INDEX"; - public static final String ARG_GROUP_ICON = "ARG_GROUP_ICON"; - public static final String ARG_GROUP_MESSAGE = "ARG_GROUP_MESSAGE"; - public static final String ARG_GROUP_SHOW_DO_NOT_ASK = "ARG_GROUP_SHOW_DO_NOT_ASK"; - public static final String ARG_GROUP_DO_NOT_ASK_CHECKED = "ARG_GROUP_DO_NOT_ASK_CHECKED"; - - // Animation parameters. - private static final long SIZE_START_DELAY = 300; - private static final long SIZE_START_LENGTH = 233; - private static final long FADE_OUT_START_DELAY = 300; - private static final long FADE_OUT_START_LENGTH = 217; - private static final long TRANSLATE_START_DELAY = 367; - private static final long TRANSLATE_LENGTH = 317; - private static final long GROUP_UPDATE_DELAY = 400; - private static final long DO_NOT_ASK_CHECK_DELAY = 450; - - private final Context mContext; - - private ResultListener mResultListener; - - private String mGroupName; - private int mGroupCount; - private int mGroupIndex; - private Icon mGroupIcon; - private CharSequence mGroupMessage; - private boolean mShowDonNotAsk; - private boolean mDoNotAskChecked; - - private ImageView mIconView; - private TextView mCurrentGroupView; - private TextView mMessageView; - private CheckBox mDoNotAskCheckbox; - private Button mAllowButton; - - private ArrayList mHeightControllers; - private ManualLayoutFrame mRootView; - - // Needed for animation - private ViewGroup mDescContainer; - private ViewGroup mCurrentDesc; - private ViewGroup mNextDesc; - - private ViewGroup mDialogContainer; - - private final Runnable mUpdateGroup = new Runnable() { - @Override - public void run() { - updateGroup(); - } - }; - - GrantPermissionsDefaultViewHandler(Context context) { - mContext = context; - } - - @Override - public GrantPermissionsDefaultViewHandler setResultListener(ResultListener listener) { - mResultListener = listener; - return this; - } - - @Override - public void saveInstanceState(Bundle arguments) { - arguments.putString(ARG_GROUP_NAME, mGroupName); - arguments.putInt(ARG_GROUP_COUNT, mGroupCount); - arguments.putInt(ARG_GROUP_INDEX, mGroupIndex); - arguments.putParcelable(ARG_GROUP_ICON, mGroupIcon); - arguments.putCharSequence(ARG_GROUP_MESSAGE, mGroupMessage); - arguments.putBoolean(ARG_GROUP_SHOW_DO_NOT_ASK, mShowDonNotAsk); - arguments.putBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED, mDoNotAskCheckbox.isChecked()); - } - - @Override - public void loadInstanceState(Bundle savedInstanceState) { - mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); - mGroupMessage = savedInstanceState.getCharSequence(ARG_GROUP_MESSAGE); - mGroupIcon = savedInstanceState.getParcelable(ARG_GROUP_ICON); - mGroupCount = savedInstanceState.getInt(ARG_GROUP_COUNT); - mGroupIndex = savedInstanceState.getInt(ARG_GROUP_INDEX); - mShowDonNotAsk = savedInstanceState.getBoolean(ARG_GROUP_SHOW_DO_NOT_ASK); - mDoNotAskChecked = savedInstanceState.getBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED); - } - - @Override - public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, - CharSequence message, boolean showDonNotAsk) { - mGroupName = groupName; - mGroupCount = groupCount; - mGroupIndex = groupIndex; - mGroupIcon = icon; - mGroupMessage = message; - mShowDonNotAsk = showDonNotAsk; - mDoNotAskChecked = false; - // If this is a second (or later) permission and the views exist, then animate. - if (mIconView != null) { - if (mGroupIndex > 0) { - // The first message will be announced as the title of the activity, all others - // we need to announce ourselves. - mDescContainer.announceForAccessibility(message); - animateToPermission(); - } else { - updateDescription(); - updateGroup(); - updateDoNotAskCheckBox(); - } - } - } - - private void animateToPermission() { - if (mHeightControllers == null) { - // We need to manually control the height of any views heigher than the root that - // we inflate. Find all the views up to the root and create ViewHeightControllers for - // them. - mHeightControllers = new ArrayList<>(); - ViewRootImpl viewRoot = mRootView.getViewRootImpl(); - ViewParent v = mRootView.getParent(); - addHeightController(mDialogContainer); - addHeightController(mRootView); - while (v != viewRoot) { - addHeightController((View) v); - v = v.getParent(); - } - // On the heighest level view, we want to setTop rather than setBottom to control the - // height, this way the dialog will grow up rather than down. - ViewHeightController realRootView = - mHeightControllers.get(mHeightControllers.size() - 1); - realRootView.setControlTop(true); - } - - // Grab the current height/y positions, then wait for the layout to change, - // so we can get the end height/y positions. - final SparseArray startPositions = getViewPositions(); - final int startHeight = mRootView.getLayoutHeight(); - mRootView.addOnLayoutChangeListener(new OnLayoutChangeListener() { - @Override - public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, - int oldTop, int oldRight, int oldBottom) { - mRootView.removeOnLayoutChangeListener(this); - SparseArray endPositions = getViewPositions(); - int endHeight = mRootView.getLayoutHeight(); - if (startPositions.get(R.id.do_not_ask_checkbox) == 0 - && endPositions.get(R.id.do_not_ask_checkbox) != 0) { - // If the checkbox didn't have a position before but has one now then set - // the start position to the end position because it just became visible. - startPositions.put(R.id.do_not_ask_checkbox, - endPositions.get(R.id.do_not_ask_checkbox)); - } - animateYPos(startPositions, endPositions, endHeight - startHeight); - } - }); - - // Fade out old description group and scale out the icon for it. - Interpolator interpolator = AnimationUtils.loadInterpolator(mContext, - android.R.interpolator.fast_out_linear_in); - mIconView.animate() - .scaleX(0) - .scaleY(0) - .setStartDelay(FADE_OUT_START_DELAY) - .setDuration(FADE_OUT_START_LENGTH) - .setInterpolator(interpolator) - .start(); - mCurrentDesc.animate() - .alpha(0) - .setStartDelay(FADE_OUT_START_DELAY) - .setDuration(FADE_OUT_START_LENGTH) - .setInterpolator(interpolator) - .setListener(null) - .start(); - - // Update the index of the permission after the animations have started. - mCurrentGroupView.getHandler().postDelayed(mUpdateGroup, GROUP_UPDATE_DELAY); - - // Add the new description and translate it in. - mNextDesc = (ViewGroup) LayoutInflater.from(mContext).inflate( - R.layout.permission_description, mDescContainer, false); - - mMessageView = (TextView) mNextDesc.findViewById(R.id.permission_message); - mIconView = (ImageView) mNextDesc.findViewById(R.id.permission_icon); - updateDescription(); - - int width = mDescContainer.getRootView().getWidth(); - mDescContainer.addView(mNextDesc); - mNextDesc.setTranslationX(width); - - final View oldDesc = mCurrentDesc; - // Remove the old view from the description, so that we can shrink if necessary. - mDescContainer.removeView(oldDesc); - oldDesc.setPadding(mDescContainer.getLeft(), mDescContainer.getTop(), - mRootView.getRight() - mDescContainer.getRight(), 0); - mRootView.addView(oldDesc); - - mCurrentDesc = mNextDesc; - mNextDesc.animate() - .translationX(0) - .setStartDelay(TRANSLATE_START_DELAY) - .setDuration(TRANSLATE_LENGTH) - .setInterpolator(AnimationUtils.loadInterpolator(mContext, - android.R.interpolator.linear_out_slow_in)) - .setListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - // This is the longest animation, when it finishes, we are done. - mRootView.removeView(oldDesc); - } - }) - .start(); - - boolean visibleBefore = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; - updateDoNotAskCheckBox(); - boolean visibleAfter = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; - if (visibleBefore != visibleAfter) { - Animation anim = AnimationUtils.loadAnimation(mContext, - visibleAfter ? android.R.anim.fade_in : android.R.anim.fade_out); - anim.setStartOffset(visibleAfter ? DO_NOT_ASK_CHECK_DELAY : 0); - mDoNotAskCheckbox.startAnimation(anim); - } - } - - private void addHeightController(View v) { - ViewHeightController heightController = new ViewHeightController(v); - heightController.setHeight(v.getHeight()); - mHeightControllers.add(heightController); - } - - private SparseArray getViewPositions() { - SparseArray locMap = new SparseArray<>(); - final int N = mDialogContainer.getChildCount(); - for (int i = 0; i < N; i++) { - View child = mDialogContainer.getChildAt(i); - if (child.getId() <= 0) { - // Only track views with ids. - continue; - } - locMap.put(child.getId(), child.getY()); - } - return locMap; - } - - private void animateYPos(SparseArray startPositions, SparseArray endPositions, - int heightDiff) { - final int N = startPositions.size(); - for (int i = 0; i < N; i++) { - int key = startPositions.keyAt(i); - float start = startPositions.get(key); - float end = endPositions.get(key); - if (start != end) { - final View child = mDialogContainer.findViewById(key); - child.setTranslationY(start - end); - child.animate() - .setStartDelay(SIZE_START_DELAY) - .setDuration(SIZE_START_LENGTH) - .translationY(0) - .start(); - } - } - for (int i = 0; i < mHeightControllers.size(); i++) { - mHeightControllers.get(i).animateAddHeight(heightDiff); - } - } - - @Override - public View createView() { - mRootView = (ManualLayoutFrame) LayoutInflater.from(mContext) - .inflate(R.layout.grant_permissions, null); - ((ButtonBarLayout) mRootView.findViewById(R.id.button_group)).setAllowStacking( - Resources.getSystem().getBoolean( - com.android.internal.R.bool.allow_stacked_button_bar)); - - mDialogContainer = (ViewGroup) mRootView.findViewById(R.id.dialog_container); - mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); - mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); - mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); - mDoNotAskCheckbox = (CheckBox) mRootView.findViewById(R.id.do_not_ask_checkbox); - mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); - - mDescContainer = (ViewGroup) mRootView.findViewById(R.id.desc_container); - mCurrentDesc = (ViewGroup) mRootView.findViewById(R.id.perm_desc_root); - - mAllowButton.setOnClickListener(this); - mRootView.findViewById(R.id.permission_deny_button).setOnClickListener(this); - mDoNotAskCheckbox.setOnClickListener(this); - - if (mGroupName != null) { - updateDescription(); - updateGroup(); - updateDoNotAskCheckBox(); - } - - return mRootView; - } - - @Override - public void updateWindowAttributes(LayoutParams outLayoutParams) { - // No-op - } - - private void updateDescription() { - mIconView.setImageDrawable(mGroupIcon.loadDrawable(mContext)); - mMessageView.setText(mGroupMessage); - } - - private void updateGroup() { - if (mGroupCount > 1) { - mCurrentGroupView.setVisibility(View.VISIBLE); - mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, - mGroupIndex + 1, mGroupCount)); - } else { - mCurrentGroupView.setVisibility(View.INVISIBLE); - } - } - - private void updateDoNotAskCheckBox() { - if (mShowDonNotAsk) { - mDoNotAskCheckbox.setVisibility(View.VISIBLE); - mDoNotAskCheckbox.setOnClickListener(this); - mDoNotAskCheckbox.setChecked(mDoNotAskChecked); - } else { - mDoNotAskCheckbox.setVisibility(View.GONE); - mDoNotAskCheckbox.setOnClickListener(null); - } - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.permission_allow_button: - if (mResultListener != null) { - view.clearAccessibilityFocus(); - mResultListener.onPermissionGrantResult(mGroupName, true, false); - } - break; - case R.id.permission_deny_button: - mAllowButton.setEnabled(true); - if (mResultListener != null) { - view.clearAccessibilityFocus(); - mResultListener.onPermissionGrantResult(mGroupName, false, - mDoNotAskCheckbox.isChecked()); - } - break; - case R.id.do_not_ask_checkbox: - mAllowButton.setEnabled(!mDoNotAskCheckbox.isChecked()); - break; - } - } - - @Override - public void onBackPressed() { - if (mResultListener != null) { - final boolean doNotAskAgain = mDoNotAskCheckbox.isChecked(); - mResultListener.onPermissionGrantResult(mGroupName, false, doNotAskAgain); - } - } - - /** - * Manually controls the height of a view through getBottom/setTop. Also listens - * for layout changes and sets the height again to be sure it doesn't change. - */ - private static final class ViewHeightController implements OnLayoutChangeListener { - private final View mView; - private int mHeight; - private int mNextHeight; - private boolean mControlTop; - private ObjectAnimator mAnimator; - - public ViewHeightController(View view) { - mView = view; - mView.addOnLayoutChangeListener(this); - } - - public void setControlTop(boolean controlTop) { - mControlTop = controlTop; - } - - public void animateAddHeight(int heightDiff) { - if (heightDiff != 0) { - if (mNextHeight == 0) { - mNextHeight = mHeight; - } - mNextHeight += heightDiff; - if (mAnimator != null) { - mAnimator.cancel(); - } - mAnimator = ObjectAnimator.ofInt(this, "height", mHeight, mNextHeight); - mAnimator.setStartDelay(SIZE_START_DELAY); - mAnimator.setDuration(SIZE_START_LENGTH); - mAnimator.start(); - } - } - - public void setHeight(int height) { - mHeight = height; - updateHeight(); - } - - @Override - public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, - int oldTop, int oldRight, int oldBottom) { - // Ensure that the height never changes. - updateHeight(); - } - - private void updateHeight() { - if (mControlTop) { - mView.setTop(mView.getBottom() - mHeight); - } else { - mView.setBottom(mView.getTop() + mHeight); - } - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java deleted file mode 100644 index 0e979ab6..00000000 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.android.packageinstaller.permission.ui; - -import android.content.Context; -import android.graphics.PixelFormat; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.view.Gravity; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.android.packageinstaller.R; - -/** - * TV-specific view handler for the grant permissions activity. - */ -final class GrantPermissionsTvViewHandler implements GrantPermissionsViewHandler, OnClickListener { - - private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; - - private final Context mContext; - - private ResultListener mResultListener; - - private String mGroupName; - - private LinearLayout mRootView; - private TextView mMessageView; - private ImageView mIconView; - private TextView mCurrentGroupView; - private Button mAllowButton; - private Button mSoftDenyButton; - private Button mHardDenyButton; - - GrantPermissionsTvViewHandler(Context context) { - mContext = context; - } - - @Override - public GrantPermissionsTvViewHandler setResultListener(ResultListener listener) { - mResultListener = listener; - return this; - } - - @Override - public View createView() { - mRootView = (LinearLayout) LayoutInflater.from(mContext) - .inflate(R.layout.grant_permissions, null); - - mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); - mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); - mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); - mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); - mSoftDenyButton = (Button) mRootView.findViewById(R.id.permission_deny_button); - mHardDenyButton = (Button) mRootView.findViewById( - R.id.permission_deny_dont_ask_again_button); - - mAllowButton.setOnClickListener(this); - mSoftDenyButton.setOnClickListener(this); - mHardDenyButton.setOnClickListener(this); - - return mRootView; - } - - @Override - public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { - outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; - outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; - outLayoutParams.format = PixelFormat.OPAQUE; - outLayoutParams.gravity = Gravity.BOTTOM; - outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; - outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; - } - - @Override - public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, - CharSequence message, boolean showDoNotAsk) { - mGroupName = groupName; - - mMessageView.setText(message); - mIconView.setImageIcon(icon); - mHardDenyButton.setVisibility(showDoNotAsk ? View.VISIBLE : View.GONE); - if (groupCount > 1) { - mCurrentGroupView.setVisibility(View.VISIBLE); - mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, - groupIndex + 1, groupCount)); - } else { - mCurrentGroupView.setVisibility(View.INVISIBLE); - } - } - - @Override - public void saveInstanceState(Bundle outState) { - outState.putString(ARG_GROUP_NAME, mGroupName); - } - - @Override - public void loadInstanceState(Bundle savedInstanceState) { - mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); - } - - @Override - public void onClick(View view) { - boolean granted = false; - boolean doNotAskAgain = false; - switch (view.getId()) { - case R.id.permission_allow_button: - granted = true; - break; - case R.id.permission_deny_dont_ask_again_button: - doNotAskAgain = true; - break; - } - if (mResultListener != null) { - mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); - } - } - - @Override - public void onBackPressed() { - if (mResultListener != null) { - mResultListener.onPermissionGrantResult(mGroupName, false, false); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java index 4032abb2..5e2259af 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java @@ -25,7 +25,7 @@ import android.view.WindowManager; * Class for managing the presentation and user interaction of the "grant * permissions" user interface. */ -interface GrantPermissionsViewHandler { +public interface GrantPermissionsViewHandler { /** * Listener interface for getting notified when the user responds to a diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java index 8ba6b127..f5703564 100644 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java @@ -20,6 +20,7 @@ import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.util.Log; +import com.android.packageinstaller.permission.utils.Utils; public final class ManagePermissionsActivity extends OverlayTouchActivity { private static final String LOG_TAG = "ManagePermissionsActivity"; @@ -37,7 +38,13 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { switch (action) { case Intent.ACTION_MANAGE_PERMISSIONS: { - fragment = ManagePermissionsFragment.newInstance(); + if (Utils.isTelevision(this)) { + fragment = com.android.packageinstaller.permission.ui.television + .ManagePermissionsFragment.newInstance(); + } else { + fragment = com.android.packageinstaller.permission.ui.handheld + .ManagePermissionsFragment.newInstance(); + } } break; case Intent.ACTION_MANAGE_APP_PERMISSIONS: { @@ -47,7 +54,13 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { finish(); return; } - fragment = AppPermissionsFragment.newInstance(packageName); + if (Utils.isTelevision(this)) { + fragment = com.android.packageinstaller.permission.ui.television + .AppPermissionsFragment.newInstance(packageName); + } else { + fragment = com.android.packageinstaller.permission.ui.handheld + .AppPermissionsFragment.newInstance(packageName); + } } break; case Intent.ACTION_MANAGE_PERMISSION_APPS: { @@ -57,7 +70,13 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { finish(); return; } - fragment = PermissionAppsFragment.newInstance(permissionName); + if (Utils.isTelevision(this)) { + fragment = com.android.packageinstaller.permission.ui.television + .PermissionAppsFragment.newInstance(permissionName); + } else { + fragment = com.android.packageinstaller.permission.ui.handheld + .PermissionAppsFragment.newInstance(permissionName); + } } break; default: { diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java deleted file mode 100644 index e5e06e09..00000000 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java +++ /dev/null @@ -1,267 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.annotation.Nullable; -import android.app.ActionBar; -import android.app.FragmentTransaction; -import android.content.ActivityNotFoundException; -import android.content.Context; -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceScreen; -import android.util.ArraySet; -import android.util.Log; -import android.view.MenuItem; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.model.PermissionApps; -import com.android.packageinstaller.permission.model.PermissionApps.PmCache; -import com.android.packageinstaller.permission.model.PermissionGroup; -import com.android.packageinstaller.permission.model.PermissionGroups; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.List; - -public final class ManagePermissionsFragment extends PermissionsFrameFragment - implements PermissionGroups.PermissionsGroupsChangeCallback, OnPreferenceClickListener { - private static final String LOG_TAG = "ManagePermissionsFragment"; - - private static final String OS_PKG = "android"; - - private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; - - private ArraySet mLauncherPkgs; - - private PermissionGroups mPermissions; - - private PreferenceScreen mExtraScreen; - - public static ManagePermissionsFragment newInstance() { - return new ManagePermissionsFragment(); - } - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - setLoading(true /* loading */, false /* animate */); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - } - mLauncherPkgs = Utils.getLauncherPackages(getContext()); - mPermissions = new PermissionGroups(getActivity(), getLoaderManager(), this); - } - - @Override - public void onResume() { - super.onResume(); - mPermissions.refresh(); - updatePermissionsUi(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - getActivity().finish(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public boolean onPreferenceClick(Preference preference) { - String key = preference.getKey(); - - PermissionGroup group = mPermissions.getGroup(key); - if (group == null) { - return false; - } - - Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) - .putExtra(Intent.EXTRA_PERMISSION_NAME, key); - try { - getActivity().startActivity(intent); - } catch (ActivityNotFoundException e) { - Log.w(LOG_TAG, "No app to handle " + intent); - } - - return true; - } - - @Override - public void onPermissionGroupsChanged() { - updatePermissionsUi(); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - bindPermissionUi(getActivity(), getView()); - } - - private static void bindPermissionUi(@Nullable Context context, @Nullable View rootView) { - if (context == null || rootView == null) { - return; - } - - ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); - if (iconView != null) { - // Set the icon as the background instead of the image because ImageView - // doesn't properly scale vector drawables beyond their intrinsic size - Drawable icon = context.getDrawable(R.drawable.ic_lock); - icon.setTint(context.getColor(R.color.off_white)); - iconView.setBackground(icon); - } - TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); - if (titleView != null) { - titleView.setText(R.string.app_permissions); - } - TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); - if (breadcrumbView != null) { - breadcrumbView.setText(R.string.app_permissions_breadcrumb); - } - } - - private void updatePermissionsUi() { - Context context = getPreferenceManager().getContext(); - if (context == null) { - return; - } - - List groups = mPermissions.getGroups(); - PreferenceScreen screen = getPreferenceScreen(); - - // Use this to speed up getting the info for all of the PermissionApps below. - // Create a new one for each refresh to make sure it has fresh data. - PmCache cache = new PmCache(getContext().getPackageManager()); - for (PermissionGroup group : groups) { - boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); - - Preference preference = findPreference(group.getName()); - if (preference == null && mExtraScreen != null) { - preference = mExtraScreen.findPreference(group.getName()); - } - if (preference == null) { - preference = new Preference(context); - preference.setOnPreferenceClickListener(this); - preference.setKey(group.getName()); - preference.setIcon(Utils.applyTint(context, group.getIcon(), - android.R.attr.colorControlNormal)); - preference.setTitle(group.getLabel()); - // Set blank summary so that no resizing/jumping happens when the summary is loaded. - preference.setSummary(" "); - preference.setPersistent(false); - if (isSystemPermission) { - screen.addPreference(preference); - } else { - if (mExtraScreen == null) { - mExtraScreen = getPreferenceManager().createPreferenceScreen(context); - } - mExtraScreen.addPreference(preference); - } - } - final Preference finalPref = preference; - - new PermissionApps(getContext(), group.getName(), new PermissionApps.Callback() { - @Override - public void onPermissionsLoaded(PermissionApps permissionApps) { - if (getActivity() == null) { - return; - } - int granted = permissionApps.getGrantedCount(mLauncherPkgs); - int total = permissionApps.getTotalCount(mLauncherPkgs); - finalPref.setSummary(getString(R.string.app_permissions_group_summary, - granted, total)); - } - }, cache).refresh(false); - } - - if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 - && screen.findPreference(EXTRA_PREFS_KEY) == null) { - Preference extraScreenPreference = new Preference(context); - extraScreenPreference.setKey(EXTRA_PREFS_KEY); - extraScreenPreference.setIcon(Utils.applyTint(context, - com.android.internal.R.drawable.ic_more_items, - android.R.attr.colorControlNormal)); - extraScreenPreference.setTitle(R.string.additional_permissions); - extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); - frag.setTargetFragment(ManagePermissionsFragment.this, 0); - FragmentTransaction ft = getFragmentManager().beginTransaction(); - ft.replace(android.R.id.content, frag); - ft.addToBackStack(null); - ft.commit(); - return true; - } - }); - int count = mExtraScreen.getPreferenceCount(); - extraScreenPreference.setSummary(getResources().getQuantityString( - R.plurals.additional_permissions_more, count, count)); - screen.addPreference(extraScreenPreference); - } - if (screen.getPreferenceCount() != 0) { - setLoading(false /* loading */, true /* animate */); - } - } - - public static class AdditionalPermissionsFragment extends PermissionsFrameFragment { - @Override - public void onCreate(Bundle icicle) { - setLoading(true /* loading */, false /* animate */); - super.onCreate(icicle); - getActivity().setTitle(R.string.additional_permissions); - setHasOptionsMenu(true); - } - - @Override - public void onDestroy() { - getActivity().setTitle(R.string.app_permissions); - super.onDestroy(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getFragmentManager().popBackStack(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - bindPermissionUi(getActivity(), getView()); - } - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); - setLoading(false /* loading */, true /* animate */); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java b/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java index a7c1e2a1..61734b47 100644 --- a/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java +++ b/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.android.packageinstaller.permission.ui; import android.app.Activity; diff --git a/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java deleted file mode 100644 index 8dacd037..00000000 --- a/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java +++ /dev/null @@ -1,433 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.app.ActionBar; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.support.v14.preference.SwitchPreference; -import android.support.v4.util.ArrayMap; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceChangeListener; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceScreen; -import android.util.ArraySet; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.model.AppPermissionGroup; -import com.android.packageinstaller.permission.model.PermissionApps; -import com.android.packageinstaller.permission.model.PermissionApps.Callback; -import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; -import com.android.packageinstaller.permission.utils.LocationUtils; -import com.android.packageinstaller.permission.utils.SafetyNetLogger; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.ArrayList; -import java.util.List; - -public final class PermissionAppsFragment extends PermissionsFrameFragment implements Callback, - OnPreferenceChangeListener { - - private static final int MENU_SHOW_SYSTEM = Menu.FIRST; - private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 1; - private static final String KEY_SHOW_SYSTEM_PREFS = "_showSystem"; - - public static PermissionAppsFragment newInstance(String permissionName) { - return setPermissionName(new PermissionAppsFragment(), permissionName); - } - - private static T setPermissionName(T fragment, String permissionName) { - Bundle arguments = new Bundle(); - arguments.putString(Intent.EXTRA_PERMISSION_NAME, permissionName); - fragment.setArguments(arguments); - return fragment; - } - - private PermissionApps mPermissionApps; - - private PreferenceScreen mExtraScreen; - - private ArrayMap mToggledGroups; - private ArraySet mLauncherPkgs; - private boolean mHasConfirmedRevoke; - - private boolean mShowSystem; - private MenuItem mShowSystemMenu; - private MenuItem mHideSystemMenu; - - private Callback mOnPermissionsLoadedListener; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setLoading(true /* loading */, false /* animate */); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - } - mLauncherPkgs = Utils.getLauncherPackages(getContext()); - - String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); - mPermissionApps = new PermissionApps(getActivity(), groupName, this); - mPermissionApps.refresh(true); - } - - @Override - public void onResume() { - super.onResume(); - mPermissionApps.refresh(true); - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, - R.string.menu_show_system); - mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE, - R.string.menu_hide_system); - updateMenu(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getActivity().finish(); - return true; - case MENU_SHOW_SYSTEM: - case MENU_HIDE_SYSTEM: - mShowSystem = item.getItemId() == MENU_SHOW_SYSTEM; - if (mPermissionApps.getApps() != null) { - onPermissionsLoaded(mPermissionApps); - } - updateMenu(); - break; - } - return super.onOptionsItemSelected(item); - } - - private void updateMenu() { - mShowSystemMenu.setVisible(!mShowSystem); - mHideSystemMenu.setVisible(mShowSystem); - } - - @Override - protected void onSetEmptyText(TextView textView) { - textView.setText(R.string.no_apps); - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - bindUi(this, mPermissionApps); - } - - private static void bindUi(Fragment fragment, PermissionApps permissionApps) { - final Drawable icon = permissionApps.getIcon(); - final CharSequence label = permissionApps.getLabel(); - final ActionBar ab = fragment.getActivity().getActionBar(); - if (ab != null) { - ab.setTitle(fragment.getString(R.string.permission_title, label)); - } - - final ViewGroup rootView = (ViewGroup) fragment.getView(); - final ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); - if (iconView != null) { - // Set the icon as the background instead of the image because ImageView - // doesn't properly scale vector drawables beyond their intrinsic size - iconView.setBackground(icon); - } - final TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); - if (titleView != null) { - titleView.setText(label); - } - final TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); - if (breadcrumbView != null) { - breadcrumbView.setText(R.string.app_permissions); - } - } - - private void setOnPermissionsLoadedListener(Callback callback) { - mOnPermissionsLoadedListener = callback; - } - - @Override - public void onPermissionsLoaded(PermissionApps permissionApps) { - Context context = getPreferenceManager().getContext(); - - if (context == null) { - return; - } - - boolean isTelevision = Utils.isTelevision(context); - PreferenceScreen screen = getPreferenceScreen(); - - ArraySet preferencesToRemove = new ArraySet<>(); - for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) { - preferencesToRemove.add(screen.getPreference(i).getKey()); - } - if (mExtraScreen != null) { - for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { - preferencesToRemove.add(mExtraScreen.getPreference(i).getKey()); - } - } - - for (PermissionApp app : permissionApps.getApps()) { - if (!Utils.shouldShowPermission(app)) { - continue; - } - - String key = app.getKey(); - preferencesToRemove.remove(key); - Preference existingPref = screen.findPreference(key); - if (existingPref == null && mExtraScreen != null) { - existingPref = mExtraScreen.findPreference(key); - } - - boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs); - if (isSystemApp && !isTelevision && !mShowSystem) { - if (existingPref != null) { - screen.removePreference(existingPref); - } - continue; - } - - if (existingPref != null) { - // If existing preference - only update its state. - if (app.isPolicyFixed()) { - existingPref.setSummary(getString( - R.string.permission_summary_enforced_by_policy)); - } - existingPref.setPersistent(false); - existingPref.setEnabled(!app.isPolicyFixed()); - if (existingPref instanceof SwitchPreference) { - ((SwitchPreference) existingPref) - .setChecked(app.areRuntimePermissionsGranted()); - } - continue; - } - - SwitchPreference pref = new SwitchPreference(context); - pref.setOnPreferenceChangeListener(this); - pref.setKey(app.getKey()); - pref.setIcon(app.getIcon()); - pref.setTitle(app.getLabel()); - if (app.isPolicyFixed()) { - pref.setSummary(getString(R.string.permission_summary_enforced_by_policy)); - } - pref.setPersistent(false); - pref.setEnabled(!app.isPolicyFixed()); - pref.setChecked(app.areRuntimePermissionsGranted()); - - if (isSystemApp && isTelevision) { - if (mExtraScreen == null) { - mExtraScreen = getPreferenceManager().createPreferenceScreen(context); - } - mExtraScreen.addPreference(pref); - } else { - screen.addPreference(pref); - } - } - - if (mExtraScreen != null) { - preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS); - Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS); - - if (pref == null) { - pref = new Preference(context); - pref.setKey(KEY_SHOW_SYSTEM_PREFS); - pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, - android.R.attr.colorControlNormal)); - pref.setTitle(R.string.preference_show_system_apps); - pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - SystemAppsFragment frag = new SystemAppsFragment(); - setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME)); - frag.setTargetFragment(PermissionAppsFragment.this, 0); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, frag) - .addToBackStack("SystemApps") - .commit(); - return true; - } - }); - screen.addPreference(pref); - } - - int grantedCount = 0; - for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { - if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) { - grantedCount++; - } - } - pref.setSummary(getString(R.string.app_permissions_group_summary, - grantedCount, mExtraScreen.getPreferenceCount())); - } - - for (String key : preferencesToRemove) { - Preference pref = screen.findPreference(key); - if (pref != null) { - screen.removePreference(pref); - } else if (mExtraScreen != null) { - pref = mExtraScreen.findPreference(key); - if (pref != null) { - mExtraScreen.removePreference(pref); - } - } - } - - setLoading(false /* loading */, true /* animate */); - - if (mOnPermissionsLoadedListener != null) { - mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps); - } - } - - @Override - public boolean onPreferenceChange(final Preference preference, Object newValue) { - String pkg = preference.getKey(); - final PermissionApp app = mPermissionApps.getApp(pkg); - - if (app == null) { - return false; - } - - OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); - if (activity.isObscuredTouch()) { - activity.showOverlayDialog(); - return false; - } - - addToggledGroup(app.getPackageName(), app.getPermissionGroup()); - - if (LocationUtils.isLocationGroupAndProvider(mPermissionApps.getGroupName(), - app.getPackageName())) { - LocationUtils.showLocationDialog(getContext(), app.getLabel()); - return false; - } - if (newValue == Boolean.TRUE) { - app.grantRuntimePermissions(); - } else { - final boolean grantedByDefault = app.hasGrantedByDefaultPermissions(); - if (grantedByDefault || (!app.hasRuntimePermissions() && !mHasConfirmedRevoke)) { - new AlertDialog.Builder(getContext()) - .setMessage(grantedByDefault ? R.string.system_warning - : R.string.old_sdk_deny_warning) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.grant_dialog_button_deny, - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - ((SwitchPreference) preference).setChecked(false); - app.revokeRuntimePermissions(); - if (!grantedByDefault) { - mHasConfirmedRevoke = true; - } - } - }) - .show(); - return false; - } else { - app.revokeRuntimePermissions(); - } - } - return true; - } - - @Override - public void onPause() { - super.onPause(); - logToggledGroups(); - } - - private void addToggledGroup(String packageName, AppPermissionGroup group) { - if (mToggledGroups == null) { - mToggledGroups = new ArrayMap<>(); - } - // Double toggle is back to initial state. - if (mToggledGroups.containsKey(packageName)) { - mToggledGroups.remove(packageName); - } else { - mToggledGroups.put(packageName, group); - } - } - - private void logToggledGroups() { - if (mToggledGroups != null) { - final int groupCount = mToggledGroups.size(); - for (int i = 0; i < groupCount; i++) { - String packageName = mToggledGroups.keyAt(i); - List groups = new ArrayList<>(); - groups.add(mToggledGroups.valueAt(i)); - SafetyNetLogger.logPermissionsToggled(packageName, groups); - } - mToggledGroups = null; - } - } - - public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback { - PermissionAppsFragment mOuterFragment; - - @Override - public void onCreate(Bundle savedInstanceState) { - mOuterFragment = (PermissionAppsFragment) getTargetFragment(); - setLoading(true /* loading */, false /* animate */); - super.onCreate(savedInstanceState); - } - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - if (mOuterFragment.mExtraScreen != null) { - setPreferenceScreen(); - } else { - mOuterFragment.setOnPermissionsLoadedListener(this); - } - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); - PermissionApps permissionApps = new PermissionApps(getActivity(), groupName, null); - bindUi(this, permissionApps); - } - - @Override - public void onPermissionsLoaded(PermissionApps permissionApps) { - setPreferenceScreen(); - mOuterFragment.setOnPermissionsLoadedListener(null); - } - - private void setPreferenceScreen() { - setPreferenceScreen(mOuterFragment.mExtraScreen); - setLoading(false /* loading */, true /* animate */); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java deleted file mode 100644 index 40058f6d..00000000 --- a/src/com/android/packageinstaller/permission/ui/PermissionsFrameFragment.java +++ /dev/null @@ -1,187 +0,0 @@ -package com.android.packageinstaller.permission.ui; - -import android.annotation.Nullable; -import android.os.Bundle; -import android.support.v14.preference.PreferenceFragment; -import android.support.v17.leanback.widget.VerticalGridView; -import android.support.v7.preference.PreferenceScreen; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.RecyclerView.AdapterDataObserver; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.animation.Animation; -import android.view.animation.Animation.AnimationListener; -import android.view.animation.AnimationUtils; -import android.widget.TextView; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.utils.Utils; - -public abstract class PermissionsFrameFragment extends PreferenceFragment { - - private static final float WINDOW_ALIGNMENT_OFFSET_PERCENT = 50; - - private ViewGroup mPreferencesContainer; - - // TV-specific instance variables - @Nullable private VerticalGridView mGridView; - - private View mLoadingView; - private ViewGroup mPrefsView; - private boolean mIsLoading; - - /** - * Returns the view group that holds the preferences objects. This will - * only be set after {@link #onCreateView} has been called. - */ - protected final ViewGroup getPreferencesContainer() { - return mPreferencesContainer; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container, - false); - mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container); - if (mPrefsView == null) { - mPrefsView = rootView; - } - mLoadingView = rootView.findViewById(R.id.loading_container); - mPreferencesContainer = (ViewGroup) super.onCreateView( - inflater, mPrefsView, savedInstanceState); - setLoading(mIsLoading, false, true /* force */); - mPrefsView.addView(mPreferencesContainer); - return rootView; - } - - @Override - public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) { - PreferenceScreen preferences = getPreferenceScreen(); - if (preferences == null) { - preferences = getPreferenceManager().createPreferenceScreen(getActivity()); - setPreferenceScreen(preferences); - } - } - - protected void setLoading(boolean loading, boolean animate) { - setLoading(loading, animate, false); - } - - private void setLoading(boolean loading, boolean animate, boolean force) { - if (mIsLoading != loading || force) { - mIsLoading = loading; - if (getView() == null) { - // If there is no created view, there is no reason to animate. - animate = false; - } - if (mPrefsView != null) { - setViewShown(mPrefsView, !loading, animate); - } - if (mLoadingView != null) { - setViewShown(mLoadingView, loading, animate); - } - } - } - - private void setViewShown(final View view, boolean shown, boolean animate) { - if (animate) { - Animation animation = AnimationUtils.loadAnimation(getContext(), - shown ? android.R.anim.fade_in : android.R.anim.fade_out); - if (shown) { - view.setVisibility(View.VISIBLE); - } else { - animation.setAnimationListener(new AnimationListener() { - @Override - public void onAnimationStart(Animation animation) { - } - - @Override - public void onAnimationRepeat(Animation animation) { - } - - @Override - public void onAnimationEnd(Animation animation) { - view.setVisibility(View.INVISIBLE); - } - }); - } - view.startAnimation(animation); - } else { - view.clearAnimation(); - view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); - } - } - - @Override - public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, - Bundle savedInstanceState) { - if (Utils.isTelevision(getContext())) { - mGridView = (VerticalGridView) inflater.inflate( - R.layout.leanback_preferences_list, parent, false); - mGridView.setWindowAlignmentOffset(0); - mGridView.setWindowAlignmentOffsetPercent(WINDOW_ALIGNMENT_OFFSET_PERCENT); - mGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE); - mGridView.setFocusScrollStrategy(VerticalGridView.FOCUS_SCROLL_ALIGNED); - return mGridView; - } else { - return super.onCreateRecyclerView(inflater, parent, savedInstanceState); - } - } - - @Override - protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { - final RecyclerView.Adapter adapter = super.onCreateAdapter(preferenceScreen); - - if (adapter != null) { - final TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions); - onSetEmptyText(emptyView); - final RecyclerView recyclerView = getListView(); - adapter.registerAdapterDataObserver(new AdapterDataObserver() { - @Override - public void onChanged() { - checkEmpty(); - } - - @Override - public void onItemRangeInserted(int positionStart, int itemCount) { - checkEmpty(); - } - - @Override - public void onItemRangeRemoved(int positionStart, int itemCount) { - checkEmpty(); - } - - private void checkEmpty() { - boolean isEmpty = adapter.getItemCount() == 0; - emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); - recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE); - if (!isEmpty && mGridView != null) { - mGridView.requestFocus(); - } - } - }); - - boolean isEmpty = adapter.getItemCount() == 0; - emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); - recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE); - if (!isEmpty && mGridView != null) { - mGridView.requestFocus(); - } - } - - return adapter; - } - - /** - * Hook for subclasses to change the default text of the empty view. - * Base implementation leaves the default empty view text. - * - * @param textView the empty text view - */ - protected void onSetEmptyText(TextView textView) { - } -} - diff --git a/src/com/android/packageinstaller/permission/ui/PreferenceImageView.java b/src/com/android/packageinstaller/permission/ui/PreferenceImageView.java new file mode 100644 index 00000000..c3f51674 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/PreferenceImageView.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2014 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.packageinstaller.permission.ui; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.ImageView; + +/** + * Extension of ImageView that correctly applies maxWidth and maxHeight. + */ +public class PreferenceImageView extends ImageView { + + public PreferenceImageView(Context context) { + this(context, null); + } + + public PreferenceImageView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) { + this(context, attrs, defStyleAttr, 0); + } + + public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + final int widthMode = MeasureSpec.getMode(widthMeasureSpec); + if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) { + final int widthSize = MeasureSpec.getSize(widthMeasureSpec); + final int maxWidth = getMaxWidth(); + if (maxWidth != Integer.MAX_VALUE + && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) { + widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST); + } + } + + final int heightMode = MeasureSpec.getMode(heightMeasureSpec); + if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) { + final int heightSize = MeasureSpec.getSize(heightMeasureSpec); + final int maxHeight = getMaxHeight(); + if (maxHeight != Integer.MAX_VALUE + && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) { + heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); + } + } + + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } +} diff --git a/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java deleted file mode 100644 index 7b58fed1..00000000 --- a/src/com/android/packageinstaller/permission/ui/SettingsWithHeader.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.utils.Utils; - -public abstract class SettingsWithHeader extends PermissionsFrameFragment - implements OnClickListener { - - private View mHeader; - protected Intent mInfoIntent; - protected Drawable mIcon; - protected CharSequence mLabel; - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); - - if (!Utils.isTelevision(getContext())) { - mHeader = inflater.inflate(R.layout.header, root, false); - getPreferencesContainer().addView(mHeader, 0); - updateHeader(); - } - - return root; - } - - public void setHeader(Drawable icon, CharSequence label, Intent infoIntent) { - mIcon = icon; - mLabel = label; - mInfoIntent = infoIntent; - updateHeader(); - } - - private void updateHeader() { - if (mHeader != null) { - final ImageView appIcon = (ImageView) mHeader.findViewById(R.id.icon); - appIcon.setImageDrawable(mIcon); - - final TextView appName = (TextView) mHeader.findViewById(R.id.name); - appName.setText(mLabel); - - final View info = mHeader.findViewById(R.id.info); - if (mInfoIntent == null) { - info.setVisibility(View.GONE); - } else { - info.setVisibility(View.VISIBLE); - info.setClickable(true); - info.setOnClickListener(this); - } - } - } - - @Override - public void onClick(View v) { - getActivity().startActivity(mInfoIntent); - } - -} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java new file mode 100644 index 00000000..b3b0895c --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java @@ -0,0 +1,214 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageItemInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionGroupInfo; +import android.content.pm.PermissionInfo; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceCategory; +import android.preference.PreferenceGroup; +import android.provider.Settings; +import android.util.Log; +import android.view.MenuItem; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public final class AllAppPermissionsFragment extends SettingsWithHeader { + + private static final String LOG_TAG = "AllAppPermissionsFragment"; + + private static final String KEY_OTHER = "other_perms"; + + public static AllAppPermissionsFragment newInstance(String packageName) { + AllAppPermissionsFragment instance = new AllAppPermissionsFragment(); + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + instance.setArguments(arguments); + return instance; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(R.string.all_permissions); + ab.setDisplayHomeAsUpEnabled(true); + } + } + + @Override + public void onResume() { + super.onResume(); + updateUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getFragmentManager().popBackStack(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + private void updateUi() { + if (getPreferenceScreen() != null) { + getPreferenceScreen().removeAll(); + } + addPreferencesFromResource(R.xml.all_permissions); + PreferenceGroup otherGroup = (PreferenceGroup) findPreference(KEY_OTHER); + ArrayList prefs = new ArrayList<>(); // Used for sorting. + prefs.add(otherGroup); + String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + otherGroup.removeAll(); + PackageManager pm = getContext().getPackageManager(); + + try { + PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS); + + ApplicationInfo appInfo = info.applicationInfo; + final Drawable icon = appInfo.loadIcon(pm); + final CharSequence label = appInfo.loadLabel(pm); + Intent infoIntent = null; + if (!getActivity().getIntent().getBooleanExtra( + AppPermissionsFragment.EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", pkg, null)); + } + setHeader(icon, label, infoIntent); + + if (info.requestedPermissions != null) { + for (int i = 0; i < info.requestedPermissions.length; i++) { + PermissionInfo perm; + try { + perm = pm.getPermissionInfo(info.requestedPermissions[i], 0); + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, + "Can't get permission info for " + info.requestedPermissions[i], e); + continue; + } + + if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0 + || (perm.flags & PermissionInfo.FLAG_HIDDEN) != 0) { + continue; + } + + if (perm.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { + PermissionGroupInfo group = getGroup(perm.group, pm); + PreferenceGroup pref = + findOrCreate(group != null ? group : perm, pm, prefs); + pref.addPreference(getPreference(perm, group, pm)); + } else if (perm.protectionLevel == PermissionInfo.PROTECTION_NORMAL) { + PermissionGroupInfo group = getGroup(perm.group, pm); + otherGroup.addPreference(getPreference(perm, group, pm)); + } + } + } + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, "Problem getting package info for " + pkg, e); + } + // Sort an ArrayList of the groups and then set the order from the sorting. + Collections.sort(prefs, new Comparator() { + @Override + public int compare(Preference lhs, Preference rhs) { + String lKey = lhs.getKey(); + String rKey = rhs.getKey(); + if (lKey.equals(KEY_OTHER)) { + return 1; + } else if (rKey.equals(KEY_OTHER)) { + return -1; + } else if (Utils.isModernPermissionGroup(lKey) + != Utils.isModernPermissionGroup(rKey)) { + return Utils.isModernPermissionGroup(lKey) ? -1 : 1; + } + return lhs.getTitle().toString().compareTo(rhs.getTitle().toString()); + } + }); + for (int i = 0; i < prefs.size(); i++) { + prefs.get(i).setOrder(i); + } + } + + private PermissionGroupInfo getGroup(String group, PackageManager pm) { + try { + return pm.getPermissionGroupInfo(group, 0); + } catch (NameNotFoundException e) { + return null; + } + } + + private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm, + ArrayList prefs) { + PreferenceGroup pref = (PreferenceGroup) findPreference(group.name); + if (pref == null) { + pref = new PreferenceCategory(getContext()); + pref.setKey(group.name); + pref.setTitle(group.loadLabel(pm)); + prefs.add(pref); + getPreferenceScreen().addPreference(pref); + } + return pref; + } + + private Preference getPreference(PermissionInfo perm, PermissionGroupInfo group, + PackageManager pm) { + Preference pref = new Preference(getContext()); + Drawable icon = null; + if (perm.icon != 0) { + icon = perm.loadIcon(pm); + } else if (group != null && group.icon != 0) { + icon = group.loadIcon(pm); + } else { + icon = getContext().getDrawable(R.drawable.ic_perm_device_info); + } + pref.setIcon(Utils.applyTint(getContext(), icon, android.R.attr.colorControlNormal)); + pref.setTitle(perm.loadLabel(pm)); + final CharSequence desc = perm.loadDescription(pm); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + new AlertDialog.Builder(getContext()) + .setMessage(desc) + .setPositiveButton(android.R.string.ok, null) + .show(); + return true; + } + }); + + return pref; + } +} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java new file mode 100644 index 00000000..f56cba70 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java @@ -0,0 +1,404 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; +import android.provider.Settings; +import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.AppPermissions; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class AppPermissionsFragment extends SettingsWithHeader + implements OnPreferenceChangeListener { + + private static final String LOG_TAG = "ManagePermsFragment"; + + static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"; + + private static final int MENU_ALL_PERMS = 0; + + private List mToggledGroups; + private AppPermissions mAppPermissions; + private PreferenceScreen mExtraScreen; + + private boolean mHasConfirmedRevoke; + + public static AppPermissionsFragment newInstance(String packageName) { + return setPackageName(new AppPermissionsFragment(), packageName); + } + + private static T setPackageName(T fragment, String packageName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + fragment.setArguments(arguments); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + Activity activity = getActivity(); + PackageInfo packageInfo = getPackageInfo(activity, packageName); + if (packageInfo == null) { + Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); + activity.finish(); + return; + } + + mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); + loadPreferences(); + } + + @Override + public void onResume() { + super.onResume(); + mAppPermissions.refresh(); + setPreferencesCheckedState(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getActivity().finish(); + return true; + } + + case MENU_ALL_PERMS: { + Fragment frag = AllAppPermissionsFragment.newInstance( + getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("AllPerms") + .commit(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (mAppPermissions != null) { + bindUi(this, mAppPermissions.getPackageInfo()); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions); + } + + private static void bindUi(SettingsWithHeader fragment, PackageInfo packageInfo) { + Activity activity = fragment.getActivity(); + PackageManager pm = activity.getPackageManager(); + ApplicationInfo appInfo = packageInfo.applicationInfo; + Intent infoIntent = null; + if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", packageInfo.packageName, null)); + } + + Drawable icon = appInfo.loadIcon(pm); + CharSequence label = appInfo.loadLabel(pm); + fragment.setHeader(icon, label, infoIntent); + + ActionBar ab = activity.getActionBar(); + if (ab != null) { + ab.setTitle(R.string.app_permissions); + } + + ViewGroup rootView = (ViewGroup) fragment.getView(); + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + iconView.setImageDrawable(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(label); + } + } + + private void loadPreferences() { + Context context = getActivity(); + if (context == null) { + return; + } + + PreferenceScreen screen = getPreferenceScreen(); + if (screen == null) { + screen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(screen); + } + + screen.removeAll(); + + if (mExtraScreen != null) { + mExtraScreen.removeAll(); + } + + final Preference extraPerms = new Preference(context); + extraPerms.setIcon(R.drawable.ic_toc); + extraPerms.setTitle(R.string.additional_permissions); + + for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { + if (!Utils.shouldShowPermission(group, mAppPermissions.getPackageInfo().packageName)) { + continue; + } + + boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); + + SwitchPreference preference = new SwitchPreference(context); + preference.setOnPreferenceChangeListener(this); + preference.setKey(group.getName()); + Drawable icon = Utils.loadDrawable(context.getPackageManager(), + group.getIconPkg(), group.getIconResId()); + preference.setIcon(Utils.applyTint(getContext(), icon, + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + if (group.isPolicyFixed()) { + preference.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + preference.setPersistent(false); + preference.setEnabled(!group.isPolicyFixed()); + preference.setChecked(group.areRuntimePermissionsGranted()); + + if (isPlatform) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + + if (mExtraScreen != null) { + extraPerms.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + setPackageName(frag, getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + frag.setTargetFragment(AppPermissionsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack(null) + .commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraPerms.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraPerms); + } + + setLoading(false /* loading */, true /* animate */); + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String groupName = preference.getKey(); + final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName); + + if (group == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(group); + + if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { + LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + group.grantRuntimePermissions(false); + } else { + final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); + if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + group.revokeRuntimePermissions(false); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + group.revokeRuntimePermissions(false); + } + } + + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayList<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.contains(group)) { + mToggledGroups.remove(group); + } else { + mToggledGroups.add(group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + String packageName = mAppPermissions.getPackageInfo().packageName; + SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); + mToggledGroups = null; + } + } + + private void setPreferencesCheckedState() { + setPreferencesCheckedState(getPreferenceScreen()); + if (mExtraScreen != null) { + setPreferencesCheckedState(mExtraScreen); + } + } + + private void setPreferencesCheckedState(PreferenceScreen screen) { + int preferenceCount = screen.getPreferenceCount(); + for (int i = 0; i < preferenceCount; i++) { + Preference preference = screen.getPreference(i); + if (preference instanceof SwitchPreference) { + SwitchPreference switchPref = (SwitchPreference) preference; + AppPermissionGroup group = mAppPermissions.getPermissionGroup(switchPref.getKey()); + if (group != null) { + switchPref.setChecked(group.areRuntimePermissionsGranted()); + } + } + } + } + + private static PackageInfo getPackageInfo(Activity activity, String packageName) { + try { + return activity.getPackageManager().getPackageInfo( + packageName, PackageManager.GET_PERMISSIONS); + } catch (PackageManager.NameNotFoundException e) { + Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); + return null; + } + } + + public static class AdditionalPermissionsFragment extends SettingsWithHeader { + AppPermissionsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (AppPermissionsFragment) getTargetFragment(); + super.onCreate(savedInstanceState); + setHeader(mOuterFragment.mIcon, mOuterFragment.mLabel, mOuterFragment.mInfoIntent); + setHasOptionsMenu(true); + setPreferenceScreen(mOuterFragment.mExtraScreen); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + bindUi(this, getPackageInfo(getActivity(), packageName)); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java b/src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java new file mode 100644 index 00000000..2d27f069 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java @@ -0,0 +1,462 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.animation.ObjectAnimator; +import android.content.Context; +import android.content.res.Resources; +import android.graphics.drawable.Icon; +import android.os.Bundle; +import android.util.SparseArray; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnLayoutChangeListener; +import android.view.ViewGroup; +import android.view.ViewParent; +import android.view.ViewRootImpl; +import android.view.WindowManager.LayoutParams; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; +import android.view.animation.Interpolator; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.ui.ButtonBarLayout; +import com.android.packageinstaller.permission.ui.GrantPermissionsViewHandler; +import com.android.packageinstaller.permission.ui.ManualLayoutFrame; + +import java.util.ArrayList; + +public final class GrantPermissionsViewHandlerImpl + implements GrantPermissionsViewHandler, OnClickListener { + + public static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; + public static final String ARG_GROUP_COUNT = "ARG_GROUP_COUNT"; + public static final String ARG_GROUP_INDEX = "ARG_GROUP_INDEX"; + public static final String ARG_GROUP_ICON = "ARG_GROUP_ICON"; + public static final String ARG_GROUP_MESSAGE = "ARG_GROUP_MESSAGE"; + public static final String ARG_GROUP_SHOW_DO_NOT_ASK = "ARG_GROUP_SHOW_DO_NOT_ASK"; + public static final String ARG_GROUP_DO_NOT_ASK_CHECKED = "ARG_GROUP_DO_NOT_ASK_CHECKED"; + + // Animation parameters. + private static final long SIZE_START_DELAY = 300; + private static final long SIZE_START_LENGTH = 233; + private static final long FADE_OUT_START_DELAY = 300; + private static final long FADE_OUT_START_LENGTH = 217; + private static final long TRANSLATE_START_DELAY = 367; + private static final long TRANSLATE_LENGTH = 317; + private static final long GROUP_UPDATE_DELAY = 400; + private static final long DO_NOT_ASK_CHECK_DELAY = 450; + + private final Context mContext; + + private ResultListener mResultListener; + + private String mGroupName; + private int mGroupCount; + private int mGroupIndex; + private Icon mGroupIcon; + private CharSequence mGroupMessage; + private boolean mShowDonNotAsk; + private boolean mDoNotAskChecked; + + private ImageView mIconView; + private TextView mCurrentGroupView; + private TextView mMessageView; + private CheckBox mDoNotAskCheckbox; + private Button mAllowButton; + + private ArrayList mHeightControllers; + private ManualLayoutFrame mRootView; + + // Needed for animation + private ViewGroup mDescContainer; + private ViewGroup mCurrentDesc; + private ViewGroup mNextDesc; + + private ViewGroup mDialogContainer; + + private final Runnable mUpdateGroup = new Runnable() { + @Override + public void run() { + updateGroup(); + } + }; + + public GrantPermissionsViewHandlerImpl(Context context) { + mContext = context; + } + + @Override + public GrantPermissionsViewHandlerImpl setResultListener(ResultListener listener) { + mResultListener = listener; + return this; + } + + @Override + public void saveInstanceState(Bundle arguments) { + arguments.putString(ARG_GROUP_NAME, mGroupName); + arguments.putInt(ARG_GROUP_COUNT, mGroupCount); + arguments.putInt(ARG_GROUP_INDEX, mGroupIndex); + arguments.putParcelable(ARG_GROUP_ICON, mGroupIcon); + arguments.putCharSequence(ARG_GROUP_MESSAGE, mGroupMessage); + arguments.putBoolean(ARG_GROUP_SHOW_DO_NOT_ASK, mShowDonNotAsk); + arguments.putBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED, mDoNotAskCheckbox.isChecked()); + } + + @Override + public void loadInstanceState(Bundle savedInstanceState) { + mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); + mGroupMessage = savedInstanceState.getCharSequence(ARG_GROUP_MESSAGE); + mGroupIcon = savedInstanceState.getParcelable(ARG_GROUP_ICON); + mGroupCount = savedInstanceState.getInt(ARG_GROUP_COUNT); + mGroupIndex = savedInstanceState.getInt(ARG_GROUP_INDEX); + mShowDonNotAsk = savedInstanceState.getBoolean(ARG_GROUP_SHOW_DO_NOT_ASK); + mDoNotAskChecked = savedInstanceState.getBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED); + } + + @Override + public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, + CharSequence message, boolean showDonNotAsk) { + mGroupName = groupName; + mGroupCount = groupCount; + mGroupIndex = groupIndex; + mGroupIcon = icon; + mGroupMessage = message; + mShowDonNotAsk = showDonNotAsk; + mDoNotAskChecked = false; + // If this is a second (or later) permission and the views exist, then animate. + if (mIconView != null) { + if (mGroupIndex > 0) { + // The first message will be announced as the title of the activity, all others + // we need to announce ourselves. + mDescContainer.announceForAccessibility(message); + animateToPermission(); + } else { + updateDescription(); + updateGroup(); + updateDoNotAskCheckBox(); + } + } + } + + private void animateToPermission() { + if (mHeightControllers == null) { + // We need to manually control the height of any views heigher than the root that + // we inflate. Find all the views up to the root and create ViewHeightControllers for + // them. + mHeightControllers = new ArrayList<>(); + ViewRootImpl viewRoot = mRootView.getViewRootImpl(); + ViewParent v = mRootView.getParent(); + addHeightController(mDialogContainer); + addHeightController(mRootView); + while (v != viewRoot) { + addHeightController((View) v); + v = v.getParent(); + } + // On the heighest level view, we want to setTop rather than setBottom to control the + // height, this way the dialog will grow up rather than down. + ViewHeightController realRootView = + mHeightControllers.get(mHeightControllers.size() - 1); + realRootView.setControlTop(true); + } + + // Grab the current height/y positions, then wait for the layout to change, + // so we can get the end height/y positions. + final SparseArray startPositions = getViewPositions(); + final int startHeight = mRootView.getLayoutHeight(); + mRootView.addOnLayoutChangeListener(new OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, + int oldTop, int oldRight, int oldBottom) { + mRootView.removeOnLayoutChangeListener(this); + SparseArray endPositions = getViewPositions(); + int endHeight = mRootView.getLayoutHeight(); + if (startPositions.get(R.id.do_not_ask_checkbox) == 0 + && endPositions.get(R.id.do_not_ask_checkbox) != 0) { + // If the checkbox didn't have a position before but has one now then set + // the start position to the end position because it just became visible. + startPositions.put(R.id.do_not_ask_checkbox, + endPositions.get(R.id.do_not_ask_checkbox)); + } + animateYPos(startPositions, endPositions, endHeight - startHeight); + } + }); + + // Fade out old description group and scale out the icon for it. + Interpolator interpolator = AnimationUtils.loadInterpolator(mContext, + android.R.interpolator.fast_out_linear_in); + mIconView.animate() + .scaleX(0) + .scaleY(0) + .setStartDelay(FADE_OUT_START_DELAY) + .setDuration(FADE_OUT_START_LENGTH) + .setInterpolator(interpolator) + .start(); + mCurrentDesc.animate() + .alpha(0) + .setStartDelay(FADE_OUT_START_DELAY) + .setDuration(FADE_OUT_START_LENGTH) + .setInterpolator(interpolator) + .setListener(null) + .start(); + + // Update the index of the permission after the animations have started. + mCurrentGroupView.getHandler().postDelayed(mUpdateGroup, GROUP_UPDATE_DELAY); + + // Add the new description and translate it in. + mNextDesc = (ViewGroup) LayoutInflater.from(mContext).inflate( + R.layout.permission_description, mDescContainer, false); + + mMessageView = (TextView) mNextDesc.findViewById(R.id.permission_message); + mIconView = (ImageView) mNextDesc.findViewById(R.id.permission_icon); + updateDescription(); + + int width = mDescContainer.getRootView().getWidth(); + mDescContainer.addView(mNextDesc); + mNextDesc.setTranslationX(width); + + final View oldDesc = mCurrentDesc; + // Remove the old view from the description, so that we can shrink if necessary. + mDescContainer.removeView(oldDesc); + oldDesc.setPadding(mDescContainer.getLeft(), mDescContainer.getTop(), + mRootView.getRight() - mDescContainer.getRight(), 0); + mRootView.addView(oldDesc); + + mCurrentDesc = mNextDesc; + mNextDesc.animate() + .translationX(0) + .setStartDelay(TRANSLATE_START_DELAY) + .setDuration(TRANSLATE_LENGTH) + .setInterpolator(AnimationUtils.loadInterpolator(mContext, + android.R.interpolator.linear_out_slow_in)) + .setListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + // This is the longest animation, when it finishes, we are done. + mRootView.removeView(oldDesc); + } + }) + .start(); + + boolean visibleBefore = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; + updateDoNotAskCheckBox(); + boolean visibleAfter = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; + if (visibleBefore != visibleAfter) { + Animation anim = AnimationUtils.loadAnimation(mContext, + visibleAfter ? android.R.anim.fade_in : android.R.anim.fade_out); + anim.setStartOffset(visibleAfter ? DO_NOT_ASK_CHECK_DELAY : 0); + mDoNotAskCheckbox.startAnimation(anim); + } + } + + private void addHeightController(View v) { + ViewHeightController heightController = new ViewHeightController(v); + heightController.setHeight(v.getHeight()); + mHeightControllers.add(heightController); + } + + private SparseArray getViewPositions() { + SparseArray locMap = new SparseArray<>(); + final int N = mDialogContainer.getChildCount(); + for (int i = 0; i < N; i++) { + View child = mDialogContainer.getChildAt(i); + if (child.getId() <= 0) { + // Only track views with ids. + continue; + } + locMap.put(child.getId(), child.getY()); + } + return locMap; + } + + private void animateYPos(SparseArray startPositions, SparseArray endPositions, + int heightDiff) { + final int N = startPositions.size(); + for (int i = 0; i < N; i++) { + int key = startPositions.keyAt(i); + float start = startPositions.get(key); + float end = endPositions.get(key); + if (start != end) { + final View child = mDialogContainer.findViewById(key); + child.setTranslationY(start - end); + child.animate() + .setStartDelay(SIZE_START_DELAY) + .setDuration(SIZE_START_LENGTH) + .translationY(0) + .start(); + } + } + for (int i = 0; i < mHeightControllers.size(); i++) { + mHeightControllers.get(i).animateAddHeight(heightDiff); + } + } + + @Override + public View createView() { + mRootView = (ManualLayoutFrame) LayoutInflater.from(mContext) + .inflate(R.layout.grant_permissions, null); + ((ButtonBarLayout) mRootView.findViewById(R.id.button_group)).setAllowStacking(true); + + mDialogContainer = (ViewGroup) mRootView.findViewById(R.id.dialog_container); + mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); + mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); + mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); + mDoNotAskCheckbox = (CheckBox) mRootView.findViewById(R.id.do_not_ask_checkbox); + mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); + + mDescContainer = (ViewGroup) mRootView.findViewById(R.id.desc_container); + mCurrentDesc = (ViewGroup) mRootView.findViewById(R.id.perm_desc_root); + + mAllowButton.setOnClickListener(this); + mRootView.findViewById(R.id.permission_deny_button).setOnClickListener(this); + mDoNotAskCheckbox.setOnClickListener(this); + + if (mGroupName != null) { + updateDescription(); + updateGroup(); + updateDoNotAskCheckBox(); + } + + return mRootView; + } + + @Override + public void updateWindowAttributes(LayoutParams outLayoutParams) { + // No-op + } + + private void updateDescription() { + mIconView.setImageDrawable(mGroupIcon.loadDrawable(mContext)); + mMessageView.setText(mGroupMessage); + } + + private void updateGroup() { + if (mGroupCount > 1) { + mCurrentGroupView.setVisibility(View.VISIBLE); + mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, + mGroupIndex + 1, mGroupCount)); + } else { + mCurrentGroupView.setVisibility(View.INVISIBLE); + } + } + + private void updateDoNotAskCheckBox() { + if (mShowDonNotAsk) { + mDoNotAskCheckbox.setVisibility(View.VISIBLE); + mDoNotAskCheckbox.setOnClickListener(this); + mDoNotAskCheckbox.setChecked(mDoNotAskChecked); + } else { + mDoNotAskCheckbox.setVisibility(View.GONE); + mDoNotAskCheckbox.setOnClickListener(null); + } + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.permission_allow_button: + if (mResultListener != null) { + view.clearAccessibilityFocus(); + mResultListener.onPermissionGrantResult(mGroupName, true, false); + } + break; + case R.id.permission_deny_button: + mAllowButton.setEnabled(true); + if (mResultListener != null) { + view.clearAccessibilityFocus(); + mResultListener.onPermissionGrantResult(mGroupName, false, + mDoNotAskCheckbox.isChecked()); + } + break; + case R.id.do_not_ask_checkbox: + mAllowButton.setEnabled(!mDoNotAskCheckbox.isChecked()); + break; + } + } + + @Override + public void onBackPressed() { + if (mResultListener != null) { + final boolean doNotAskAgain = mDoNotAskCheckbox.isChecked(); + mResultListener.onPermissionGrantResult(mGroupName, false, doNotAskAgain); + } + } + + /** + * Manually controls the height of a view through getBottom/setTop. Also listens + * for layout changes and sets the height again to be sure it doesn't change. + */ + private static final class ViewHeightController implements OnLayoutChangeListener { + private final View mView; + private int mHeight; + private int mNextHeight; + private boolean mControlTop; + private ObjectAnimator mAnimator; + + public ViewHeightController(View view) { + mView = view; + mView.addOnLayoutChangeListener(this); + } + + public void setControlTop(boolean controlTop) { + mControlTop = controlTop; + } + + public void animateAddHeight(int heightDiff) { + if (heightDiff != 0) { + if (mNextHeight == 0) { + mNextHeight = mHeight; + } + mNextHeight += heightDiff; + if (mAnimator != null) { + mAnimator.cancel(); + } + mAnimator = ObjectAnimator.ofInt(this, "height", mHeight, mNextHeight); + mAnimator.setStartDelay(SIZE_START_DELAY); + mAnimator.setDuration(SIZE_START_LENGTH); + mAnimator.start(); + } + } + + public void setHeight(int height) { + mHeight = height; + updateHeight(); + } + + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, + int oldTop, int oldRight, int oldBottom) { + // Ensure that the height never changes. + updateHeight(); + } + + private void updateHeight() { + if (mControlTop) { + mView.setTop(mView.getBottom() - mHeight); + } else { + mView.setBottom(mView.getTop() + mHeight); + } + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java new file mode 100644 index 00000000..c53da879 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java @@ -0,0 +1,268 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.util.ArraySet; +import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.PmCache; +import com.android.packageinstaller.permission.model.PermissionGroup; +import com.android.packageinstaller.permission.model.PermissionGroups; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.List; + +public final class ManagePermissionsFragment extends PermissionsFrameFragment + implements PermissionGroups.PermissionsGroupsChangeCallback, + Preference.OnPreferenceClickListener { + private static final String LOG_TAG = "ManagePermissionsFragment"; + + private static final String OS_PKG = "android"; + + private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; + + private ArraySet mLauncherPkgs; + + private PermissionGroups mPermissions; + + private PreferenceScreen mExtraScreen; + + public static ManagePermissionsFragment newInstance() { + return new ManagePermissionsFragment(); + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + mPermissions = new PermissionGroups(getActivity(), getLoaderManager(), this); + } + + @Override + public void onResume() { + super.onResume(); + mPermissions.refresh(); + updatePermissionsUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + getActivity().finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + String key = preference.getKey(); + + PermissionGroup group = mPermissions.getGroup(key); + if (group == null) { + return false; + } + + Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) + .putExtra(Intent.EXTRA_PERMISSION_NAME, key); + try { + getActivity().startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.w(LOG_TAG, "No app to handle " + intent); + } + + return true; + } + + @Override + public void onPermissionGroupsChanged() { + updatePermissionsUi(); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + + private static void bindPermissionUi(@Nullable Context context, @Nullable View rootView) { + if (context == null || rootView == null) { + return; + } + + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + Drawable icon = context.getDrawable(R.drawable.ic_lock); + icon.setTint(context.getColor(R.color.off_white)); + iconView.setBackground(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions_breadcrumb); + } + } + + private void updatePermissionsUi() { + Context context = getActivity(); + if (context == null) { + return; + } + + List groups = mPermissions.getGroups(); + PreferenceScreen screen = getPreferenceScreen(); + if (screen == null) { + screen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(screen); + } + + // Use this to speed up getting the info for all of the PermissionApps below. + // Create a new one for each refresh to make sure it has fresh data. + PmCache cache = new PmCache(getContext().getPackageManager()); + for (PermissionGroup group : groups) { + boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); + + Preference preference = findPreference(group.getName()); + if (preference == null && mExtraScreen != null) { + preference = mExtraScreen.findPreference(group.getName()); + } + if (preference == null) { + preference = new Preference(context); + preference.setOnPreferenceClickListener(this); + preference.setKey(group.getName()); + preference.setIcon(Utils.applyTint(context, group.getIcon(), + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + // Set blank summary so that no resizing/jumping happens when the summary is loaded. + preference.setSummary(" "); + preference.setPersistent(false); + if (isSystemPermission) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + final Preference finalPref = preference; + + new PermissionApps(getContext(), group.getName(), new PermissionApps.Callback() { + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + if (getActivity() == null) { + return; + } + int granted = permissionApps.getGrantedCount(mLauncherPkgs); + int total = permissionApps.getTotalCount(mLauncherPkgs); + finalPref.setSummary(getString(R.string.app_permissions_group_summary, + granted, total)); + } + }, cache).refresh(false); + } + + if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 + && screen.findPreference(EXTRA_PREFS_KEY) == null) { + Preference extraScreenPreference = new Preference(context); + extraScreenPreference.setKey(EXTRA_PREFS_KEY); + extraScreenPreference.setIcon(Utils.applyTint(context, + R.drawable.ic_more_items, + android.R.attr.colorControlNormal)); + extraScreenPreference.setTitle(R.string.additional_permissions); + extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + frag.setTargetFragment(ManagePermissionsFragment.this, 0); + FragmentTransaction ft = getFragmentManager().beginTransaction(); + ft.replace(android.R.id.content, frag); + ft.addToBackStack(null); + ft.commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraScreenPreference.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraScreenPreference); + } + if (screen.getPreferenceCount() != 0) { + setLoading(false /* loading */, true /* animate */); + } + } + + public static class AdditionalPermissionsFragment extends PermissionsFrameFragment { + @Override + public void onCreate(Bundle icicle) { + setLoading(true /* loading */, false /* animate */); + super.onCreate(icicle); + getActivity().setTitle(R.string.additional_permissions); + setHasOptionsMenu(true); + + setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + + @Override + public void onDestroy() { + getActivity().setTitle(R.string.app_permissions); + super.onDestroy(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java new file mode 100644 index 00000000..554830a7 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java @@ -0,0 +1,427 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; +import android.util.ArrayMap; +import android.util.ArraySet; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.Callback; +import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class PermissionAppsFragment extends PermissionsFrameFragment implements Callback, + Preference.OnPreferenceChangeListener { + + private static final int MENU_SHOW_SYSTEM = Menu.FIRST; + private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 1; + private static final String KEY_SHOW_SYSTEM_PREFS = "_showSystem"; + + public static PermissionAppsFragment newInstance(String permissionName) { + return setPermissionName(new PermissionAppsFragment(), permissionName); + } + + private static T setPermissionName(T fragment, String permissionName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PERMISSION_NAME, permissionName); + fragment.setArguments(arguments); + return fragment; + } + + private PermissionApps mPermissionApps; + + private PreferenceScreen mExtraScreen; + + private ArrayMap mToggledGroups; + private ArraySet mLauncherPkgs; + private boolean mHasConfirmedRevoke; + + private boolean mShowSystem; + private MenuItem mShowSystemMenu; + private MenuItem mHideSystemMenu; + + private Callback mOnPermissionsLoadedListener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + mPermissionApps = new PermissionApps(getActivity(), groupName, this); + mPermissionApps.refresh(true); + } + + @Override + public void onResume() { + super.onResume(); + mPermissionApps.refresh(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, + R.string.menu_show_system); + mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE, + R.string.menu_hide_system); + updateMenu(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getActivity().finish(); + return true; + case MENU_SHOW_SYSTEM: + case MENU_HIDE_SYSTEM: + mShowSystem = item.getItemId() == MENU_SHOW_SYSTEM; + if (mPermissionApps.getApps() != null) { + onPermissionsLoaded(mPermissionApps); + } + updateMenu(); + break; + } + return super.onOptionsItemSelected(item); + } + + private void updateMenu() { + mShowSystemMenu.setVisible(!mShowSystem); + mHideSystemMenu.setVisible(mShowSystem); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindUi(this, mPermissionApps); + } + + private static void bindUi(Fragment fragment, PermissionApps permissionApps) { + final Drawable icon = permissionApps.getIcon(); + final CharSequence label = permissionApps.getLabel(); + final ActionBar ab = fragment.getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(fragment.getString(R.string.permission_title, label)); + } + + final ViewGroup rootView = (ViewGroup) fragment.getView(); + final ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + iconView.setBackground(icon); + } + final TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(label); + } + final TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions); + } + } + + private void setOnPermissionsLoadedListener(Callback callback) { + mOnPermissionsLoadedListener = callback; + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + Context context = getActivity(); + + if (context == null) { + return; + } + + boolean isTelevision = Utils.isTelevision(context); + PreferenceScreen screen = getPreferenceScreen(); + if (screen == null) { + screen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(screen); + } + + ArraySet preferencesToRemove = new ArraySet<>(); + for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(screen.getPreference(i).getKey()); + } + if (mExtraScreen != null) { + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(mExtraScreen.getPreference(i).getKey()); + } + } + + for (PermissionApp app : permissionApps.getApps()) { + if (!Utils.shouldShowPermission(app)) { + continue; + } + + String key = app.getKey(); + preferencesToRemove.remove(key); + Preference existingPref = screen.findPreference(key); + if (existingPref == null && mExtraScreen != null) { + existingPref = mExtraScreen.findPreference(key); + } + + boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs); + if (isSystemApp && !isTelevision && !mShowSystem) { + if (existingPref != null) { + screen.removePreference(existingPref); + } + continue; + } + + if (existingPref != null) { + // If existing preference - only update its state. + if (app.isPolicyFixed()) { + existingPref.setSummary(getString( + R.string.permission_summary_enforced_by_policy)); + } + existingPref.setPersistent(false); + existingPref.setEnabled(!app.isPolicyFixed()); + if (existingPref instanceof SwitchPreference) { + ((SwitchPreference) existingPref) + .setChecked(app.areRuntimePermissionsGranted()); + } + continue; + } + + SwitchPreference pref = new SwitchPreference(context); + pref.setOnPreferenceChangeListener(this); + pref.setKey(app.getKey()); + pref.setIcon(app.getIcon()); + pref.setTitle(app.getLabel()); + if (app.isPolicyFixed()) { + pref.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + pref.setPersistent(false); + pref.setEnabled(!app.isPolicyFixed()); + pref.setChecked(app.areRuntimePermissionsGranted()); + + if (isSystemApp && isTelevision) { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(pref); + } else { + screen.addPreference(pref); + } + } + + if (mExtraScreen != null) { + preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS); + Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS); + + if (pref == null) { + pref = new Preference(context); + pref.setKey(KEY_SHOW_SYSTEM_PREFS); + pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, + android.R.attr.colorControlNormal)); + pref.setTitle(R.string.preference_show_system_apps); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + SystemAppsFragment frag = new SystemAppsFragment(); + setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME)); + frag.setTargetFragment(PermissionAppsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("SystemApps") + .commit(); + return true; + } + }); + screen.addPreference(pref); + } + + int grantedCount = 0; + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) { + grantedCount++; + } + } + pref.setSummary(getString(R.string.app_permissions_group_summary, + grantedCount, mExtraScreen.getPreferenceCount())); + } + + for (String key : preferencesToRemove) { + Preference pref = screen.findPreference(key); + if (pref != null) { + screen.removePreference(pref); + } else if (mExtraScreen != null) { + pref = mExtraScreen.findPreference(key); + if (pref != null) { + mExtraScreen.removePreference(pref); + } + } + } + + setLoading(false /* loading */, true /* animate */); + + if (mOnPermissionsLoadedListener != null) { + mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps); + } + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String pkg = preference.getKey(); + final PermissionApp app = mPermissionApps.getApp(pkg); + + if (app == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(app.getPackageName(), app.getPermissionGroup()); + + if (LocationUtils.isLocationGroupAndProvider(mPermissionApps.getGroupName(), + app.getPackageName())) { + LocationUtils.showLocationDialog(getContext(), app.getLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + app.grantRuntimePermissions(); + } else { + final boolean grantedByDefault = app.hasGrantedByDefaultPermissions(); + if (grantedByDefault || (!app.hasRuntimePermissions() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + app.revokeRuntimePermissions(); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + app.revokeRuntimePermissions(); + } + } + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(String packageName, AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayMap<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.containsKey(packageName)) { + mToggledGroups.remove(packageName); + } else { + mToggledGroups.put(packageName, group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + final int groupCount = mToggledGroups.size(); + for (int i = 0; i < groupCount; i++) { + String packageName = mToggledGroups.keyAt(i); + List groups = new ArrayList<>(); + groups.add(mToggledGroups.valueAt(i)); + SafetyNetLogger.logPermissionsToggled(packageName, groups); + } + mToggledGroups = null; + } + } + + public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback { + PermissionAppsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (PermissionAppsFragment) getTargetFragment(); + setLoading(true /* loading */, false /* animate */); + super.onCreate(savedInstanceState); + if (mOuterFragment.mExtraScreen != null) { + setPreferenceScreen(); + } else { + mOuterFragment.setOnPermissionsLoadedListener(this); + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + PermissionApps permissionApps = new PermissionApps(getActivity(), groupName, null); + bindUi(this, permissionApps); + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + setPreferenceScreen(); + mOuterFragment.setOnPermissionsLoadedListener(null); + } + + private void setPreferenceScreen() { + setPreferenceScreen(mOuterFragment.mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java new file mode 100644 index 00000000..e7f63b23 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.os.Bundle; +import android.preference.PreferenceFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.Animation.AnimationListener; +import android.view.animation.AnimationUtils; +import android.widget.ListView; +import android.widget.TextView; +import com.android.packageinstaller.R; + +public abstract class PermissionsFrameFragment extends PreferenceFragment { + private ViewGroup mPreferencesContainer; + + private View mLoadingView; + private ViewGroup mPrefsView; + private boolean mIsLoading; + + /** + * Returns the view group that holds the preferences objects. This will + * only be set after {@link #onCreateView} has been called. + */ + protected final ViewGroup getPreferencesContainer() { + return mPreferencesContainer; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container, + false); + mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container); + if (mPrefsView == null) { + mPrefsView = rootView; + } + mLoadingView = rootView.findViewById(R.id.loading_container); + mPreferencesContainer = (ViewGroup) super.onCreateView( + inflater, mPrefsView, savedInstanceState); + setLoading(mIsLoading, false, true /* force */); + mPrefsView.addView(mPreferencesContainer); + return rootView; + } + + protected void setLoading(boolean loading, boolean animate) { + setLoading(loading, animate, false); + } + + private void setLoading(boolean loading, boolean animate, boolean force) { + if (mIsLoading != loading || force) { + mIsLoading = loading; + if (getView() == null) { + // If there is no created view, there is no reason to animate. + animate = false; + } + if (mPrefsView != null) { + setViewShown(mPrefsView, !loading, animate); + } + if (mLoadingView != null) { + setViewShown(mLoadingView, loading, animate); + } + } + } + + @Override + public ListView getListView() { + ListView listView = super.getListView(); + if (listView.getEmptyView() == null) { + TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions); + listView.setEmptyView(emptyView); + } + return listView; + } + + private void setViewShown(final View view, boolean shown, boolean animate) { + if (animate) { + Animation animation = AnimationUtils.loadAnimation(getContext(), + shown ? android.R.anim.fade_in : android.R.anim.fade_out); + if (shown) { + view.setVisibility(View.VISIBLE); + } else { + animation.setAnimationListener(new AnimationListener() { + @Override + public void onAnimationStart(Animation animation) { + } + + @Override + public void onAnimationRepeat(Animation animation) { + } + + @Override + public void onAnimationEnd(Animation animation) { + view.setVisibility(View.INVISIBLE); + } + }); + } + view.startAnimation(animation); + } else { + view.clearAnimation(); + view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java new file mode 100644 index 00000000..acb3c61e --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +public abstract class SettingsWithHeader extends PermissionsFrameFragment + implements OnClickListener { + + private View mHeader; + protected Intent mInfoIntent; + protected Drawable mIcon; + protected CharSequence mLabel; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); + + if (!Utils.isTelevision(getContext())) { + mHeader = inflater.inflate(R.layout.header, root, false); + getPreferencesContainer().addView(mHeader, 0); + updateHeader(); + } + + return root; + } + + public void setHeader(Drawable icon, CharSequence label, Intent infoIntent) { + mIcon = icon; + mLabel = label; + mInfoIntent = infoIntent; + updateHeader(); + } + + private void updateHeader() { + if (mHeader != null) { + final ImageView appIcon = (ImageView) mHeader.findViewById(R.id.icon); + appIcon.setImageDrawable(mIcon); + + final TextView appName = (TextView) mHeader.findViewById(R.id.name); + appName.setText(mLabel); + + final View info = mHeader.findViewById(R.id.info); + if (mInfoIntent == null) { + info.setVisibility(View.GONE); + } else { + info.setVisibility(View.VISIBLE); + info.setClickable(true); + info.setOnClickListener(this); + } + } + } + + @Override + public void onClick(View v) { + getActivity().startActivity(mInfoIntent); + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java new file mode 100644 index 00000000..d4910128 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java @@ -0,0 +1,217 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageItemInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionGroupInfo; +import android.content.pm.PermissionInfo; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceCategory; +import android.support.v7.preference.PreferenceGroup; +import android.util.Log; +import android.view.MenuItem; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public final class AllAppPermissionsFragment extends SettingsWithHeader { + + private static final String LOG_TAG = "AllAppPermissionsFragment"; + + private static final String KEY_OTHER = "other_perms"; + + public static AllAppPermissionsFragment newInstance(String packageName) { + AllAppPermissionsFragment instance = new AllAppPermissionsFragment(); + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + instance.setArguments(arguments); + return instance; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(R.string.all_permissions); + ab.setDisplayHomeAsUpEnabled(true); + } + } + + @Override + public void onResume() { + super.onResume(); + updateUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getFragmentManager().popBackStack(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + private void updateUi() { + if (getPreferenceScreen() != null) { + getPreferenceScreen().removeAll(); + } + addPreferencesFromResource(R.xml.all_permissions); + PreferenceGroup otherGroup = (PreferenceGroup) findPreference(KEY_OTHER); + ArrayList prefs = new ArrayList<>(); // Used for sorting. + prefs.add(otherGroup); + String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + otherGroup.removeAll(); + PackageManager pm = getContext().getPackageManager(); + + try { + PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS); + + ApplicationInfo appInfo = info.applicationInfo; + final Drawable icon = appInfo.loadIcon(pm); + final CharSequence label = appInfo.loadLabel(pm); + Intent infoIntent = null; + if (!getActivity().getIntent().getBooleanExtra( + AppPermissionsFragment.EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", pkg, null)); + } + setHeader(icon, label, infoIntent); + + if (info.requestedPermissions != null) { + for (int i = 0; i < info.requestedPermissions.length; i++) { + PermissionInfo perm; + try { + perm = pm.getPermissionInfo(info.requestedPermissions[i], 0); + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, + "Can't get permission info for " + info.requestedPermissions[i], e); + continue; + } + + if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0 + || (perm.flags & PermissionInfo.FLAG_HIDDEN) != 0) { + continue; + } + + if (perm.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { + PermissionGroupInfo group = getGroup(perm.group, pm); + PreferenceGroup pref = + findOrCreate(group != null ? group : perm, pm, prefs); + pref.addPreference(getPreference(perm, group, pm)); + } else if (perm.protectionLevel == PermissionInfo.PROTECTION_NORMAL) { + PermissionGroupInfo group = getGroup(perm.group, pm); + otherGroup.addPreference(getPreference(perm, group, pm)); + } + } + } + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, "Problem getting package info for " + pkg, e); + } + // Sort an ArrayList of the groups and then set the order from the sorting. + Collections.sort(prefs, new Comparator() { + @Override + public int compare(Preference lhs, Preference rhs) { + String lKey = lhs.getKey(); + String rKey = rhs.getKey(); + if (lKey.equals(KEY_OTHER)) { + return 1; + } else if (rKey.equals(KEY_OTHER)) { + return -1; + } else if (Utils.isModernPermissionGroup(lKey) + != Utils.isModernPermissionGroup(rKey)) { + return Utils.isModernPermissionGroup(lKey) ? -1 : 1; + } + return lhs.getTitle().toString().compareTo(rhs.getTitle().toString()); + } + }); + for (int i = 0; i < prefs.size(); i++) { + prefs.get(i).setOrder(i); + } + } + + private PermissionGroupInfo getGroup(String group, PackageManager pm) { + try { + return pm.getPermissionGroupInfo(group, 0); + } catch (NameNotFoundException e) { + return null; + } + } + + private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm, + ArrayList prefs) { + PreferenceGroup pref = (PreferenceGroup) findPreference(group.name); + if (pref == null) { + pref = new PreferenceCategory(getContext()); + pref.setKey(group.name); + pref.setLayoutResource(R.layout.preference_category_material); + pref.setTitle(group.loadLabel(pm)); + prefs.add(pref); + getPreferenceScreen().addPreference(pref); + } + return pref; + } + + private Preference getPreference(PermissionInfo perm, PermissionGroupInfo group, + PackageManager pm) { + Preference pref = new Preference(getContext()); + pref.setLayoutResource(R.layout.preference_permissions); + Drawable icon = null; + if (perm.icon != 0) { + icon = perm.loadIcon(pm); + } else if (group != null && group.icon != 0) { + icon = group.loadIcon(pm); + } else { + icon = getContext().getDrawable(R.drawable.ic_perm_device_info); + } + pref.setIcon(Utils.applyTint(getContext(), icon, android.R.attr.colorControlNormal)); + pref.setTitle(perm.loadLabel(pm)); + final CharSequence desc = perm.loadDescription(pm); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + new AlertDialog.Builder(getContext()) + .setMessage(desc) + .setPositiveButton(android.R.string.ok, null) + .show(); + return true; + } + }); + + return pref; + } +} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java new file mode 100644 index 00000000..42a2661c --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java @@ -0,0 +1,404 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.support.v14.preference.SwitchPreference; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceChangeListener; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceScreen; +import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.AppPermissions; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class AppPermissionsFragment extends SettingsWithHeader + implements OnPreferenceChangeListener { + + private static final String LOG_TAG = "ManagePermsFragment"; + + static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"; + + private static final int MENU_ALL_PERMS = 0; + + private List mToggledGroups; + private AppPermissions mAppPermissions; + private PreferenceScreen mExtraScreen; + + private boolean mHasConfirmedRevoke; + + public static AppPermissionsFragment newInstance(String packageName) { + return setPackageName(new AppPermissionsFragment(), packageName); + } + + private static T setPackageName(T fragment, String packageName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + fragment.setArguments(arguments); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + Activity activity = getActivity(); + PackageInfo packageInfo = getPackageInfo(activity, packageName); + if (packageInfo == null) { + Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); + activity.finish(); + return; + } + + mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); + loadPreferences(); + } + + @Override + public void onResume() { + super.onResume(); + mAppPermissions.refresh(); + setPreferencesCheckedState(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getActivity().finish(); + return true; + } + + case MENU_ALL_PERMS: { + Fragment frag = AllAppPermissionsFragment.newInstance( + getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("AllPerms") + .commit(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (mAppPermissions != null) { + bindUi(this, mAppPermissions.getPackageInfo()); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions); + } + + private static void bindUi(SettingsWithHeader fragment, PackageInfo packageInfo) { + Activity activity = fragment.getActivity(); + PackageManager pm = activity.getPackageManager(); + ApplicationInfo appInfo = packageInfo.applicationInfo; + Intent infoIntent = null; + if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", packageInfo.packageName, null)); + } + + Drawable icon = appInfo.loadIcon(pm); + CharSequence label = appInfo.loadLabel(pm); + fragment.setHeader(icon, label, infoIntent); + + ActionBar ab = activity.getActionBar(); + if (ab != null) { + ab.setTitle(R.string.app_permissions); + } + + ViewGroup rootView = (ViewGroup) fragment.getView(); + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + iconView.setImageDrawable(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(label); + } + } + + private void loadPreferences() { + Context context = getPreferenceManager().getContext(); + if (context == null) { + return; + } + + PreferenceScreen screen = getPreferenceScreen(); + screen.removeAll(); + + if (mExtraScreen != null) { + mExtraScreen.removeAll(); + } + + final Preference extraPerms = new Preference(context); + extraPerms.setIcon(R.drawable.ic_toc); + extraPerms.setTitle(R.string.additional_permissions); + + for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { + if (!Utils.shouldShowPermission(group, mAppPermissions.getPackageInfo().packageName)) { + continue; + } + + boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); + + SwitchPreference preference = new SwitchPreference(context); + preference.setOnPreferenceChangeListener(this); + preference.setKey(group.getName()); + Drawable icon = Utils.loadDrawable(context.getPackageManager(), + group.getIconPkg(), group.getIconResId()); + preference.setIcon(Utils.applyTint(getContext(), icon, + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + if (group.isPolicyFixed()) { + preference.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + preference.setPersistent(false); + preference.setEnabled(!group.isPolicyFixed()); + preference.setChecked(group.areRuntimePermissionsGranted()); + + if (isPlatform) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + + if (mExtraScreen != null) { + extraPerms.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + setPackageName(frag, getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + frag.setTargetFragment(AppPermissionsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack(null) + .commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraPerms.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraPerms); + } + + setLoading(false /* loading */, true /* animate */); + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String groupName = preference.getKey(); + final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName); + + if (group == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(group); + + if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { + LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + group.grantRuntimePermissions(false); + } else { + final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); + if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + group.revokeRuntimePermissions(false); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + group.revokeRuntimePermissions(false); + } + } + + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayList<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.contains(group)) { + mToggledGroups.remove(group); + } else { + mToggledGroups.add(group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + String packageName = mAppPermissions.getPackageInfo().packageName; + SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); + mToggledGroups = null; + } + } + + private void setPreferencesCheckedState() { + setPreferencesCheckedState(getPreferenceScreen()); + if (mExtraScreen != null) { + setPreferencesCheckedState(mExtraScreen); + } + } + + private void setPreferencesCheckedState(PreferenceScreen screen) { + int preferenceCount = screen.getPreferenceCount(); + for (int i = 0; i < preferenceCount; i++) { + Preference preference = screen.getPreference(i); + if (preference instanceof SwitchPreference) { + SwitchPreference switchPref = (SwitchPreference) preference; + AppPermissionGroup group = mAppPermissions.getPermissionGroup(switchPref.getKey()); + if (group != null) { + switchPref.setChecked(group.areRuntimePermissionsGranted()); + } + } + } + } + + private static PackageInfo getPackageInfo(Activity activity, String packageName) { + try { + return activity.getPackageManager().getPackageInfo( + packageName, PackageManager.GET_PERMISSIONS); + } catch (PackageManager.NameNotFoundException e) { + Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); + return null; + } + } + + public static class AdditionalPermissionsFragment extends SettingsWithHeader { + AppPermissionsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (AppPermissionsFragment) getTargetFragment(); + super.onCreate(savedInstanceState); + setHeader(mOuterFragment.mIcon, mOuterFragment.mLabel, mOuterFragment.mInfoIntent); + setHasOptionsMenu(true); + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferenceScreen(mOuterFragment.mExtraScreen); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + bindUi(this, getPackageInfo(getActivity(), packageName)); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java b/src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java new file mode 100644 index 00000000..a2538821 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java @@ -0,0 +1,131 @@ +package com.android.packageinstaller.permission.ui.television; + +import android.content.Context; +import android.graphics.PixelFormat; +import android.graphics.drawable.Icon; +import android.os.Bundle; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.ui.GrantPermissionsViewHandler; + +/** + * TV-specific view handler for the grant permissions activity. + */ +public final class GrantPermissionsViewHandlerImpl implements GrantPermissionsViewHandler, OnClickListener { + + private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; + + private final Context mContext; + + private ResultListener mResultListener; + + private String mGroupName; + + private LinearLayout mRootView; + private TextView mMessageView; + private ImageView mIconView; + private TextView mCurrentGroupView; + private Button mAllowButton; + private Button mSoftDenyButton; + private Button mHardDenyButton; + + public GrantPermissionsViewHandlerImpl(Context context) { + mContext = context; + } + + @Override + public GrantPermissionsViewHandlerImpl setResultListener(ResultListener listener) { + mResultListener = listener; + return this; + } + + @Override + public View createView() { + mRootView = (LinearLayout) LayoutInflater.from(mContext) + .inflate(R.layout.grant_permissions, null); + + mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); + mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); + mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); + mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); + mSoftDenyButton = (Button) mRootView.findViewById(R.id.permission_deny_button); + mHardDenyButton = (Button) mRootView.findViewById( + R.id.permission_deny_dont_ask_again_button); + + mAllowButton.setOnClickListener(this); + mSoftDenyButton.setOnClickListener(this); + mHardDenyButton.setOnClickListener(this); + + return mRootView; + } + + @Override + public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { + outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; + outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; + outLayoutParams.format = PixelFormat.OPAQUE; + outLayoutParams.gravity = Gravity.BOTTOM; + outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; + outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + } + + @Override + public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, + CharSequence message, boolean showDoNotAsk) { + mGroupName = groupName; + + mMessageView.setText(message); + mIconView.setImageIcon(icon); + mHardDenyButton.setVisibility(showDoNotAsk ? View.VISIBLE : View.GONE); + if (groupCount > 1) { + mCurrentGroupView.setVisibility(View.VISIBLE); + mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, + groupIndex + 1, groupCount)); + } else { + mCurrentGroupView.setVisibility(View.INVISIBLE); + } + } + + @Override + public void saveInstanceState(Bundle outState) { + outState.putString(ARG_GROUP_NAME, mGroupName); + } + + @Override + public void loadInstanceState(Bundle savedInstanceState) { + mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); + } + + @Override + public void onClick(View view) { + boolean granted = false; + boolean doNotAskAgain = false; + switch (view.getId()) { + case R.id.permission_allow_button: + granted = true; + break; + case R.id.permission_deny_dont_ask_again_button: + doNotAskAgain = true; + break; + } + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); + } + } + + @Override + public void onBackPressed() { + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, false, false); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java new file mode 100644 index 00000000..47301f48 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java @@ -0,0 +1,267 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceScreen; +import android.util.ArraySet; +import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.PmCache; +import com.android.packageinstaller.permission.model.PermissionGroup; +import com.android.packageinstaller.permission.model.PermissionGroups; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.List; + +public final class ManagePermissionsFragment extends PermissionsFrameFragment + implements PermissionGroups.PermissionsGroupsChangeCallback, OnPreferenceClickListener { + private static final String LOG_TAG = "ManagePermissionsFragment"; + + private static final String OS_PKG = "android"; + + private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; + + private ArraySet mLauncherPkgs; + + private PermissionGroups mPermissions; + + private PreferenceScreen mExtraScreen; + + public static ManagePermissionsFragment newInstance() { + return new ManagePermissionsFragment(); + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + mPermissions = new PermissionGroups(getActivity(), getLoaderManager(), this); + } + + @Override + public void onResume() { + super.onResume(); + mPermissions.refresh(); + updatePermissionsUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + getActivity().finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + String key = preference.getKey(); + + PermissionGroup group = mPermissions.getGroup(key); + if (group == null) { + return false; + } + + Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) + .putExtra(Intent.EXTRA_PERMISSION_NAME, key); + try { + getActivity().startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.w(LOG_TAG, "No app to handle " + intent); + } + + return true; + } + + @Override + public void onPermissionGroupsChanged() { + updatePermissionsUi(); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + + private static void bindPermissionUi(@Nullable Context context, @Nullable View rootView) { + if (context == null || rootView == null) { + return; + } + + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + Drawable icon = context.getDrawable(R.drawable.ic_lock); + icon.setTint(context.getColor(R.color.off_white)); + iconView.setBackground(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions_breadcrumb); + } + } + + private void updatePermissionsUi() { + Context context = getPreferenceManager().getContext(); + if (context == null) { + return; + } + + List groups = mPermissions.getGroups(); + PreferenceScreen screen = getPreferenceScreen(); + + // Use this to speed up getting the info for all of the PermissionApps below. + // Create a new one for each refresh to make sure it has fresh data. + PmCache cache = new PmCache(getContext().getPackageManager()); + for (PermissionGroup group : groups) { + boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); + + Preference preference = findPreference(group.getName()); + if (preference == null && mExtraScreen != null) { + preference = mExtraScreen.findPreference(group.getName()); + } + if (preference == null) { + preference = new Preference(context); + preference.setOnPreferenceClickListener(this); + preference.setKey(group.getName()); + preference.setIcon(Utils.applyTint(context, group.getIcon(), + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + // Set blank summary so that no resizing/jumping happens when the summary is loaded. + preference.setSummary(" "); + preference.setPersistent(false); + if (isSystemPermission) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + final Preference finalPref = preference; + + new PermissionApps(getContext(), group.getName(), new PermissionApps.Callback() { + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + if (getActivity() == null) { + return; + } + int granted = permissionApps.getGrantedCount(mLauncherPkgs); + int total = permissionApps.getTotalCount(mLauncherPkgs); + finalPref.setSummary(getString(R.string.app_permissions_group_summary, + granted, total)); + } + }, cache).refresh(false); + } + + if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 + && screen.findPreference(EXTRA_PREFS_KEY) == null) { + Preference extraScreenPreference = new Preference(context); + extraScreenPreference.setKey(EXTRA_PREFS_KEY); + extraScreenPreference.setIcon(Utils.applyTint(context, + R.drawable.ic_more_items, + android.R.attr.colorControlNormal)); + extraScreenPreference.setTitle(R.string.additional_permissions); + extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + frag.setTargetFragment(ManagePermissionsFragment.this, 0); + FragmentTransaction ft = getFragmentManager().beginTransaction(); + ft.replace(android.R.id.content, frag); + ft.addToBackStack(null); + ft.commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraScreenPreference.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraScreenPreference); + } + if (screen.getPreferenceCount() != 0) { + setLoading(false /* loading */, true /* animate */); + } + } + + public static class AdditionalPermissionsFragment extends PermissionsFrameFragment { + @Override + public void onCreate(Bundle icicle) { + setLoading(true /* loading */, false /* animate */); + super.onCreate(icicle); + getActivity().setTitle(R.string.additional_permissions); + setHasOptionsMenu(true); + } + + @Override + public void onDestroy() { + getActivity().setTitle(R.string.app_permissions); + super.onDestroy(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java new file mode 100644 index 00000000..e41e05fb --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java @@ -0,0 +1,434 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.v14.preference.SwitchPreference; +import android.support.v4.util.ArrayMap; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceChangeListener; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceScreen; +import android.util.ArraySet; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.Callback; +import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class PermissionAppsFragment extends PermissionsFrameFragment implements Callback, + OnPreferenceChangeListener { + + private static final int MENU_SHOW_SYSTEM = Menu.FIRST; + private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 1; + private static final String KEY_SHOW_SYSTEM_PREFS = "_showSystem"; + + public static PermissionAppsFragment newInstance(String permissionName) { + return setPermissionName(new PermissionAppsFragment(), permissionName); + } + + private static T setPermissionName(T fragment, String permissionName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PERMISSION_NAME, permissionName); + fragment.setArguments(arguments); + return fragment; + } + + private PermissionApps mPermissionApps; + + private PreferenceScreen mExtraScreen; + + private ArrayMap mToggledGroups; + private ArraySet mLauncherPkgs; + private boolean mHasConfirmedRevoke; + + private boolean mShowSystem; + private MenuItem mShowSystemMenu; + private MenuItem mHideSystemMenu; + + private Callback mOnPermissionsLoadedListener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + mPermissionApps = new PermissionApps(getActivity(), groupName, this); + mPermissionApps.refresh(true); + } + + @Override + public void onResume() { + super.onResume(); + mPermissionApps.refresh(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, + R.string.menu_show_system); + mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE, + R.string.menu_hide_system); + updateMenu(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getActivity().finish(); + return true; + case MENU_SHOW_SYSTEM: + case MENU_HIDE_SYSTEM: + mShowSystem = item.getItemId() == MENU_SHOW_SYSTEM; + if (mPermissionApps.getApps() != null) { + onPermissionsLoaded(mPermissionApps); + } + updateMenu(); + break; + } + return super.onOptionsItemSelected(item); + } + + private void updateMenu() { + mShowSystemMenu.setVisible(!mShowSystem); + mHideSystemMenu.setVisible(mShowSystem); + } + + @Override + protected void onSetEmptyText(TextView textView) { + textView.setText(R.string.no_apps); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindUi(this, mPermissionApps); + } + + private static void bindUi(Fragment fragment, PermissionApps permissionApps) { + final Drawable icon = permissionApps.getIcon(); + final CharSequence label = permissionApps.getLabel(); + final ActionBar ab = fragment.getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(fragment.getString(R.string.permission_title, label)); + } + + final ViewGroup rootView = (ViewGroup) fragment.getView(); + final ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + iconView.setBackground(icon); + } + final TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(label); + } + final TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions); + } + } + + private void setOnPermissionsLoadedListener(Callback callback) { + mOnPermissionsLoadedListener = callback; + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + Context context = getPreferenceManager().getContext(); + + if (context == null) { + return; + } + + boolean isTelevision = Utils.isTelevision(context); + PreferenceScreen screen = getPreferenceScreen(); + + ArraySet preferencesToRemove = new ArraySet<>(); + for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(screen.getPreference(i).getKey()); + } + if (mExtraScreen != null) { + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(mExtraScreen.getPreference(i).getKey()); + } + } + + for (PermissionApp app : permissionApps.getApps()) { + if (!Utils.shouldShowPermission(app)) { + continue; + } + + String key = app.getKey(); + preferencesToRemove.remove(key); + Preference existingPref = screen.findPreference(key); + if (existingPref == null && mExtraScreen != null) { + existingPref = mExtraScreen.findPreference(key); + } + + boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs); + if (isSystemApp && !isTelevision && !mShowSystem) { + if (existingPref != null) { + screen.removePreference(existingPref); + } + continue; + } + + if (existingPref != null) { + // If existing preference - only update its state. + if (app.isPolicyFixed()) { + existingPref.setSummary(getString( + R.string.permission_summary_enforced_by_policy)); + } + existingPref.setPersistent(false); + existingPref.setEnabled(!app.isPolicyFixed()); + if (existingPref instanceof SwitchPreference) { + ((SwitchPreference) existingPref) + .setChecked(app.areRuntimePermissionsGranted()); + } + continue; + } + + SwitchPreference pref = new SwitchPreference(context); + pref.setOnPreferenceChangeListener(this); + pref.setKey(app.getKey()); + pref.setIcon(app.getIcon()); + pref.setTitle(app.getLabel()); + if (app.isPolicyFixed()) { + pref.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + pref.setPersistent(false); + pref.setEnabled(!app.isPolicyFixed()); + pref.setChecked(app.areRuntimePermissionsGranted()); + + if (isSystemApp && isTelevision) { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(pref); + } else { + screen.addPreference(pref); + } + } + + if (mExtraScreen != null) { + preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS); + Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS); + + if (pref == null) { + pref = new Preference(context); + pref.setKey(KEY_SHOW_SYSTEM_PREFS); + pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, + android.R.attr.colorControlNormal)); + pref.setTitle(R.string.preference_show_system_apps); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + SystemAppsFragment frag = new SystemAppsFragment(); + setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME)); + frag.setTargetFragment(PermissionAppsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("SystemApps") + .commit(); + return true; + } + }); + screen.addPreference(pref); + } + + int grantedCount = 0; + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) { + grantedCount++; + } + } + pref.setSummary(getString(R.string.app_permissions_group_summary, + grantedCount, mExtraScreen.getPreferenceCount())); + } + + for (String key : preferencesToRemove) { + Preference pref = screen.findPreference(key); + if (pref != null) { + screen.removePreference(pref); + } else if (mExtraScreen != null) { + pref = mExtraScreen.findPreference(key); + if (pref != null) { + mExtraScreen.removePreference(pref); + } + } + } + + setLoading(false /* loading */, true /* animate */); + + if (mOnPermissionsLoadedListener != null) { + mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps); + } + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String pkg = preference.getKey(); + final PermissionApp app = mPermissionApps.getApp(pkg); + + if (app == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(app.getPackageName(), app.getPermissionGroup()); + + if (LocationUtils.isLocationGroupAndProvider(mPermissionApps.getGroupName(), + app.getPackageName())) { + LocationUtils.showLocationDialog(getContext(), app.getLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + app.grantRuntimePermissions(); + } else { + final boolean grantedByDefault = app.hasGrantedByDefaultPermissions(); + if (grantedByDefault || (!app.hasRuntimePermissions() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + app.revokeRuntimePermissions(); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + app.revokeRuntimePermissions(); + } + } + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(String packageName, AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayMap<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.containsKey(packageName)) { + mToggledGroups.remove(packageName); + } else { + mToggledGroups.put(packageName, group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + final int groupCount = mToggledGroups.size(); + for (int i = 0; i < groupCount; i++) { + String packageName = mToggledGroups.keyAt(i); + List groups = new ArrayList<>(); + groups.add(mToggledGroups.valueAt(i)); + SafetyNetLogger.logPermissionsToggled(packageName, groups); + } + mToggledGroups = null; + } + } + + public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback { + PermissionAppsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (PermissionAppsFragment) getTargetFragment(); + setLoading(true /* loading */, false /* animate */); + super.onCreate(savedInstanceState); + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + if (mOuterFragment.mExtraScreen != null) { + setPreferenceScreen(); + } else { + mOuterFragment.setOnPermissionsLoadedListener(this); + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + PermissionApps permissionApps = new PermissionApps(getActivity(), groupName, null); + bindUi(this, permissionApps); + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + setPreferenceScreen(); + mOuterFragment.setOnPermissionsLoadedListener(null); + } + + private void setPreferenceScreen() { + setPreferenceScreen(mOuterFragment.mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java new file mode 100644 index 00000000..bc0e8457 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.annotation.Nullable; +import android.os.Bundle; +import android.support.v14.preference.PreferenceFragment; +import android.support.v17.leanback.widget.VerticalGridView; +import android.support.v7.preference.PreferenceScreen; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.RecyclerView.AdapterDataObserver; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.Animation.AnimationListener; +import android.view.animation.AnimationUtils; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +public abstract class PermissionsFrameFragment extends PreferenceFragment { + + private static final float WINDOW_ALIGNMENT_OFFSET_PERCENT = 50; + + private ViewGroup mPreferencesContainer; + + // TV-specific instance variables + @Nullable private VerticalGridView mGridView; + + private View mLoadingView; + private ViewGroup mPrefsView; + private boolean mIsLoading; + + /** + * Returns the view group that holds the preferences objects. This will + * only be set after {@link #onCreateView} has been called. + */ + protected final ViewGroup getPreferencesContainer() { + return mPreferencesContainer; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container, + false); + mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container); + if (mPrefsView == null) { + mPrefsView = rootView; + } + mLoadingView = rootView.findViewById(R.id.loading_container); + mPreferencesContainer = (ViewGroup) super.onCreateView( + inflater, mPrefsView, savedInstanceState); + setLoading(mIsLoading, false, true /* force */); + mPrefsView.addView(mPreferencesContainer); + return rootView; + } + + @Override + public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) { + PreferenceScreen preferences = getPreferenceScreen(); + if (preferences == null) { + preferences = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(preferences); + } + } + + protected void setLoading(boolean loading, boolean animate) { + setLoading(loading, animate, false); + } + + private void setLoading(boolean loading, boolean animate, boolean force) { + if (mIsLoading != loading || force) { + mIsLoading = loading; + if (getView() == null) { + // If there is no created view, there is no reason to animate. + animate = false; + } + if (mPrefsView != null) { + setViewShown(mPrefsView, !loading, animate); + } + if (mLoadingView != null) { + setViewShown(mLoadingView, loading, animate); + } + } + } + + private void setViewShown(final View view, boolean shown, boolean animate) { + if (animate) { + Animation animation = AnimationUtils.loadAnimation(getContext(), + shown ? android.R.anim.fade_in : android.R.anim.fade_out); + if (shown) { + view.setVisibility(View.VISIBLE); + } else { + animation.setAnimationListener(new AnimationListener() { + @Override + public void onAnimationStart(Animation animation) { + } + + @Override + public void onAnimationRepeat(Animation animation) { + } + + @Override + public void onAnimationEnd(Animation animation) { + view.setVisibility(View.INVISIBLE); + } + }); + } + view.startAnimation(animation); + } else { + view.clearAnimation(); + view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); + } + } + + @Override + public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, + Bundle savedInstanceState) { + if (Utils.isTelevision(getContext())) { + mGridView = (VerticalGridView) inflater.inflate( + R.layout.leanback_preferences_list, parent, false); + mGridView.setWindowAlignmentOffset(0); + mGridView.setWindowAlignmentOffsetPercent(WINDOW_ALIGNMENT_OFFSET_PERCENT); + mGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE); + mGridView.setFocusScrollStrategy(VerticalGridView.FOCUS_SCROLL_ALIGNED); + return mGridView; + } else { + return super.onCreateRecyclerView(inflater, parent, savedInstanceState); + } + } + + @Override + protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { + final RecyclerView.Adapter adapter = super.onCreateAdapter(preferenceScreen); + + if (adapter != null) { + final TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions); + onSetEmptyText(emptyView); + final RecyclerView recyclerView = getListView(); + adapter.registerAdapterDataObserver(new AdapterDataObserver() { + @Override + public void onChanged() { + checkEmpty(); + } + + @Override + public void onItemRangeInserted(int positionStart, int itemCount) { + checkEmpty(); + } + + @Override + public void onItemRangeRemoved(int positionStart, int itemCount) { + checkEmpty(); + } + + private void checkEmpty() { + boolean isEmpty = adapter.getItemCount() == 0; + emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); + recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE); + if (!isEmpty && mGridView != null) { + mGridView.requestFocus(); + } + } + }); + + boolean isEmpty = adapter.getItemCount() == 0; + emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); + recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE); + if (!isEmpty && mGridView != null) { + mGridView.requestFocus(); + } + } + + return adapter; + } + + /** + * Hook for subclasses to change the default text of the empty view. + * Base implementation leaves the default empty view text. + * + * @param textView the empty text view + */ + protected void onSetEmptyText(TextView textView) { + } +} + diff --git a/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java new file mode 100644 index 00000000..c7f5cda3 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +public abstract class SettingsWithHeader extends PermissionsFrameFragment + implements OnClickListener { + + private View mHeader; + protected Intent mInfoIntent; + protected Drawable mIcon; + protected CharSequence mLabel; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); + + if (!Utils.isTelevision(getContext())) { + mHeader = inflater.inflate(R.layout.header, root, false); + getPreferencesContainer().addView(mHeader, 0); + updateHeader(); + } + + return root; + } + + public void setHeader(Drawable icon, CharSequence label, Intent infoIntent) { + mIcon = icon; + mLabel = label; + mInfoIntent = infoIntent; + updateHeader(); + } + + private void updateHeader() { + if (mHeader != null) { + final ImageView appIcon = (ImageView) mHeader.findViewById(R.id.icon); + appIcon.setImageDrawable(mIcon); + + final TextView appName = (TextView) mHeader.findViewById(R.id.name); + appName.setText(mLabel); + + final View info = mHeader.findViewById(R.id.info); + if (mInfoIntent == null) { + info.setVisibility(View.GONE); + } else { + info.setVisibility(View.VISIBLE); + info.setClickable(true); + info.setOnClickListener(this); + } + } + } + + @Override + public void onClick(View v) { + getActivity().startActivity(mInfoIntent); + } + +} diff --git a/src/com/android/packageinstaller/permission/utils/LocationUtils.java b/src/com/android/packageinstaller/permission/utils/LocationUtils.java index 512fcf44..0296ae80 100644 --- a/src/com/android/packageinstaller/permission/utils/LocationUtils.java +++ b/src/com/android/packageinstaller/permission/utils/LocationUtils.java @@ -36,23 +36,9 @@ public class LocationUtils { public static final String LOCATION_PERMISSION = Manifest.permission_group.LOCATION; - public static ArrayList getLocationProviders() { - ArrayList providers = new ArrayList<>(); - Resources res = Resources.getSystem(); - providers.add(res.getString( - com.android.internal.R.string.config_networkLocationProviderPackageName)); - - for (String provider : - res.getStringArray(com.android.internal.R.array.config_locationProviderPackageNames)) { - providers.add(provider); - } - - return providers; - } - public static void showLocationDialog(final Context context, CharSequence label) { new AlertDialog.Builder(context) - .setIcon(com.android.internal.R.drawable.ic_dialog_alert_material) + .setIcon(R.drawable.ic_dialog_alert_material) .setTitle(android.R.string.dialog_alert_title) .setMessage(context.getString(R.string.location_warning, label)) .setNegativeButton(R.string.ok, null) @@ -83,5 +69,4 @@ public class LocationUtils { return false; } } - } -- cgit v1.2.3 From ef861375eebd9ac6cce7c0bb163380ab1c951063 Mon Sep 17 00:00:00 2001 From: Svetoslav Date: Fri, 20 Nov 2015 14:00:11 -0800 Subject: resolve merge conflicts of c10abb25f3 to cw-e-dev. Change-Id: I3fe38a9ac62466b38efec834dceb712d2782c518 --- .../permission/model/PermissionApps.java | 3 +- .../permission/ui/AllAppPermissionsFragment.java | 217 ---------- .../permission/ui/AppPermissionsFragment.java | 403 ------------------ .../permission/ui/ButtonBarLayout.java | 117 ++++++ .../permission/ui/GrantPermissionsActivity.java | 12 +- .../ui/GrantPermissionsDefaultViewHandler.java | 462 --------------------- .../ui/GrantPermissionsTvViewHandler.java | 130 ------ .../permission/ui/GrantPermissionsViewHandler.java | 2 +- .../permission/ui/ManagePermissionsActivity.java | 24 +- .../permission/ui/ManagePermissionsFragment.java | 267 ------------ .../permission/ui/OverlayWarningDialog.java | 1 + .../permission/ui/PermissionAppsFragment.java | 434 ------------------- .../permission/ui/PreferenceImageView.java | 69 +++ .../ui/handheld/AllAppPermissionsFragment.java | 214 ++++++++++ .../ui/handheld/AppPermissionsFragment.java | 404 ++++++++++++++++++ .../handheld/GrantPermissionsViewHandlerImpl.java | 462 +++++++++++++++++++++ .../ui/handheld/ManagePermissionsFragment.java | 268 ++++++++++++ .../ui/handheld/PermissionAppsFragment.java | 427 +++++++++++++++++++ .../ui/handheld/PermissionsFrameFragment.java | 121 ++++++ .../permission/ui/handheld/SettingsWithHeader.java | 84 ++++ .../ui/television/AllAppPermissionsFragment.java | 217 ++++++++++ .../ui/television/AppPermissionsFragment.java | 404 ++++++++++++++++++ .../GrantPermissionsViewHandlerImpl.java | 131 ++++++ .../ui/television/ManagePermissionsFragment.java | 267 ++++++++++++ .../ui/television/PermissionAppsFragment.java | 435 +++++++++++++++++++ .../ui/television/PermissionsFrameFragment.java | 203 +++++++++ .../ui/television/SettingsWithHeader.java | 85 ++++ .../permission/utils/LocationUtils.java | 17 +- 28 files changed, 3940 insertions(+), 1940 deletions(-) delete mode 100644 src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java delete mode 100644 src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java delete mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java delete mode 100644 src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java delete mode 100644 src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java delete mode 100644 src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/PreferenceImageView.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java create mode 100644 src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/model/PermissionApps.java b/src/com/android/packageinstaller/permission/model/PermissionApps.java index 9365bf13..e5d96d55 100644 --- a/src/com/android/packageinstaller/permission/model/PermissionApps.java +++ b/src/com/android/packageinstaller/permission/model/PermissionApps.java @@ -31,6 +31,7 @@ import android.util.ArraySet; import android.util.Log; import android.util.SparseArray; +import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; import java.util.ArrayList; @@ -275,7 +276,7 @@ public class PermissionApps { if (info.icon != 0) { mIcon = info.loadUnbadgedIcon(mPm); } else { - mIcon = mContext.getDrawable(com.android.internal.R.drawable.ic_perm_device_info); + mIcon = mContext.getDrawable(R.drawable.ic_perm_device_info); } mIcon = Utils.applyTint(mContext, mIcon, android.R.attr.colorControlNormal); } diff --git a/src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java deleted file mode 100644 index 2fb9a510..00000000 --- a/src/com/android/packageinstaller/permission/ui/AllAppPermissionsFragment.java +++ /dev/null @@ -1,217 +0,0 @@ -/* -* Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.app.ActionBar; -import android.app.AlertDialog; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageItemInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.content.pm.PermissionGroupInfo; -import android.content.pm.PermissionInfo; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Bundle; -import android.provider.Settings; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceCategory; -import android.support.v7.preference.PreferenceGroup; -import android.util.Log; -import android.view.MenuItem; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; - -public final class AllAppPermissionsFragment extends SettingsWithHeader { - - private static final String LOG_TAG = "AllAppPermissionsFragment"; - - private static final String KEY_OTHER = "other_perms"; - - public static AllAppPermissionsFragment newInstance(String packageName) { - AllAppPermissionsFragment instance = new AllAppPermissionsFragment(); - Bundle arguments = new Bundle(); - arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); - instance.setArguments(arguments); - return instance; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setTitle(R.string.all_permissions); - ab.setDisplayHomeAsUpEnabled(true); - } - } - - @Override - public void onResume() { - super.onResume(); - updateUi(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: { - getFragmentManager().popBackStack(); - return true; - } - } - return super.onOptionsItemSelected(item); - } - - private void updateUi() { - if (getPreferenceScreen() != null) { - getPreferenceScreen().removeAll(); - } - addPreferencesFromResource(R.xml.all_permissions); - PreferenceGroup otherGroup = (PreferenceGroup) findPreference(KEY_OTHER); - ArrayList prefs = new ArrayList<>(); // Used for sorting. - prefs.add(otherGroup); - String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); - otherGroup.removeAll(); - PackageManager pm = getContext().getPackageManager(); - - try { - PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS); - - ApplicationInfo appInfo = info.applicationInfo; - final Drawable icon = appInfo.loadIcon(pm); - final CharSequence label = appInfo.loadLabel(pm); - Intent infoIntent = null; - if (!getActivity().getIntent().getBooleanExtra( - AppPermissionsFragment.EXTRA_HIDE_INFO_BUTTON, false)) { - infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - .setData(Uri.fromParts("package", pkg, null)); - } - setHeader(icon, label, infoIntent); - - if (info.requestedPermissions != null) { - for (int i = 0; i < info.requestedPermissions.length; i++) { - PermissionInfo perm; - try { - perm = pm.getPermissionInfo(info.requestedPermissions[i], 0); - } catch (NameNotFoundException e) { - Log.e(LOG_TAG, - "Can't get permission info for " + info.requestedPermissions[i], e); - continue; - } - - if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0 - || (perm.flags & PermissionInfo.FLAG_HIDDEN) != 0) { - continue; - } - - if (perm.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { - PermissionGroupInfo group = getGroup(perm.group, pm); - PreferenceGroup pref = - findOrCreate(group != null ? group : perm, pm, prefs); - pref.addPreference(getPreference(perm, group, pm)); - } else if (perm.protectionLevel == PermissionInfo.PROTECTION_NORMAL) { - PermissionGroupInfo group = getGroup(perm.group, pm); - otherGroup.addPreference(getPreference(perm, group, pm)); - } - } - } - } catch (NameNotFoundException e) { - Log.e(LOG_TAG, "Problem getting package info for " + pkg, e); - } - // Sort an ArrayList of the groups and then set the order from the sorting. - Collections.sort(prefs, new Comparator() { - @Override - public int compare(Preference lhs, Preference rhs) { - String lKey = lhs.getKey(); - String rKey = rhs.getKey(); - if (lKey.equals(KEY_OTHER)) { - return 1; - } else if (rKey.equals(KEY_OTHER)) { - return -1; - } else if (Utils.isModernPermissionGroup(lKey) - != Utils.isModernPermissionGroup(rKey)) { - return Utils.isModernPermissionGroup(lKey) ? -1 : 1; - } - return lhs.getTitle().toString().compareTo(rhs.getTitle().toString()); - } - }); - for (int i = 0; i < prefs.size(); i++) { - prefs.get(i).setOrder(i); - } - } - - private PermissionGroupInfo getGroup(String group, PackageManager pm) { - try { - return pm.getPermissionGroupInfo(group, 0); - } catch (NameNotFoundException e) { - return null; - } - } - - private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm, - ArrayList prefs) { - PreferenceGroup pref = (PreferenceGroup) findPreference(group.name); - if (pref == null) { - pref = new PreferenceCategory(getContext()); - pref.setKey(group.name); - pref.setLayoutResource(R.layout.preference_category_material); - pref.setTitle(group.loadLabel(pm)); - prefs.add(pref); - getPreferenceScreen().addPreference(pref); - } - return pref; - } - - private Preference getPreference(PermissionInfo perm, PermissionGroupInfo group, - PackageManager pm) { - Preference pref = new Preference(getContext()); - pref.setLayoutResource(R.layout.preference_permissions); - Drawable icon = null; - if (perm.icon != 0) { - icon = perm.loadIcon(pm); - } else if (group != null && group.icon != 0) { - icon = group.loadIcon(pm); - } else { - icon = getContext().getDrawable(R.drawable.ic_perm_device_info); - } - pref.setIcon(Utils.applyTint(getContext(), icon, android.R.attr.colorControlNormal)); - pref.setTitle(perm.loadLabel(pm)); - final CharSequence desc = perm.loadDescription(pm); - pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - new AlertDialog.Builder(getContext()) - .setMessage(desc) - .setPositiveButton(android.R.string.ok, null) - .show(); - return true; - } - }); - - return pref; - } -} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java deleted file mode 100644 index 6396c61e..00000000 --- a/src/com/android/packageinstaller/permission/ui/AppPermissionsFragment.java +++ /dev/null @@ -1,403 +0,0 @@ -/* -* Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.annotation.Nullable; -import android.app.ActionBar; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Bundle; -import android.provider.Settings; -import android.support.v14.preference.SwitchPreference; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceChangeListener; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceScreen; -import android.util.Log; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.Toast; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.model.AppPermissionGroup; -import com.android.packageinstaller.permission.model.AppPermissions; -import com.android.packageinstaller.permission.utils.LocationUtils; -import com.android.packageinstaller.permission.utils.SafetyNetLogger; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.ArrayList; -import java.util.List; - -public final class AppPermissionsFragment extends SettingsWithHeader - implements OnPreferenceChangeListener { - - private static final String LOG_TAG = "ManagePermsFragment"; - - static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"; - - private static final int MENU_ALL_PERMS = 0; - - private List mToggledGroups; - private AppPermissions mAppPermissions; - private PreferenceScreen mExtraScreen; - - private boolean mHasConfirmedRevoke; - - public static AppPermissionsFragment newInstance(String packageName) { - return setPackageName(new AppPermissionsFragment(), packageName); - } - - private static T setPackageName(T fragment, String packageName) { - Bundle arguments = new Bundle(); - arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); - fragment.setArguments(arguments); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setLoading(true /* loading */, false /* animate */); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - } - - String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); - Activity activity = getActivity(); - PackageInfo packageInfo = getPackageInfo(activity, packageName); - if (packageInfo == null) { - Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); - activity.finish(); - return; - } - - mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { - @Override - public void run() { - getActivity().finish(); - } - }); - loadPreferences(); - } - - @Override - public void onResume() { - super.onResume(); - mAppPermissions.refresh(); - setPreferencesCheckedState(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: { - getActivity().finish(); - return true; - } - - case MENU_ALL_PERMS: { - Fragment frag = AllAppPermissionsFragment.newInstance( - getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, frag) - .addToBackStack("AllPerms") - .commit(); - return true; - } - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - if (mAppPermissions != null) { - bindUi(this, mAppPermissions.getPackageInfo()); - } - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - super.onCreateOptionsMenu(menu, inflater); - menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions); - } - - private static void bindUi(SettingsWithHeader fragment, PackageInfo packageInfo) { - Activity activity = fragment.getActivity(); - PackageManager pm = activity.getPackageManager(); - ApplicationInfo appInfo = packageInfo.applicationInfo; - Intent infoIntent = null; - if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) { - infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - .setData(Uri.fromParts("package", packageInfo.packageName, null)); - } - - Drawable icon = appInfo.loadIcon(pm); - CharSequence label = appInfo.loadLabel(pm); - fragment.setHeader(icon, label, infoIntent); - - ActionBar ab = activity.getActionBar(); - if (ab != null) { - ab.setTitle(R.string.app_permissions); - } - - ViewGroup rootView = (ViewGroup) fragment.getView(); - ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); - if (iconView != null) { - iconView.setImageDrawable(icon); - } - TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); - if (titleView != null) { - titleView.setText(R.string.app_permissions); - } - TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); - if (breadcrumbView != null) { - breadcrumbView.setText(label); - } - } - - private void loadPreferences() { - Context context = getPreferenceManager().getContext(); - if (context == null) { - return; - } - - PreferenceScreen screen = getPreferenceScreen(); - screen.removeAll(); - - if (mExtraScreen != null) { - mExtraScreen.removeAll(); - } - - final Preference extraPerms = new Preference(context); - extraPerms.setIcon(R.drawable.ic_toc); - extraPerms.setTitle(R.string.additional_permissions); - - for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { - if (!Utils.shouldShowPermission(group, mAppPermissions.getPackageInfo().packageName)) { - continue; - } - - boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); - - SwitchPreference preference = new SwitchPreference(context); - preference.setOnPreferenceChangeListener(this); - preference.setKey(group.getName()); - Drawable icon = Utils.loadDrawable(context.getPackageManager(), - group.getIconPkg(), group.getIconResId()); - preference.setIcon(Utils.applyTint(getContext(), icon, - android.R.attr.colorControlNormal)); - preference.setTitle(group.getLabel()); - if (group.isPolicyFixed()) { - preference.setSummary(getString(R.string.permission_summary_enforced_by_policy)); - } - preference.setPersistent(false); - preference.setEnabled(!group.isPolicyFixed()); - preference.setChecked(group.areRuntimePermissionsGranted()); - - if (isPlatform) { - screen.addPreference(preference); - } else { - if (mExtraScreen == null) { - mExtraScreen = getPreferenceManager().createPreferenceScreen(context); - } - mExtraScreen.addPreference(preference); - } - } - - if (mExtraScreen != null) { - extraPerms.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); - setPackageName(frag, getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); - frag.setTargetFragment(AppPermissionsFragment.this, 0); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, frag) - .addToBackStack(null) - .commit(); - return true; - } - }); - int count = mExtraScreen.getPreferenceCount(); - extraPerms.setSummary(getResources().getQuantityString( - R.plurals.additional_permissions_more, count, count)); - screen.addPreference(extraPerms); - } - - setLoading(false /* loading */, true /* animate */); - } - - @Override - public boolean onPreferenceChange(final Preference preference, Object newValue) { - String groupName = preference.getKey(); - final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName); - - if (group == null) { - return false; - } - - OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); - if (activity.isObscuredTouch()) { - activity.showOverlayDialog(); - return false; - } - - addToggledGroup(group); - - if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { - LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); - return false; - } - if (newValue == Boolean.TRUE) { - group.grantRuntimePermissions(false); - } else { - final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); - if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { - new AlertDialog.Builder(getContext()) - .setMessage(grantedByDefault ? R.string.system_warning - : R.string.old_sdk_deny_warning) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.grant_dialog_button_deny, - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - ((SwitchPreference) preference).setChecked(false); - group.revokeRuntimePermissions(false); - if (!grantedByDefault) { - mHasConfirmedRevoke = true; - } - } - }) - .show(); - return false; - } else { - group.revokeRuntimePermissions(false); - } - } - - return true; - } - - @Override - public void onPause() { - super.onPause(); - logToggledGroups(); - } - - private void addToggledGroup(AppPermissionGroup group) { - if (mToggledGroups == null) { - mToggledGroups = new ArrayList<>(); - } - // Double toggle is back to initial state. - if (mToggledGroups.contains(group)) { - mToggledGroups.remove(group); - } else { - mToggledGroups.add(group); - } - } - - private void logToggledGroups() { - if (mToggledGroups != null) { - String packageName = mAppPermissions.getPackageInfo().packageName; - SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); - mToggledGroups = null; - } - } - - private void setPreferencesCheckedState() { - setPreferencesCheckedState(getPreferenceScreen()); - if (mExtraScreen != null) { - setPreferencesCheckedState(mExtraScreen); - } - } - - private void setPreferencesCheckedState(PreferenceScreen screen) { - int preferenceCount = screen.getPreferenceCount(); - for (int i = 0; i < preferenceCount; i++) { - Preference preference = screen.getPreference(i); - if (preference instanceof SwitchPreference) { - SwitchPreference switchPref = (SwitchPreference) preference; - AppPermissionGroup group = mAppPermissions.getPermissionGroup(switchPref.getKey()); - if (group != null) { - switchPref.setChecked(group.areRuntimePermissionsGranted()); - } - } - } - } - - private static PackageInfo getPackageInfo(Activity activity, String packageName) { - try { - return activity.getPackageManager().getPackageInfo( - packageName, PackageManager.GET_PERMISSIONS); - } catch (PackageManager.NameNotFoundException e) { - Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); - return null; - } - } - - public static class AdditionalPermissionsFragment extends SettingsWithHeader { - AppPermissionsFragment mOuterFragment; - - @Override - public void onCreate(Bundle savedInstanceState) { - mOuterFragment = (AppPermissionsFragment) getTargetFragment(); - super.onCreate(savedInstanceState); - setHeader(mOuterFragment.mIcon, mOuterFragment.mLabel, mOuterFragment.mInfoIntent); - setHasOptionsMenu(true); - } - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferenceScreen(mOuterFragment.mExtraScreen); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); - bindUi(this, getPackageInfo(getActivity(), packageName)); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getFragmentManager().popBackStack(); - return true; - } - return super.onOptionsItemSelected(item); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java b/src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java new file mode 100644 index 00000000..59e54707 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/ButtonBarLayout.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.View; +import android.widget.LinearLayout; +import com.android.packageinstaller.R; + +/** + * An extension of LinearLayout that automatically switches to vertical + * orientation when it can't fit its child views horizontally. + */ +public class ButtonBarLayout extends LinearLayout { + /** Whether the current configuration allows stacking. */ + private boolean mAllowStacking; + + private int mLastWidthSize = -1; + + public ButtonBarLayout(Context context, AttributeSet attrs) { + super(context, attrs); + mAllowStacking = true; + } + + public void setAllowStacking(boolean allowStacking) { + if (mAllowStacking != allowStacking) { + mAllowStacking = allowStacking; + if (!mAllowStacking && getOrientation() == LinearLayout.VERTICAL) { + setStacked(false); + } + requestLayout(); + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + final int widthSize = MeasureSpec.getSize(widthMeasureSpec); + + if (mAllowStacking) { + if (widthSize > mLastWidthSize && isStacked()) { + // We're being measured wider this time, try un-stacking. + setStacked(false); + } + + mLastWidthSize = widthSize; + } + + boolean needsRemeasure = false; + + // If we're not stacked, make sure the measure spec is AT_MOST rather + // than EXACTLY. This ensures that we'll still get TOO_SMALL so that we + // know to stack the buttons. + final int initialWidthMeasureSpec; + if (!isStacked() && MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) { + initialWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.AT_MOST); + + // We'll need to remeasure again to fill excess space. + needsRemeasure = true; + } else { + initialWidthMeasureSpec = widthMeasureSpec; + } + + super.onMeasure(initialWidthMeasureSpec, heightMeasureSpec); + + if (mAllowStacking && !isStacked()) { + final int measuredWidth = getMeasuredWidthAndState(); + final int measuredWidthState = measuredWidth & MEASURED_STATE_MASK; + if (measuredWidthState == MEASURED_STATE_TOO_SMALL) { + setStacked(true); + + // Measure again in the new orientation. + needsRemeasure = true; + } + } + + if (needsRemeasure) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + + private void setStacked(boolean stacked) { + setOrientation(stacked ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL); + setGravity(stacked ? Gravity.RIGHT : Gravity.BOTTOM); + + final View spacer = findViewById(R.id.spacer); + if (spacer != null) { + spacer.setVisibility(stacked ? View.GONE : View.INVISIBLE); + } + + // Reverse the child order. This is specific to the Material button + // bar's layout XML and will probably not generalize. + final int childCount = getChildCount(); + for (int i = childCount - 2; i >= 0; i--) { + bringChildToFront(getChildAt(i)); + } + } + + private boolean isStacked() { + return getOrientation() == LinearLayout.VERTICAL; + } +} diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java index ffa8bf35..102fd6ef 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java @@ -26,11 +26,12 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PermissionInfo; import android.content.res.Resources; +import android.graphics.Typeface; import android.graphics.drawable.Icon; import android.hardware.camera2.utils.ArrayUtils; import android.os.Bundle; import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; +import android.text.style.StyleSpan; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; @@ -73,11 +74,13 @@ public class GrantPermissionsActivity extends OverlayTouchActivity setTitle(R.string.permission_request_title); if (DeviceUtils.isTelevision(this)) { - mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this); + mViewHandler = new com.android.packageinstaller.permission.ui.television + .GrantPermissionsViewHandlerImpl(this).setResultListener(this); } else if (DeviceUtils.isWear(this)) { mViewHandler = new GrantPermissionsWatchViewHandler(this).setResultListener(this); } else { - mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this); + mViewHandler = new com.android.packageinstaller.permission.ui.handheld + .GrantPermissionsViewHandlerImpl(this).setResultListener(this); } mRequestedPermissions = getIntent().getStringArrayExtra( @@ -209,8 +212,7 @@ public class GrantPermissionsActivity extends OverlayTouchActivity // Color the app name. int appLabelStart = message.toString().indexOf(appLabel.toString(), 0); int appLabelLength = appLabel.length(); - int color = getColor(R.color.grant_permissions_app_color); - message.setSpan(new ForegroundColorSpan(color), appLabelStart, + message.setSpan(new StyleSpan(Typeface.BOLD), appLabelStart, appLabelStart + appLabelLength, 0); // Set the new grant view diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java deleted file mode 100644 index c5d78784..00000000 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsDefaultViewHandler.java +++ /dev/null @@ -1,462 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; -import android.animation.ObjectAnimator; -import android.content.Context; -import android.content.res.Resources; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.util.SparseArray; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.View.OnLayoutChangeListener; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.view.ViewRootImpl; -import android.view.WindowManager.LayoutParams; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; -import android.view.animation.Interpolator; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.internal.widget.ButtonBarLayout; -import com.android.packageinstaller.R; - -import java.util.ArrayList; - -final class GrantPermissionsDefaultViewHandler - implements GrantPermissionsViewHandler, OnClickListener { - - public static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; - public static final String ARG_GROUP_COUNT = "ARG_GROUP_COUNT"; - public static final String ARG_GROUP_INDEX = "ARG_GROUP_INDEX"; - public static final String ARG_GROUP_ICON = "ARG_GROUP_ICON"; - public static final String ARG_GROUP_MESSAGE = "ARG_GROUP_MESSAGE"; - public static final String ARG_GROUP_SHOW_DO_NOT_ASK = "ARG_GROUP_SHOW_DO_NOT_ASK"; - public static final String ARG_GROUP_DO_NOT_ASK_CHECKED = "ARG_GROUP_DO_NOT_ASK_CHECKED"; - - // Animation parameters. - private static final long SIZE_START_DELAY = 300; - private static final long SIZE_START_LENGTH = 233; - private static final long FADE_OUT_START_DELAY = 300; - private static final long FADE_OUT_START_LENGTH = 217; - private static final long TRANSLATE_START_DELAY = 367; - private static final long TRANSLATE_LENGTH = 317; - private static final long GROUP_UPDATE_DELAY = 400; - private static final long DO_NOT_ASK_CHECK_DELAY = 450; - - private final Context mContext; - - private ResultListener mResultListener; - - private String mGroupName; - private int mGroupCount; - private int mGroupIndex; - private Icon mGroupIcon; - private CharSequence mGroupMessage; - private boolean mShowDonNotAsk; - private boolean mDoNotAskChecked; - - private ImageView mIconView; - private TextView mCurrentGroupView; - private TextView mMessageView; - private CheckBox mDoNotAskCheckbox; - private Button mAllowButton; - - private ArrayList mHeightControllers; - private ManualLayoutFrame mRootView; - - // Needed for animation - private ViewGroup mDescContainer; - private ViewGroup mCurrentDesc; - private ViewGroup mNextDesc; - - private ViewGroup mDialogContainer; - - private final Runnable mUpdateGroup = new Runnable() { - @Override - public void run() { - updateGroup(); - } - }; - - GrantPermissionsDefaultViewHandler(Context context) { - mContext = context; - } - - @Override - public GrantPermissionsDefaultViewHandler setResultListener(ResultListener listener) { - mResultListener = listener; - return this; - } - - @Override - public void saveInstanceState(Bundle arguments) { - arguments.putString(ARG_GROUP_NAME, mGroupName); - arguments.putInt(ARG_GROUP_COUNT, mGroupCount); - arguments.putInt(ARG_GROUP_INDEX, mGroupIndex); - arguments.putParcelable(ARG_GROUP_ICON, mGroupIcon); - arguments.putCharSequence(ARG_GROUP_MESSAGE, mGroupMessage); - arguments.putBoolean(ARG_GROUP_SHOW_DO_NOT_ASK, mShowDonNotAsk); - arguments.putBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED, mDoNotAskCheckbox.isChecked()); - } - - @Override - public void loadInstanceState(Bundle savedInstanceState) { - mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); - mGroupMessage = savedInstanceState.getCharSequence(ARG_GROUP_MESSAGE); - mGroupIcon = savedInstanceState.getParcelable(ARG_GROUP_ICON); - mGroupCount = savedInstanceState.getInt(ARG_GROUP_COUNT); - mGroupIndex = savedInstanceState.getInt(ARG_GROUP_INDEX); - mShowDonNotAsk = savedInstanceState.getBoolean(ARG_GROUP_SHOW_DO_NOT_ASK); - mDoNotAskChecked = savedInstanceState.getBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED); - } - - @Override - public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, - CharSequence message, boolean showDonNotAsk) { - mGroupName = groupName; - mGroupCount = groupCount; - mGroupIndex = groupIndex; - mGroupIcon = icon; - mGroupMessage = message; - mShowDonNotAsk = showDonNotAsk; - mDoNotAskChecked = false; - // If this is a second (or later) permission and the views exist, then animate. - if (mIconView != null) { - if (mGroupIndex > 0) { - // The first message will be announced as the title of the activity, all others - // we need to announce ourselves. - mDescContainer.announceForAccessibility(message); - animateToPermission(); - } else { - updateDescription(); - updateGroup(); - updateDoNotAskCheckBox(); - } - } - } - - private void animateToPermission() { - if (mHeightControllers == null) { - // We need to manually control the height of any views heigher than the root that - // we inflate. Find all the views up to the root and create ViewHeightControllers for - // them. - mHeightControllers = new ArrayList<>(); - ViewRootImpl viewRoot = mRootView.getViewRootImpl(); - ViewParent v = mRootView.getParent(); - addHeightController(mDialogContainer); - addHeightController(mRootView); - while (v != viewRoot) { - addHeightController((View) v); - v = v.getParent(); - } - // On the heighest level view, we want to setTop rather than setBottom to control the - // height, this way the dialog will grow up rather than down. - ViewHeightController realRootView = - mHeightControllers.get(mHeightControllers.size() - 1); - realRootView.setControlTop(true); - } - - // Grab the current height/y positions, then wait for the layout to change, - // so we can get the end height/y positions. - final SparseArray startPositions = getViewPositions(); - final int startHeight = mRootView.getLayoutHeight(); - mRootView.addOnLayoutChangeListener(new OnLayoutChangeListener() { - @Override - public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, - int oldTop, int oldRight, int oldBottom) { - mRootView.removeOnLayoutChangeListener(this); - SparseArray endPositions = getViewPositions(); - int endHeight = mRootView.getLayoutHeight(); - if (startPositions.get(R.id.do_not_ask_checkbox) == 0 - && endPositions.get(R.id.do_not_ask_checkbox) != 0) { - // If the checkbox didn't have a position before but has one now then set - // the start position to the end position because it just became visible. - startPositions.put(R.id.do_not_ask_checkbox, - endPositions.get(R.id.do_not_ask_checkbox)); - } - animateYPos(startPositions, endPositions, endHeight - startHeight); - } - }); - - // Fade out old description group and scale out the icon for it. - Interpolator interpolator = AnimationUtils.loadInterpolator(mContext, - android.R.interpolator.fast_out_linear_in); - mIconView.animate() - .scaleX(0) - .scaleY(0) - .setStartDelay(FADE_OUT_START_DELAY) - .setDuration(FADE_OUT_START_LENGTH) - .setInterpolator(interpolator) - .start(); - mCurrentDesc.animate() - .alpha(0) - .setStartDelay(FADE_OUT_START_DELAY) - .setDuration(FADE_OUT_START_LENGTH) - .setInterpolator(interpolator) - .setListener(null) - .start(); - - // Update the index of the permission after the animations have started. - mCurrentGroupView.getHandler().postDelayed(mUpdateGroup, GROUP_UPDATE_DELAY); - - // Add the new description and translate it in. - mNextDesc = (ViewGroup) LayoutInflater.from(mContext).inflate( - R.layout.permission_description, mDescContainer, false); - - mMessageView = (TextView) mNextDesc.findViewById(R.id.permission_message); - mIconView = (ImageView) mNextDesc.findViewById(R.id.permission_icon); - updateDescription(); - - int width = mDescContainer.getRootView().getWidth(); - mDescContainer.addView(mNextDesc); - mNextDesc.setTranslationX(width); - - final View oldDesc = mCurrentDesc; - // Remove the old view from the description, so that we can shrink if necessary. - mDescContainer.removeView(oldDesc); - oldDesc.setPadding(mDescContainer.getLeft(), mDescContainer.getTop(), - mRootView.getRight() - mDescContainer.getRight(), 0); - mRootView.addView(oldDesc); - - mCurrentDesc = mNextDesc; - mNextDesc.animate() - .translationX(0) - .setStartDelay(TRANSLATE_START_DELAY) - .setDuration(TRANSLATE_LENGTH) - .setInterpolator(AnimationUtils.loadInterpolator(mContext, - android.R.interpolator.linear_out_slow_in)) - .setListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - // This is the longest animation, when it finishes, we are done. - mRootView.removeView(oldDesc); - } - }) - .start(); - - boolean visibleBefore = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; - updateDoNotAskCheckBox(); - boolean visibleAfter = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; - if (visibleBefore != visibleAfter) { - Animation anim = AnimationUtils.loadAnimation(mContext, - visibleAfter ? android.R.anim.fade_in : android.R.anim.fade_out); - anim.setStartOffset(visibleAfter ? DO_NOT_ASK_CHECK_DELAY : 0); - mDoNotAskCheckbox.startAnimation(anim); - } - } - - private void addHeightController(View v) { - ViewHeightController heightController = new ViewHeightController(v); - heightController.setHeight(v.getHeight()); - mHeightControllers.add(heightController); - } - - private SparseArray getViewPositions() { - SparseArray locMap = new SparseArray<>(); - final int N = mDialogContainer.getChildCount(); - for (int i = 0; i < N; i++) { - View child = mDialogContainer.getChildAt(i); - if (child.getId() <= 0) { - // Only track views with ids. - continue; - } - locMap.put(child.getId(), child.getY()); - } - return locMap; - } - - private void animateYPos(SparseArray startPositions, SparseArray endPositions, - int heightDiff) { - final int N = startPositions.size(); - for (int i = 0; i < N; i++) { - int key = startPositions.keyAt(i); - float start = startPositions.get(key); - float end = endPositions.get(key); - if (start != end) { - final View child = mDialogContainer.findViewById(key); - child.setTranslationY(start - end); - child.animate() - .setStartDelay(SIZE_START_DELAY) - .setDuration(SIZE_START_LENGTH) - .translationY(0) - .start(); - } - } - for (int i = 0; i < mHeightControllers.size(); i++) { - mHeightControllers.get(i).animateAddHeight(heightDiff); - } - } - - @Override - public View createView() { - mRootView = (ManualLayoutFrame) LayoutInflater.from(mContext) - .inflate(R.layout.grant_permissions, null); - ((ButtonBarLayout) mRootView.findViewById(R.id.button_group)).setAllowStacking( - Resources.getSystem().getBoolean( - com.android.internal.R.bool.allow_stacked_button_bar)); - - mDialogContainer = (ViewGroup) mRootView.findViewById(R.id.dialog_container); - mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); - mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); - mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); - mDoNotAskCheckbox = (CheckBox) mRootView.findViewById(R.id.do_not_ask_checkbox); - mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); - - mDescContainer = (ViewGroup) mRootView.findViewById(R.id.desc_container); - mCurrentDesc = (ViewGroup) mRootView.findViewById(R.id.perm_desc_root); - - mAllowButton.setOnClickListener(this); - mRootView.findViewById(R.id.permission_deny_button).setOnClickListener(this); - mDoNotAskCheckbox.setOnClickListener(this); - - if (mGroupName != null) { - updateDescription(); - updateGroup(); - updateDoNotAskCheckBox(); - } - - return mRootView; - } - - @Override - public void updateWindowAttributes(LayoutParams outLayoutParams) { - // No-op - } - - private void updateDescription() { - mIconView.setImageDrawable(mGroupIcon.loadDrawable(mContext)); - mMessageView.setText(mGroupMessage); - } - - private void updateGroup() { - if (mGroupCount > 1) { - mCurrentGroupView.setVisibility(View.VISIBLE); - mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, - mGroupIndex + 1, mGroupCount)); - } else { - mCurrentGroupView.setVisibility(View.INVISIBLE); - } - } - - private void updateDoNotAskCheckBox() { - if (mShowDonNotAsk) { - mDoNotAskCheckbox.setVisibility(View.VISIBLE); - mDoNotAskCheckbox.setOnClickListener(this); - mDoNotAskCheckbox.setChecked(mDoNotAskChecked); - } else { - mDoNotAskCheckbox.setVisibility(View.GONE); - mDoNotAskCheckbox.setOnClickListener(null); - } - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.permission_allow_button: - if (mResultListener != null) { - view.clearAccessibilityFocus(); - mResultListener.onPermissionGrantResult(mGroupName, true, false); - } - break; - case R.id.permission_deny_button: - mAllowButton.setEnabled(true); - if (mResultListener != null) { - view.clearAccessibilityFocus(); - mResultListener.onPermissionGrantResult(mGroupName, false, - mDoNotAskCheckbox.isChecked()); - } - break; - case R.id.do_not_ask_checkbox: - mAllowButton.setEnabled(!mDoNotAskCheckbox.isChecked()); - break; - } - } - - @Override - public void onBackPressed() { - if (mResultListener != null) { - final boolean doNotAskAgain = mDoNotAskCheckbox.isChecked(); - mResultListener.onPermissionGrantResult(mGroupName, false, doNotAskAgain); - } - } - - /** - * Manually controls the height of a view through getBottom/setTop. Also listens - * for layout changes and sets the height again to be sure it doesn't change. - */ - private static final class ViewHeightController implements OnLayoutChangeListener { - private final View mView; - private int mHeight; - private int mNextHeight; - private boolean mControlTop; - private ObjectAnimator mAnimator; - - public ViewHeightController(View view) { - mView = view; - mView.addOnLayoutChangeListener(this); - } - - public void setControlTop(boolean controlTop) { - mControlTop = controlTop; - } - - public void animateAddHeight(int heightDiff) { - if (heightDiff != 0) { - if (mNextHeight == 0) { - mNextHeight = mHeight; - } - mNextHeight += heightDiff; - if (mAnimator != null) { - mAnimator.cancel(); - } - mAnimator = ObjectAnimator.ofInt(this, "height", mHeight, mNextHeight); - mAnimator.setStartDelay(SIZE_START_DELAY); - mAnimator.setDuration(SIZE_START_LENGTH); - mAnimator.start(); - } - } - - public void setHeight(int height) { - mHeight = height; - updateHeight(); - } - - @Override - public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, - int oldTop, int oldRight, int oldBottom) { - // Ensure that the height never changes. - updateHeight(); - } - - private void updateHeight() { - if (mControlTop) { - mView.setTop(mView.getBottom() - mHeight); - } else { - mView.setBottom(mView.getTop() + mHeight); - } - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java deleted file mode 100644 index 0e979ab6..00000000 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsTvViewHandler.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.android.packageinstaller.permission.ui; - -import android.content.Context; -import android.graphics.PixelFormat; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.view.Gravity; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.android.packageinstaller.R; - -/** - * TV-specific view handler for the grant permissions activity. - */ -final class GrantPermissionsTvViewHandler implements GrantPermissionsViewHandler, OnClickListener { - - private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; - - private final Context mContext; - - private ResultListener mResultListener; - - private String mGroupName; - - private LinearLayout mRootView; - private TextView mMessageView; - private ImageView mIconView; - private TextView mCurrentGroupView; - private Button mAllowButton; - private Button mSoftDenyButton; - private Button mHardDenyButton; - - GrantPermissionsTvViewHandler(Context context) { - mContext = context; - } - - @Override - public GrantPermissionsTvViewHandler setResultListener(ResultListener listener) { - mResultListener = listener; - return this; - } - - @Override - public View createView() { - mRootView = (LinearLayout) LayoutInflater.from(mContext) - .inflate(R.layout.grant_permissions, null); - - mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); - mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); - mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); - mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); - mSoftDenyButton = (Button) mRootView.findViewById(R.id.permission_deny_button); - mHardDenyButton = (Button) mRootView.findViewById( - R.id.permission_deny_dont_ask_again_button); - - mAllowButton.setOnClickListener(this); - mSoftDenyButton.setOnClickListener(this); - mHardDenyButton.setOnClickListener(this); - - return mRootView; - } - - @Override - public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { - outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; - outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; - outLayoutParams.format = PixelFormat.OPAQUE; - outLayoutParams.gravity = Gravity.BOTTOM; - outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; - outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; - } - - @Override - public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, - CharSequence message, boolean showDoNotAsk) { - mGroupName = groupName; - - mMessageView.setText(message); - mIconView.setImageIcon(icon); - mHardDenyButton.setVisibility(showDoNotAsk ? View.VISIBLE : View.GONE); - if (groupCount > 1) { - mCurrentGroupView.setVisibility(View.VISIBLE); - mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, - groupIndex + 1, groupCount)); - } else { - mCurrentGroupView.setVisibility(View.INVISIBLE); - } - } - - @Override - public void saveInstanceState(Bundle outState) { - outState.putString(ARG_GROUP_NAME, mGroupName); - } - - @Override - public void loadInstanceState(Bundle savedInstanceState) { - mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); - } - - @Override - public void onClick(View view) { - boolean granted = false; - boolean doNotAskAgain = false; - switch (view.getId()) { - case R.id.permission_allow_button: - granted = true; - break; - case R.id.permission_deny_dont_ask_again_button: - doNotAskAgain = true; - break; - } - if (mResultListener != null) { - mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); - } - } - - @Override - public void onBackPressed() { - if (mResultListener != null) { - mResultListener.onPermissionGrantResult(mGroupName, false, false); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java b/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java index 4032abb2..5e2259af 100644 --- a/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java +++ b/src/com/android/packageinstaller/permission/ui/GrantPermissionsViewHandler.java @@ -25,7 +25,7 @@ import android.view.WindowManager; * Class for managing the presentation and user interaction of the "grant * permissions" user interface. */ -interface GrantPermissionsViewHandler { +public interface GrantPermissionsViewHandler { /** * Listener interface for getting notified when the user responds to a diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java index f7fcec5e..419dbf42 100644 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java @@ -20,6 +20,7 @@ import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.util.Log; +import com.android.packageinstaller.permission.utils.Utils; import com.android.packageinstaller.permission.ui.wear.AppPermissionsFragmentWear; import com.android.packageinstaller.DeviceUtils; @@ -40,7 +41,13 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { switch (action) { case Intent.ACTION_MANAGE_PERMISSIONS: { - fragment = ManagePermissionsFragment.newInstance(); + if (Utils.isTelevision(this)) { + fragment = com.android.packageinstaller.permission.ui.television + .ManagePermissionsFragment.newInstance(); + } else { + fragment = com.android.packageinstaller.permission.ui.handheld + .ManagePermissionsFragment.newInstance(); + } } break; case Intent.ACTION_MANAGE_APP_PERMISSIONS: { @@ -50,11 +57,14 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { finish(); return; } - if (DeviceUtils.isWear(this)) { fragment = AppPermissionsFragmentWear.newInstance(packageName); + } else if (Utils.isTelevision(this)) { + fragment = com.android.packageinstaller.permission.ui.television + .AppPermissionsFragment.newInstance(packageName); } else { - fragment = AppPermissionsFragment.newInstance(packageName); + fragment = com.android.packageinstaller.permission.ui.handheld + .AppPermissionsFragment.newInstance(packageName); } } break; @@ -65,7 +75,13 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { finish(); return; } - fragment = PermissionAppsFragment.newInstance(permissionName); + if (Utils.isTelevision(this)) { + fragment = com.android.packageinstaller.permission.ui.television + .PermissionAppsFragment.newInstance(permissionName); + } else { + fragment = com.android.packageinstaller.permission.ui.handheld + .PermissionAppsFragment.newInstance(permissionName); + } } break; default: { diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java deleted file mode 100644 index e5e06e09..00000000 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsFragment.java +++ /dev/null @@ -1,267 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.annotation.Nullable; -import android.app.ActionBar; -import android.app.FragmentTransaction; -import android.content.ActivityNotFoundException; -import android.content.Context; -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceScreen; -import android.util.ArraySet; -import android.util.Log; -import android.view.MenuItem; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.model.PermissionApps; -import com.android.packageinstaller.permission.model.PermissionApps.PmCache; -import com.android.packageinstaller.permission.model.PermissionGroup; -import com.android.packageinstaller.permission.model.PermissionGroups; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.List; - -public final class ManagePermissionsFragment extends PermissionsFrameFragment - implements PermissionGroups.PermissionsGroupsChangeCallback, OnPreferenceClickListener { - private static final String LOG_TAG = "ManagePermissionsFragment"; - - private static final String OS_PKG = "android"; - - private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; - - private ArraySet mLauncherPkgs; - - private PermissionGroups mPermissions; - - private PreferenceScreen mExtraScreen; - - public static ManagePermissionsFragment newInstance() { - return new ManagePermissionsFragment(); - } - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - setLoading(true /* loading */, false /* animate */); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - } - mLauncherPkgs = Utils.getLauncherPackages(getContext()); - mPermissions = new PermissionGroups(getActivity(), getLoaderManager(), this); - } - - @Override - public void onResume() { - super.onResume(); - mPermissions.refresh(); - updatePermissionsUi(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - getActivity().finish(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public boolean onPreferenceClick(Preference preference) { - String key = preference.getKey(); - - PermissionGroup group = mPermissions.getGroup(key); - if (group == null) { - return false; - } - - Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) - .putExtra(Intent.EXTRA_PERMISSION_NAME, key); - try { - getActivity().startActivity(intent); - } catch (ActivityNotFoundException e) { - Log.w(LOG_TAG, "No app to handle " + intent); - } - - return true; - } - - @Override - public void onPermissionGroupsChanged() { - updatePermissionsUi(); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - bindPermissionUi(getActivity(), getView()); - } - - private static void bindPermissionUi(@Nullable Context context, @Nullable View rootView) { - if (context == null || rootView == null) { - return; - } - - ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); - if (iconView != null) { - // Set the icon as the background instead of the image because ImageView - // doesn't properly scale vector drawables beyond their intrinsic size - Drawable icon = context.getDrawable(R.drawable.ic_lock); - icon.setTint(context.getColor(R.color.off_white)); - iconView.setBackground(icon); - } - TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); - if (titleView != null) { - titleView.setText(R.string.app_permissions); - } - TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); - if (breadcrumbView != null) { - breadcrumbView.setText(R.string.app_permissions_breadcrumb); - } - } - - private void updatePermissionsUi() { - Context context = getPreferenceManager().getContext(); - if (context == null) { - return; - } - - List groups = mPermissions.getGroups(); - PreferenceScreen screen = getPreferenceScreen(); - - // Use this to speed up getting the info for all of the PermissionApps below. - // Create a new one for each refresh to make sure it has fresh data. - PmCache cache = new PmCache(getContext().getPackageManager()); - for (PermissionGroup group : groups) { - boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); - - Preference preference = findPreference(group.getName()); - if (preference == null && mExtraScreen != null) { - preference = mExtraScreen.findPreference(group.getName()); - } - if (preference == null) { - preference = new Preference(context); - preference.setOnPreferenceClickListener(this); - preference.setKey(group.getName()); - preference.setIcon(Utils.applyTint(context, group.getIcon(), - android.R.attr.colorControlNormal)); - preference.setTitle(group.getLabel()); - // Set blank summary so that no resizing/jumping happens when the summary is loaded. - preference.setSummary(" "); - preference.setPersistent(false); - if (isSystemPermission) { - screen.addPreference(preference); - } else { - if (mExtraScreen == null) { - mExtraScreen = getPreferenceManager().createPreferenceScreen(context); - } - mExtraScreen.addPreference(preference); - } - } - final Preference finalPref = preference; - - new PermissionApps(getContext(), group.getName(), new PermissionApps.Callback() { - @Override - public void onPermissionsLoaded(PermissionApps permissionApps) { - if (getActivity() == null) { - return; - } - int granted = permissionApps.getGrantedCount(mLauncherPkgs); - int total = permissionApps.getTotalCount(mLauncherPkgs); - finalPref.setSummary(getString(R.string.app_permissions_group_summary, - granted, total)); - } - }, cache).refresh(false); - } - - if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 - && screen.findPreference(EXTRA_PREFS_KEY) == null) { - Preference extraScreenPreference = new Preference(context); - extraScreenPreference.setKey(EXTRA_PREFS_KEY); - extraScreenPreference.setIcon(Utils.applyTint(context, - com.android.internal.R.drawable.ic_more_items, - android.R.attr.colorControlNormal)); - extraScreenPreference.setTitle(R.string.additional_permissions); - extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); - frag.setTargetFragment(ManagePermissionsFragment.this, 0); - FragmentTransaction ft = getFragmentManager().beginTransaction(); - ft.replace(android.R.id.content, frag); - ft.addToBackStack(null); - ft.commit(); - return true; - } - }); - int count = mExtraScreen.getPreferenceCount(); - extraScreenPreference.setSummary(getResources().getQuantityString( - R.plurals.additional_permissions_more, count, count)); - screen.addPreference(extraScreenPreference); - } - if (screen.getPreferenceCount() != 0) { - setLoading(false /* loading */, true /* animate */); - } - } - - public static class AdditionalPermissionsFragment extends PermissionsFrameFragment { - @Override - public void onCreate(Bundle icicle) { - setLoading(true /* loading */, false /* animate */); - super.onCreate(icicle); - getActivity().setTitle(R.string.additional_permissions); - setHasOptionsMenu(true); - } - - @Override - public void onDestroy() { - getActivity().setTitle(R.string.app_permissions); - super.onDestroy(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getFragmentManager().popBackStack(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - bindPermissionUi(getActivity(), getView()); - } - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); - setLoading(false /* loading */, true /* animate */); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java b/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java index a7c1e2a1..61734b47 100644 --- a/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java +++ b/src/com/android/packageinstaller/permission/ui/OverlayWarningDialog.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.android.packageinstaller.permission.ui; import android.app.Activity; diff --git a/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java deleted file mode 100644 index 1e588939..00000000 --- a/src/com/android/packageinstaller/permission/ui/PermissionAppsFragment.java +++ /dev/null @@ -1,434 +0,0 @@ -/* - * Copyright (C) 2015 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.packageinstaller.permission.ui; - -import android.app.ActionBar; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.support.v14.preference.SwitchPreference; -import android.support.v4.util.ArrayMap; -import android.support.v7.preference.Preference; -import android.support.v7.preference.Preference.OnPreferenceChangeListener; -import android.support.v7.preference.Preference.OnPreferenceClickListener; -import android.support.v7.preference.PreferenceScreen; -import android.util.ArraySet; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.packageinstaller.DeviceUtils; -import com.android.packageinstaller.R; -import com.android.packageinstaller.permission.model.AppPermissionGroup; -import com.android.packageinstaller.permission.model.PermissionApps; -import com.android.packageinstaller.permission.model.PermissionApps.Callback; -import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; -import com.android.packageinstaller.permission.utils.LocationUtils; -import com.android.packageinstaller.permission.utils.SafetyNetLogger; -import com.android.packageinstaller.permission.utils.Utils; - -import java.util.ArrayList; -import java.util.List; - -public final class PermissionAppsFragment extends PermissionsFrameFragment implements Callback, - OnPreferenceChangeListener { - - private static final int MENU_SHOW_SYSTEM = Menu.FIRST; - private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 1; - private static final String KEY_SHOW_SYSTEM_PREFS = "_showSystem"; - - public static PermissionAppsFragment newInstance(String permissionName) { - return setPermissionName(new PermissionAppsFragment(), permissionName); - } - - private static T setPermissionName(T fragment, String permissionName) { - Bundle arguments = new Bundle(); - arguments.putString(Intent.EXTRA_PERMISSION_NAME, permissionName); - fragment.setArguments(arguments); - return fragment; - } - - private PermissionApps mPermissionApps; - - private PreferenceScreen mExtraScreen; - - private ArrayMap mToggledGroups; - private ArraySet mLauncherPkgs; - private boolean mHasConfirmedRevoke; - - private boolean mShowSystem; - private MenuItem mShowSystemMenu; - private MenuItem mHideSystemMenu; - - private Callback mOnPermissionsLoadedListener; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setLoading(true /* loading */, false /* animate */); - setHasOptionsMenu(true); - final ActionBar ab = getActivity().getActionBar(); - if (ab != null) { - ab.setDisplayHomeAsUpEnabled(true); - } - mLauncherPkgs = Utils.getLauncherPackages(getContext()); - - String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); - mPermissionApps = new PermissionApps(getActivity(), groupName, this); - mPermissionApps.refresh(true); - } - - @Override - public void onResume() { - super.onResume(); - mPermissionApps.refresh(true); - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, - R.string.menu_show_system); - mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE, - R.string.menu_hide_system); - updateMenu(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getActivity().finish(); - return true; - case MENU_SHOW_SYSTEM: - case MENU_HIDE_SYSTEM: - mShowSystem = item.getItemId() == MENU_SHOW_SYSTEM; - if (mPermissionApps.getApps() != null) { - onPermissionsLoaded(mPermissionApps); - } - updateMenu(); - break; - } - return super.onOptionsItemSelected(item); - } - - private void updateMenu() { - mShowSystemMenu.setVisible(!mShowSystem); - mHideSystemMenu.setVisible(mShowSystem); - } - - @Override - protected void onSetEmptyText(TextView textView) { - textView.setText(R.string.no_apps); - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - bindUi(this, mPermissionApps); - } - - private static void bindUi(Fragment fragment, PermissionApps permissionApps) { - final Drawable icon = permissionApps.getIcon(); - final CharSequence label = permissionApps.getLabel(); - final ActionBar ab = fragment.getActivity().getActionBar(); - if (ab != null) { - ab.setTitle(fragment.getString(R.string.permission_title, label)); - } - - final ViewGroup rootView = (ViewGroup) fragment.getView(); - final ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); - if (iconView != null) { - // Set the icon as the background instead of the image because ImageView - // doesn't properly scale vector drawables beyond their intrinsic size - iconView.setBackground(icon); - } - final TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); - if (titleView != null) { - titleView.setText(label); - } - final TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); - if (breadcrumbView != null) { - breadcrumbView.setText(R.string.app_permissions); - } - } - - private void setOnPermissionsLoadedListener(Callback callback) { - mOnPermissionsLoadedListener = callback; - } - - @Override - public void onPermissionsLoaded(PermissionApps permissionApps) { - Context context = getPreferenceManager().getContext(); - - if (context == null) { - return; - } - - boolean isTelevision = DeviceUtils.isTelevision(context); - PreferenceScreen screen = getPreferenceScreen(); - - ArraySet preferencesToRemove = new ArraySet<>(); - for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) { - preferencesToRemove.add(screen.getPreference(i).getKey()); - } - if (mExtraScreen != null) { - for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { - preferencesToRemove.add(mExtraScreen.getPreference(i).getKey()); - } - } - - for (PermissionApp app : permissionApps.getApps()) { - if (!Utils.shouldShowPermission(app)) { - continue; - } - - String key = app.getKey(); - preferencesToRemove.remove(key); - Preference existingPref = screen.findPreference(key); - if (existingPref == null && mExtraScreen != null) { - existingPref = mExtraScreen.findPreference(key); - } - - boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs); - if (isSystemApp && !isTelevision && !mShowSystem) { - if (existingPref != null) { - screen.removePreference(existingPref); - } - continue; - } - - if (existingPref != null) { - // If existing preference - only update its state. - if (app.isPolicyFixed()) { - existingPref.setSummary(getString( - R.string.permission_summary_enforced_by_policy)); - } - existingPref.setPersistent(false); - existingPref.setEnabled(!app.isPolicyFixed()); - if (existingPref instanceof SwitchPreference) { - ((SwitchPreference) existingPref) - .setChecked(app.areRuntimePermissionsGranted()); - } - continue; - } - - SwitchPreference pref = new SwitchPreference(context); - pref.setOnPreferenceChangeListener(this); - pref.setKey(app.getKey()); - pref.setIcon(app.getIcon()); - pref.setTitle(app.getLabel()); - if (app.isPolicyFixed()) { - pref.setSummary(getString(R.string.permission_summary_enforced_by_policy)); - } - pref.setPersistent(false); - pref.setEnabled(!app.isPolicyFixed()); - pref.setChecked(app.areRuntimePermissionsGranted()); - - if (isSystemApp && isTelevision) { - if (mExtraScreen == null) { - mExtraScreen = getPreferenceManager().createPreferenceScreen(context); - } - mExtraScreen.addPreference(pref); - } else { - screen.addPreference(pref); - } - } - - if (mExtraScreen != null) { - preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS); - Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS); - - if (pref == null) { - pref = new Preference(context); - pref.setKey(KEY_SHOW_SYSTEM_PREFS); - pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, - android.R.attr.colorControlNormal)); - pref.setTitle(R.string.preference_show_system_apps); - pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - SystemAppsFragment frag = new SystemAppsFragment(); - setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME)); - frag.setTargetFragment(PermissionAppsFragment.this, 0); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, frag) - .addToBackStack("SystemApps") - .commit(); - return true; - } - }); - screen.addPreference(pref); - } - - int grantedCount = 0; - for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { - if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) { - grantedCount++; - } - } - pref.setSummary(getString(R.string.app_permissions_group_summary, - grantedCount, mExtraScreen.getPreferenceCount())); - } - - for (String key : preferencesToRemove) { - Preference pref = screen.findPreference(key); - if (pref != null) { - screen.removePreference(pref); - } else if (mExtraScreen != null) { - pref = mExtraScreen.findPreference(key); - if (pref != null) { - mExtraScreen.removePreference(pref); - } - } - } - - setLoading(false /* loading */, true /* animate */); - - if (mOnPermissionsLoadedListener != null) { - mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps); - } - } - - @Override - public boolean onPreferenceChange(final Preference preference, Object newValue) { - String pkg = preference.getKey(); - final PermissionApp app = mPermissionApps.getApp(pkg); - - if (app == null) { - return false; - } - - OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); - if (activity.isObscuredTouch()) { - activity.showOverlayDialog(); - return false; - } - - addToggledGroup(app.getPackageName(), app.getPermissionGroup()); - - if (LocationUtils.isLocationGroupAndProvider(mPermissionApps.getGroupName(), - app.getPackageName())) { - LocationUtils.showLocationDialog(getContext(), app.getLabel()); - return false; - } - if (newValue == Boolean.TRUE) { - app.grantRuntimePermissions(); - } else { - final boolean grantedByDefault = app.hasGrantedByDefaultPermissions(); - if (grantedByDefault || (!app.hasRuntimePermissions() && !mHasConfirmedRevoke)) { - new AlertDialog.Builder(getContext()) - .setMessage(grantedByDefault ? R.string.system_warning - : R.string.old_sdk_deny_warning) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.grant_dialog_button_deny, - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - ((SwitchPreference) preference).setChecked(false); - app.revokeRuntimePermissions(); - if (!grantedByDefault) { - mHasConfirmedRevoke = true; - } - } - }) - .show(); - return false; - } else { - app.revokeRuntimePermissions(); - } - } - return true; - } - - @Override - public void onPause() { - super.onPause(); - logToggledGroups(); - } - - private void addToggledGroup(String packageName, AppPermissionGroup group) { - if (mToggledGroups == null) { - mToggledGroups = new ArrayMap<>(); - } - // Double toggle is back to initial state. - if (mToggledGroups.containsKey(packageName)) { - mToggledGroups.remove(packageName); - } else { - mToggledGroups.put(packageName, group); - } - } - - private void logToggledGroups() { - if (mToggledGroups != null) { - final int groupCount = mToggledGroups.size(); - for (int i = 0; i < groupCount; i++) { - String packageName = mToggledGroups.keyAt(i); - List groups = new ArrayList<>(); - groups.add(mToggledGroups.valueAt(i)); - SafetyNetLogger.logPermissionsToggled(packageName, groups); - } - mToggledGroups = null; - } - } - - public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback { - PermissionAppsFragment mOuterFragment; - - @Override - public void onCreate(Bundle savedInstanceState) { - mOuterFragment = (PermissionAppsFragment) getTargetFragment(); - setLoading(true /* loading */, false /* animate */); - super.onCreate(savedInstanceState); - } - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - if (mOuterFragment.mExtraScreen != null) { - setPreferenceScreen(); - } else { - mOuterFragment.setOnPermissionsLoadedListener(this); - } - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); - PermissionApps permissionApps = new PermissionApps(getActivity(), groupName, null); - bindUi(this, permissionApps); - } - - @Override - public void onPermissionsLoaded(PermissionApps permissionApps) { - setPreferenceScreen(); - mOuterFragment.setOnPermissionsLoadedListener(null); - } - - private void setPreferenceScreen() { - setPreferenceScreen(mOuterFragment.mExtraScreen); - setLoading(false /* loading */, true /* animate */); - } - } -} diff --git a/src/com/android/packageinstaller/permission/ui/PreferenceImageView.java b/src/com/android/packageinstaller/permission/ui/PreferenceImageView.java new file mode 100644 index 00000000..c3f51674 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/PreferenceImageView.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2014 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.packageinstaller.permission.ui; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.ImageView; + +/** + * Extension of ImageView that correctly applies maxWidth and maxHeight. + */ +public class PreferenceImageView extends ImageView { + + public PreferenceImageView(Context context) { + this(context, null); + } + + public PreferenceImageView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) { + this(context, attrs, defStyleAttr, 0); + } + + public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + final int widthMode = MeasureSpec.getMode(widthMeasureSpec); + if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) { + final int widthSize = MeasureSpec.getSize(widthMeasureSpec); + final int maxWidth = getMaxWidth(); + if (maxWidth != Integer.MAX_VALUE + && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) { + widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST); + } + } + + final int heightMode = MeasureSpec.getMode(heightMeasureSpec); + if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) { + final int heightSize = MeasureSpec.getSize(heightMeasureSpec); + final int maxHeight = getMaxHeight(); + if (maxHeight != Integer.MAX_VALUE + && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) { + heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); + } + } + + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java new file mode 100644 index 00000000..b3b0895c --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/AllAppPermissionsFragment.java @@ -0,0 +1,214 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageItemInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionGroupInfo; +import android.content.pm.PermissionInfo; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceCategory; +import android.preference.PreferenceGroup; +import android.provider.Settings; +import android.util.Log; +import android.view.MenuItem; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public final class AllAppPermissionsFragment extends SettingsWithHeader { + + private static final String LOG_TAG = "AllAppPermissionsFragment"; + + private static final String KEY_OTHER = "other_perms"; + + public static AllAppPermissionsFragment newInstance(String packageName) { + AllAppPermissionsFragment instance = new AllAppPermissionsFragment(); + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + instance.setArguments(arguments); + return instance; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(R.string.all_permissions); + ab.setDisplayHomeAsUpEnabled(true); + } + } + + @Override + public void onResume() { + super.onResume(); + updateUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getFragmentManager().popBackStack(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + private void updateUi() { + if (getPreferenceScreen() != null) { + getPreferenceScreen().removeAll(); + } + addPreferencesFromResource(R.xml.all_permissions); + PreferenceGroup otherGroup = (PreferenceGroup) findPreference(KEY_OTHER); + ArrayList prefs = new ArrayList<>(); // Used for sorting. + prefs.add(otherGroup); + String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + otherGroup.removeAll(); + PackageManager pm = getContext().getPackageManager(); + + try { + PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS); + + ApplicationInfo appInfo = info.applicationInfo; + final Drawable icon = appInfo.loadIcon(pm); + final CharSequence label = appInfo.loadLabel(pm); + Intent infoIntent = null; + if (!getActivity().getIntent().getBooleanExtra( + AppPermissionsFragment.EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", pkg, null)); + } + setHeader(icon, label, infoIntent); + + if (info.requestedPermissions != null) { + for (int i = 0; i < info.requestedPermissions.length; i++) { + PermissionInfo perm; + try { + perm = pm.getPermissionInfo(info.requestedPermissions[i], 0); + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, + "Can't get permission info for " + info.requestedPermissions[i], e); + continue; + } + + if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0 + || (perm.flags & PermissionInfo.FLAG_HIDDEN) != 0) { + continue; + } + + if (perm.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { + PermissionGroupInfo group = getGroup(perm.group, pm); + PreferenceGroup pref = + findOrCreate(group != null ? group : perm, pm, prefs); + pref.addPreference(getPreference(perm, group, pm)); + } else if (perm.protectionLevel == PermissionInfo.PROTECTION_NORMAL) { + PermissionGroupInfo group = getGroup(perm.group, pm); + otherGroup.addPreference(getPreference(perm, group, pm)); + } + } + } + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, "Problem getting package info for " + pkg, e); + } + // Sort an ArrayList of the groups and then set the order from the sorting. + Collections.sort(prefs, new Comparator() { + @Override + public int compare(Preference lhs, Preference rhs) { + String lKey = lhs.getKey(); + String rKey = rhs.getKey(); + if (lKey.equals(KEY_OTHER)) { + return 1; + } else if (rKey.equals(KEY_OTHER)) { + return -1; + } else if (Utils.isModernPermissionGroup(lKey) + != Utils.isModernPermissionGroup(rKey)) { + return Utils.isModernPermissionGroup(lKey) ? -1 : 1; + } + return lhs.getTitle().toString().compareTo(rhs.getTitle().toString()); + } + }); + for (int i = 0; i < prefs.size(); i++) { + prefs.get(i).setOrder(i); + } + } + + private PermissionGroupInfo getGroup(String group, PackageManager pm) { + try { + return pm.getPermissionGroupInfo(group, 0); + } catch (NameNotFoundException e) { + return null; + } + } + + private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm, + ArrayList prefs) { + PreferenceGroup pref = (PreferenceGroup) findPreference(group.name); + if (pref == null) { + pref = new PreferenceCategory(getContext()); + pref.setKey(group.name); + pref.setTitle(group.loadLabel(pm)); + prefs.add(pref); + getPreferenceScreen().addPreference(pref); + } + return pref; + } + + private Preference getPreference(PermissionInfo perm, PermissionGroupInfo group, + PackageManager pm) { + Preference pref = new Preference(getContext()); + Drawable icon = null; + if (perm.icon != 0) { + icon = perm.loadIcon(pm); + } else if (group != null && group.icon != 0) { + icon = group.loadIcon(pm); + } else { + icon = getContext().getDrawable(R.drawable.ic_perm_device_info); + } + pref.setIcon(Utils.applyTint(getContext(), icon, android.R.attr.colorControlNormal)); + pref.setTitle(perm.loadLabel(pm)); + final CharSequence desc = perm.loadDescription(pm); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + new AlertDialog.Builder(getContext()) + .setMessage(desc) + .setPositiveButton(android.R.string.ok, null) + .show(); + return true; + } + }); + + return pref; + } +} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java new file mode 100644 index 00000000..f56cba70 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/AppPermissionsFragment.java @@ -0,0 +1,404 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; +import android.provider.Settings; +import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.AppPermissions; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class AppPermissionsFragment extends SettingsWithHeader + implements OnPreferenceChangeListener { + + private static final String LOG_TAG = "ManagePermsFragment"; + + static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"; + + private static final int MENU_ALL_PERMS = 0; + + private List mToggledGroups; + private AppPermissions mAppPermissions; + private PreferenceScreen mExtraScreen; + + private boolean mHasConfirmedRevoke; + + public static AppPermissionsFragment newInstance(String packageName) { + return setPackageName(new AppPermissionsFragment(), packageName); + } + + private static T setPackageName(T fragment, String packageName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + fragment.setArguments(arguments); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + Activity activity = getActivity(); + PackageInfo packageInfo = getPackageInfo(activity, packageName); + if (packageInfo == null) { + Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); + activity.finish(); + return; + } + + mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); + loadPreferences(); + } + + @Override + public void onResume() { + super.onResume(); + mAppPermissions.refresh(); + setPreferencesCheckedState(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getActivity().finish(); + return true; + } + + case MENU_ALL_PERMS: { + Fragment frag = AllAppPermissionsFragment.newInstance( + getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("AllPerms") + .commit(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (mAppPermissions != null) { + bindUi(this, mAppPermissions.getPackageInfo()); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions); + } + + private static void bindUi(SettingsWithHeader fragment, PackageInfo packageInfo) { + Activity activity = fragment.getActivity(); + PackageManager pm = activity.getPackageManager(); + ApplicationInfo appInfo = packageInfo.applicationInfo; + Intent infoIntent = null; + if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", packageInfo.packageName, null)); + } + + Drawable icon = appInfo.loadIcon(pm); + CharSequence label = appInfo.loadLabel(pm); + fragment.setHeader(icon, label, infoIntent); + + ActionBar ab = activity.getActionBar(); + if (ab != null) { + ab.setTitle(R.string.app_permissions); + } + + ViewGroup rootView = (ViewGroup) fragment.getView(); + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + iconView.setImageDrawable(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(label); + } + } + + private void loadPreferences() { + Context context = getActivity(); + if (context == null) { + return; + } + + PreferenceScreen screen = getPreferenceScreen(); + if (screen == null) { + screen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(screen); + } + + screen.removeAll(); + + if (mExtraScreen != null) { + mExtraScreen.removeAll(); + } + + final Preference extraPerms = new Preference(context); + extraPerms.setIcon(R.drawable.ic_toc); + extraPerms.setTitle(R.string.additional_permissions); + + for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { + if (!Utils.shouldShowPermission(group, mAppPermissions.getPackageInfo().packageName)) { + continue; + } + + boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); + + SwitchPreference preference = new SwitchPreference(context); + preference.setOnPreferenceChangeListener(this); + preference.setKey(group.getName()); + Drawable icon = Utils.loadDrawable(context.getPackageManager(), + group.getIconPkg(), group.getIconResId()); + preference.setIcon(Utils.applyTint(getContext(), icon, + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + if (group.isPolicyFixed()) { + preference.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + preference.setPersistent(false); + preference.setEnabled(!group.isPolicyFixed()); + preference.setChecked(group.areRuntimePermissionsGranted()); + + if (isPlatform) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + + if (mExtraScreen != null) { + extraPerms.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + setPackageName(frag, getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + frag.setTargetFragment(AppPermissionsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack(null) + .commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraPerms.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraPerms); + } + + setLoading(false /* loading */, true /* animate */); + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String groupName = preference.getKey(); + final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName); + + if (group == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(group); + + if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { + LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + group.grantRuntimePermissions(false); + } else { + final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); + if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + group.revokeRuntimePermissions(false); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + group.revokeRuntimePermissions(false); + } + } + + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayList<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.contains(group)) { + mToggledGroups.remove(group); + } else { + mToggledGroups.add(group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + String packageName = mAppPermissions.getPackageInfo().packageName; + SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); + mToggledGroups = null; + } + } + + private void setPreferencesCheckedState() { + setPreferencesCheckedState(getPreferenceScreen()); + if (mExtraScreen != null) { + setPreferencesCheckedState(mExtraScreen); + } + } + + private void setPreferencesCheckedState(PreferenceScreen screen) { + int preferenceCount = screen.getPreferenceCount(); + for (int i = 0; i < preferenceCount; i++) { + Preference preference = screen.getPreference(i); + if (preference instanceof SwitchPreference) { + SwitchPreference switchPref = (SwitchPreference) preference; + AppPermissionGroup group = mAppPermissions.getPermissionGroup(switchPref.getKey()); + if (group != null) { + switchPref.setChecked(group.areRuntimePermissionsGranted()); + } + } + } + } + + private static PackageInfo getPackageInfo(Activity activity, String packageName) { + try { + return activity.getPackageManager().getPackageInfo( + packageName, PackageManager.GET_PERMISSIONS); + } catch (PackageManager.NameNotFoundException e) { + Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); + return null; + } + } + + public static class AdditionalPermissionsFragment extends SettingsWithHeader { + AppPermissionsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (AppPermissionsFragment) getTargetFragment(); + super.onCreate(savedInstanceState); + setHeader(mOuterFragment.mIcon, mOuterFragment.mLabel, mOuterFragment.mInfoIntent); + setHasOptionsMenu(true); + setPreferenceScreen(mOuterFragment.mExtraScreen); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + bindUi(this, getPackageInfo(getActivity(), packageName)); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java b/src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java new file mode 100644 index 00000000..2d27f069 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/GrantPermissionsViewHandlerImpl.java @@ -0,0 +1,462 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.animation.ObjectAnimator; +import android.content.Context; +import android.content.res.Resources; +import android.graphics.drawable.Icon; +import android.os.Bundle; +import android.util.SparseArray; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnLayoutChangeListener; +import android.view.ViewGroup; +import android.view.ViewParent; +import android.view.ViewRootImpl; +import android.view.WindowManager.LayoutParams; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; +import android.view.animation.Interpolator; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.ui.ButtonBarLayout; +import com.android.packageinstaller.permission.ui.GrantPermissionsViewHandler; +import com.android.packageinstaller.permission.ui.ManualLayoutFrame; + +import java.util.ArrayList; + +public final class GrantPermissionsViewHandlerImpl + implements GrantPermissionsViewHandler, OnClickListener { + + public static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; + public static final String ARG_GROUP_COUNT = "ARG_GROUP_COUNT"; + public static final String ARG_GROUP_INDEX = "ARG_GROUP_INDEX"; + public static final String ARG_GROUP_ICON = "ARG_GROUP_ICON"; + public static final String ARG_GROUP_MESSAGE = "ARG_GROUP_MESSAGE"; + public static final String ARG_GROUP_SHOW_DO_NOT_ASK = "ARG_GROUP_SHOW_DO_NOT_ASK"; + public static final String ARG_GROUP_DO_NOT_ASK_CHECKED = "ARG_GROUP_DO_NOT_ASK_CHECKED"; + + // Animation parameters. + private static final long SIZE_START_DELAY = 300; + private static final long SIZE_START_LENGTH = 233; + private static final long FADE_OUT_START_DELAY = 300; + private static final long FADE_OUT_START_LENGTH = 217; + private static final long TRANSLATE_START_DELAY = 367; + private static final long TRANSLATE_LENGTH = 317; + private static final long GROUP_UPDATE_DELAY = 400; + private static final long DO_NOT_ASK_CHECK_DELAY = 450; + + private final Context mContext; + + private ResultListener mResultListener; + + private String mGroupName; + private int mGroupCount; + private int mGroupIndex; + private Icon mGroupIcon; + private CharSequence mGroupMessage; + private boolean mShowDonNotAsk; + private boolean mDoNotAskChecked; + + private ImageView mIconView; + private TextView mCurrentGroupView; + private TextView mMessageView; + private CheckBox mDoNotAskCheckbox; + private Button mAllowButton; + + private ArrayList mHeightControllers; + private ManualLayoutFrame mRootView; + + // Needed for animation + private ViewGroup mDescContainer; + private ViewGroup mCurrentDesc; + private ViewGroup mNextDesc; + + private ViewGroup mDialogContainer; + + private final Runnable mUpdateGroup = new Runnable() { + @Override + public void run() { + updateGroup(); + } + }; + + public GrantPermissionsViewHandlerImpl(Context context) { + mContext = context; + } + + @Override + public GrantPermissionsViewHandlerImpl setResultListener(ResultListener listener) { + mResultListener = listener; + return this; + } + + @Override + public void saveInstanceState(Bundle arguments) { + arguments.putString(ARG_GROUP_NAME, mGroupName); + arguments.putInt(ARG_GROUP_COUNT, mGroupCount); + arguments.putInt(ARG_GROUP_INDEX, mGroupIndex); + arguments.putParcelable(ARG_GROUP_ICON, mGroupIcon); + arguments.putCharSequence(ARG_GROUP_MESSAGE, mGroupMessage); + arguments.putBoolean(ARG_GROUP_SHOW_DO_NOT_ASK, mShowDonNotAsk); + arguments.putBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED, mDoNotAskCheckbox.isChecked()); + } + + @Override + public void loadInstanceState(Bundle savedInstanceState) { + mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); + mGroupMessage = savedInstanceState.getCharSequence(ARG_GROUP_MESSAGE); + mGroupIcon = savedInstanceState.getParcelable(ARG_GROUP_ICON); + mGroupCount = savedInstanceState.getInt(ARG_GROUP_COUNT); + mGroupIndex = savedInstanceState.getInt(ARG_GROUP_INDEX); + mShowDonNotAsk = savedInstanceState.getBoolean(ARG_GROUP_SHOW_DO_NOT_ASK); + mDoNotAskChecked = savedInstanceState.getBoolean(ARG_GROUP_DO_NOT_ASK_CHECKED); + } + + @Override + public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, + CharSequence message, boolean showDonNotAsk) { + mGroupName = groupName; + mGroupCount = groupCount; + mGroupIndex = groupIndex; + mGroupIcon = icon; + mGroupMessage = message; + mShowDonNotAsk = showDonNotAsk; + mDoNotAskChecked = false; + // If this is a second (or later) permission and the views exist, then animate. + if (mIconView != null) { + if (mGroupIndex > 0) { + // The first message will be announced as the title of the activity, all others + // we need to announce ourselves. + mDescContainer.announceForAccessibility(message); + animateToPermission(); + } else { + updateDescription(); + updateGroup(); + updateDoNotAskCheckBox(); + } + } + } + + private void animateToPermission() { + if (mHeightControllers == null) { + // We need to manually control the height of any views heigher than the root that + // we inflate. Find all the views up to the root and create ViewHeightControllers for + // them. + mHeightControllers = new ArrayList<>(); + ViewRootImpl viewRoot = mRootView.getViewRootImpl(); + ViewParent v = mRootView.getParent(); + addHeightController(mDialogContainer); + addHeightController(mRootView); + while (v != viewRoot) { + addHeightController((View) v); + v = v.getParent(); + } + // On the heighest level view, we want to setTop rather than setBottom to control the + // height, this way the dialog will grow up rather than down. + ViewHeightController realRootView = + mHeightControllers.get(mHeightControllers.size() - 1); + realRootView.setControlTop(true); + } + + // Grab the current height/y positions, then wait for the layout to change, + // so we can get the end height/y positions. + final SparseArray startPositions = getViewPositions(); + final int startHeight = mRootView.getLayoutHeight(); + mRootView.addOnLayoutChangeListener(new OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, + int oldTop, int oldRight, int oldBottom) { + mRootView.removeOnLayoutChangeListener(this); + SparseArray endPositions = getViewPositions(); + int endHeight = mRootView.getLayoutHeight(); + if (startPositions.get(R.id.do_not_ask_checkbox) == 0 + && endPositions.get(R.id.do_not_ask_checkbox) != 0) { + // If the checkbox didn't have a position before but has one now then set + // the start position to the end position because it just became visible. + startPositions.put(R.id.do_not_ask_checkbox, + endPositions.get(R.id.do_not_ask_checkbox)); + } + animateYPos(startPositions, endPositions, endHeight - startHeight); + } + }); + + // Fade out old description group and scale out the icon for it. + Interpolator interpolator = AnimationUtils.loadInterpolator(mContext, + android.R.interpolator.fast_out_linear_in); + mIconView.animate() + .scaleX(0) + .scaleY(0) + .setStartDelay(FADE_OUT_START_DELAY) + .setDuration(FADE_OUT_START_LENGTH) + .setInterpolator(interpolator) + .start(); + mCurrentDesc.animate() + .alpha(0) + .setStartDelay(FADE_OUT_START_DELAY) + .setDuration(FADE_OUT_START_LENGTH) + .setInterpolator(interpolator) + .setListener(null) + .start(); + + // Update the index of the permission after the animations have started. + mCurrentGroupView.getHandler().postDelayed(mUpdateGroup, GROUP_UPDATE_DELAY); + + // Add the new description and translate it in. + mNextDesc = (ViewGroup) LayoutInflater.from(mContext).inflate( + R.layout.permission_description, mDescContainer, false); + + mMessageView = (TextView) mNextDesc.findViewById(R.id.permission_message); + mIconView = (ImageView) mNextDesc.findViewById(R.id.permission_icon); + updateDescription(); + + int width = mDescContainer.getRootView().getWidth(); + mDescContainer.addView(mNextDesc); + mNextDesc.setTranslationX(width); + + final View oldDesc = mCurrentDesc; + // Remove the old view from the description, so that we can shrink if necessary. + mDescContainer.removeView(oldDesc); + oldDesc.setPadding(mDescContainer.getLeft(), mDescContainer.getTop(), + mRootView.getRight() - mDescContainer.getRight(), 0); + mRootView.addView(oldDesc); + + mCurrentDesc = mNextDesc; + mNextDesc.animate() + .translationX(0) + .setStartDelay(TRANSLATE_START_DELAY) + .setDuration(TRANSLATE_LENGTH) + .setInterpolator(AnimationUtils.loadInterpolator(mContext, + android.R.interpolator.linear_out_slow_in)) + .setListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + // This is the longest animation, when it finishes, we are done. + mRootView.removeView(oldDesc); + } + }) + .start(); + + boolean visibleBefore = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; + updateDoNotAskCheckBox(); + boolean visibleAfter = mDoNotAskCheckbox.getVisibility() == View.VISIBLE; + if (visibleBefore != visibleAfter) { + Animation anim = AnimationUtils.loadAnimation(mContext, + visibleAfter ? android.R.anim.fade_in : android.R.anim.fade_out); + anim.setStartOffset(visibleAfter ? DO_NOT_ASK_CHECK_DELAY : 0); + mDoNotAskCheckbox.startAnimation(anim); + } + } + + private void addHeightController(View v) { + ViewHeightController heightController = new ViewHeightController(v); + heightController.setHeight(v.getHeight()); + mHeightControllers.add(heightController); + } + + private SparseArray getViewPositions() { + SparseArray locMap = new SparseArray<>(); + final int N = mDialogContainer.getChildCount(); + for (int i = 0; i < N; i++) { + View child = mDialogContainer.getChildAt(i); + if (child.getId() <= 0) { + // Only track views with ids. + continue; + } + locMap.put(child.getId(), child.getY()); + } + return locMap; + } + + private void animateYPos(SparseArray startPositions, SparseArray endPositions, + int heightDiff) { + final int N = startPositions.size(); + for (int i = 0; i < N; i++) { + int key = startPositions.keyAt(i); + float start = startPositions.get(key); + float end = endPositions.get(key); + if (start != end) { + final View child = mDialogContainer.findViewById(key); + child.setTranslationY(start - end); + child.animate() + .setStartDelay(SIZE_START_DELAY) + .setDuration(SIZE_START_LENGTH) + .translationY(0) + .start(); + } + } + for (int i = 0; i < mHeightControllers.size(); i++) { + mHeightControllers.get(i).animateAddHeight(heightDiff); + } + } + + @Override + public View createView() { + mRootView = (ManualLayoutFrame) LayoutInflater.from(mContext) + .inflate(R.layout.grant_permissions, null); + ((ButtonBarLayout) mRootView.findViewById(R.id.button_group)).setAllowStacking(true); + + mDialogContainer = (ViewGroup) mRootView.findViewById(R.id.dialog_container); + mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); + mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); + mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); + mDoNotAskCheckbox = (CheckBox) mRootView.findViewById(R.id.do_not_ask_checkbox); + mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); + + mDescContainer = (ViewGroup) mRootView.findViewById(R.id.desc_container); + mCurrentDesc = (ViewGroup) mRootView.findViewById(R.id.perm_desc_root); + + mAllowButton.setOnClickListener(this); + mRootView.findViewById(R.id.permission_deny_button).setOnClickListener(this); + mDoNotAskCheckbox.setOnClickListener(this); + + if (mGroupName != null) { + updateDescription(); + updateGroup(); + updateDoNotAskCheckBox(); + } + + return mRootView; + } + + @Override + public void updateWindowAttributes(LayoutParams outLayoutParams) { + // No-op + } + + private void updateDescription() { + mIconView.setImageDrawable(mGroupIcon.loadDrawable(mContext)); + mMessageView.setText(mGroupMessage); + } + + private void updateGroup() { + if (mGroupCount > 1) { + mCurrentGroupView.setVisibility(View.VISIBLE); + mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, + mGroupIndex + 1, mGroupCount)); + } else { + mCurrentGroupView.setVisibility(View.INVISIBLE); + } + } + + private void updateDoNotAskCheckBox() { + if (mShowDonNotAsk) { + mDoNotAskCheckbox.setVisibility(View.VISIBLE); + mDoNotAskCheckbox.setOnClickListener(this); + mDoNotAskCheckbox.setChecked(mDoNotAskChecked); + } else { + mDoNotAskCheckbox.setVisibility(View.GONE); + mDoNotAskCheckbox.setOnClickListener(null); + } + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.permission_allow_button: + if (mResultListener != null) { + view.clearAccessibilityFocus(); + mResultListener.onPermissionGrantResult(mGroupName, true, false); + } + break; + case R.id.permission_deny_button: + mAllowButton.setEnabled(true); + if (mResultListener != null) { + view.clearAccessibilityFocus(); + mResultListener.onPermissionGrantResult(mGroupName, false, + mDoNotAskCheckbox.isChecked()); + } + break; + case R.id.do_not_ask_checkbox: + mAllowButton.setEnabled(!mDoNotAskCheckbox.isChecked()); + break; + } + } + + @Override + public void onBackPressed() { + if (mResultListener != null) { + final boolean doNotAskAgain = mDoNotAskCheckbox.isChecked(); + mResultListener.onPermissionGrantResult(mGroupName, false, doNotAskAgain); + } + } + + /** + * Manually controls the height of a view through getBottom/setTop. Also listens + * for layout changes and sets the height again to be sure it doesn't change. + */ + private static final class ViewHeightController implements OnLayoutChangeListener { + private final View mView; + private int mHeight; + private int mNextHeight; + private boolean mControlTop; + private ObjectAnimator mAnimator; + + public ViewHeightController(View view) { + mView = view; + mView.addOnLayoutChangeListener(this); + } + + public void setControlTop(boolean controlTop) { + mControlTop = controlTop; + } + + public void animateAddHeight(int heightDiff) { + if (heightDiff != 0) { + if (mNextHeight == 0) { + mNextHeight = mHeight; + } + mNextHeight += heightDiff; + if (mAnimator != null) { + mAnimator.cancel(); + } + mAnimator = ObjectAnimator.ofInt(this, "height", mHeight, mNextHeight); + mAnimator.setStartDelay(SIZE_START_DELAY); + mAnimator.setDuration(SIZE_START_LENGTH); + mAnimator.start(); + } + } + + public void setHeight(int height) { + mHeight = height; + updateHeight(); + } + + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, + int oldTop, int oldRight, int oldBottom) { + // Ensure that the height never changes. + updateHeight(); + } + + private void updateHeight() { + if (mControlTop) { + mView.setTop(mView.getBottom() - mHeight); + } else { + mView.setBottom(mView.getTop() + mHeight); + } + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java new file mode 100644 index 00000000..c53da879 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/ManagePermissionsFragment.java @@ -0,0 +1,268 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.util.ArraySet; +import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.PmCache; +import com.android.packageinstaller.permission.model.PermissionGroup; +import com.android.packageinstaller.permission.model.PermissionGroups; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.List; + +public final class ManagePermissionsFragment extends PermissionsFrameFragment + implements PermissionGroups.PermissionsGroupsChangeCallback, + Preference.OnPreferenceClickListener { + private static final String LOG_TAG = "ManagePermissionsFragment"; + + private static final String OS_PKG = "android"; + + private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; + + private ArraySet mLauncherPkgs; + + private PermissionGroups mPermissions; + + private PreferenceScreen mExtraScreen; + + public static ManagePermissionsFragment newInstance() { + return new ManagePermissionsFragment(); + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + mPermissions = new PermissionGroups(getActivity(), getLoaderManager(), this); + } + + @Override + public void onResume() { + super.onResume(); + mPermissions.refresh(); + updatePermissionsUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + getActivity().finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + String key = preference.getKey(); + + PermissionGroup group = mPermissions.getGroup(key); + if (group == null) { + return false; + } + + Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) + .putExtra(Intent.EXTRA_PERMISSION_NAME, key); + try { + getActivity().startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.w(LOG_TAG, "No app to handle " + intent); + } + + return true; + } + + @Override + public void onPermissionGroupsChanged() { + updatePermissionsUi(); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + + private static void bindPermissionUi(@Nullable Context context, @Nullable View rootView) { + if (context == null || rootView == null) { + return; + } + + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + Drawable icon = context.getDrawable(R.drawable.ic_lock); + icon.setTint(context.getColor(R.color.off_white)); + iconView.setBackground(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions_breadcrumb); + } + } + + private void updatePermissionsUi() { + Context context = getActivity(); + if (context == null) { + return; + } + + List groups = mPermissions.getGroups(); + PreferenceScreen screen = getPreferenceScreen(); + if (screen == null) { + screen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(screen); + } + + // Use this to speed up getting the info for all of the PermissionApps below. + // Create a new one for each refresh to make sure it has fresh data. + PmCache cache = new PmCache(getContext().getPackageManager()); + for (PermissionGroup group : groups) { + boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); + + Preference preference = findPreference(group.getName()); + if (preference == null && mExtraScreen != null) { + preference = mExtraScreen.findPreference(group.getName()); + } + if (preference == null) { + preference = new Preference(context); + preference.setOnPreferenceClickListener(this); + preference.setKey(group.getName()); + preference.setIcon(Utils.applyTint(context, group.getIcon(), + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + // Set blank summary so that no resizing/jumping happens when the summary is loaded. + preference.setSummary(" "); + preference.setPersistent(false); + if (isSystemPermission) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + final Preference finalPref = preference; + + new PermissionApps(getContext(), group.getName(), new PermissionApps.Callback() { + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + if (getActivity() == null) { + return; + } + int granted = permissionApps.getGrantedCount(mLauncherPkgs); + int total = permissionApps.getTotalCount(mLauncherPkgs); + finalPref.setSummary(getString(R.string.app_permissions_group_summary, + granted, total)); + } + }, cache).refresh(false); + } + + if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 + && screen.findPreference(EXTRA_PREFS_KEY) == null) { + Preference extraScreenPreference = new Preference(context); + extraScreenPreference.setKey(EXTRA_PREFS_KEY); + extraScreenPreference.setIcon(Utils.applyTint(context, + R.drawable.ic_more_items, + android.R.attr.colorControlNormal)); + extraScreenPreference.setTitle(R.string.additional_permissions); + extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + frag.setTargetFragment(ManagePermissionsFragment.this, 0); + FragmentTransaction ft = getFragmentManager().beginTransaction(); + ft.replace(android.R.id.content, frag); + ft.addToBackStack(null); + ft.commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraScreenPreference.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraScreenPreference); + } + if (screen.getPreferenceCount() != 0) { + setLoading(false /* loading */, true /* animate */); + } + } + + public static class AdditionalPermissionsFragment extends PermissionsFrameFragment { + @Override + public void onCreate(Bundle icicle) { + setLoading(true /* loading */, false /* animate */); + super.onCreate(icicle); + getActivity().setTitle(R.string.additional_permissions); + setHasOptionsMenu(true); + + setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + + @Override + public void onDestroy() { + getActivity().setTitle(R.string.app_permissions); + super.onDestroy(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java new file mode 100644 index 00000000..554830a7 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java @@ -0,0 +1,427 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; +import android.util.ArrayMap; +import android.util.ArraySet; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.Callback; +import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class PermissionAppsFragment extends PermissionsFrameFragment implements Callback, + Preference.OnPreferenceChangeListener { + + private static final int MENU_SHOW_SYSTEM = Menu.FIRST; + private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 1; + private static final String KEY_SHOW_SYSTEM_PREFS = "_showSystem"; + + public static PermissionAppsFragment newInstance(String permissionName) { + return setPermissionName(new PermissionAppsFragment(), permissionName); + } + + private static T setPermissionName(T fragment, String permissionName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PERMISSION_NAME, permissionName); + fragment.setArguments(arguments); + return fragment; + } + + private PermissionApps mPermissionApps; + + private PreferenceScreen mExtraScreen; + + private ArrayMap mToggledGroups; + private ArraySet mLauncherPkgs; + private boolean mHasConfirmedRevoke; + + private boolean mShowSystem; + private MenuItem mShowSystemMenu; + private MenuItem mHideSystemMenu; + + private Callback mOnPermissionsLoadedListener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + mPermissionApps = new PermissionApps(getActivity(), groupName, this); + mPermissionApps.refresh(true); + } + + @Override + public void onResume() { + super.onResume(); + mPermissionApps.refresh(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, + R.string.menu_show_system); + mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE, + R.string.menu_hide_system); + updateMenu(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getActivity().finish(); + return true; + case MENU_SHOW_SYSTEM: + case MENU_HIDE_SYSTEM: + mShowSystem = item.getItemId() == MENU_SHOW_SYSTEM; + if (mPermissionApps.getApps() != null) { + onPermissionsLoaded(mPermissionApps); + } + updateMenu(); + break; + } + return super.onOptionsItemSelected(item); + } + + private void updateMenu() { + mShowSystemMenu.setVisible(!mShowSystem); + mHideSystemMenu.setVisible(mShowSystem); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindUi(this, mPermissionApps); + } + + private static void bindUi(Fragment fragment, PermissionApps permissionApps) { + final Drawable icon = permissionApps.getIcon(); + final CharSequence label = permissionApps.getLabel(); + final ActionBar ab = fragment.getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(fragment.getString(R.string.permission_title, label)); + } + + final ViewGroup rootView = (ViewGroup) fragment.getView(); + final ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + iconView.setBackground(icon); + } + final TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(label); + } + final TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions); + } + } + + private void setOnPermissionsLoadedListener(Callback callback) { + mOnPermissionsLoadedListener = callback; + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + Context context = getActivity(); + + if (context == null) { + return; + } + + boolean isTelevision = Utils.isTelevision(context); + PreferenceScreen screen = getPreferenceScreen(); + if (screen == null) { + screen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(screen); + } + + ArraySet preferencesToRemove = new ArraySet<>(); + for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(screen.getPreference(i).getKey()); + } + if (mExtraScreen != null) { + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(mExtraScreen.getPreference(i).getKey()); + } + } + + for (PermissionApp app : permissionApps.getApps()) { + if (!Utils.shouldShowPermission(app)) { + continue; + } + + String key = app.getKey(); + preferencesToRemove.remove(key); + Preference existingPref = screen.findPreference(key); + if (existingPref == null && mExtraScreen != null) { + existingPref = mExtraScreen.findPreference(key); + } + + boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs); + if (isSystemApp && !isTelevision && !mShowSystem) { + if (existingPref != null) { + screen.removePreference(existingPref); + } + continue; + } + + if (existingPref != null) { + // If existing preference - only update its state. + if (app.isPolicyFixed()) { + existingPref.setSummary(getString( + R.string.permission_summary_enforced_by_policy)); + } + existingPref.setPersistent(false); + existingPref.setEnabled(!app.isPolicyFixed()); + if (existingPref instanceof SwitchPreference) { + ((SwitchPreference) existingPref) + .setChecked(app.areRuntimePermissionsGranted()); + } + continue; + } + + SwitchPreference pref = new SwitchPreference(context); + pref.setOnPreferenceChangeListener(this); + pref.setKey(app.getKey()); + pref.setIcon(app.getIcon()); + pref.setTitle(app.getLabel()); + if (app.isPolicyFixed()) { + pref.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + pref.setPersistent(false); + pref.setEnabled(!app.isPolicyFixed()); + pref.setChecked(app.areRuntimePermissionsGranted()); + + if (isSystemApp && isTelevision) { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(pref); + } else { + screen.addPreference(pref); + } + } + + if (mExtraScreen != null) { + preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS); + Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS); + + if (pref == null) { + pref = new Preference(context); + pref.setKey(KEY_SHOW_SYSTEM_PREFS); + pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, + android.R.attr.colorControlNormal)); + pref.setTitle(R.string.preference_show_system_apps); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + SystemAppsFragment frag = new SystemAppsFragment(); + setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME)); + frag.setTargetFragment(PermissionAppsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("SystemApps") + .commit(); + return true; + } + }); + screen.addPreference(pref); + } + + int grantedCount = 0; + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) { + grantedCount++; + } + } + pref.setSummary(getString(R.string.app_permissions_group_summary, + grantedCount, mExtraScreen.getPreferenceCount())); + } + + for (String key : preferencesToRemove) { + Preference pref = screen.findPreference(key); + if (pref != null) { + screen.removePreference(pref); + } else if (mExtraScreen != null) { + pref = mExtraScreen.findPreference(key); + if (pref != null) { + mExtraScreen.removePreference(pref); + } + } + } + + setLoading(false /* loading */, true /* animate */); + + if (mOnPermissionsLoadedListener != null) { + mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps); + } + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String pkg = preference.getKey(); + final PermissionApp app = mPermissionApps.getApp(pkg); + + if (app == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(app.getPackageName(), app.getPermissionGroup()); + + if (LocationUtils.isLocationGroupAndProvider(mPermissionApps.getGroupName(), + app.getPackageName())) { + LocationUtils.showLocationDialog(getContext(), app.getLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + app.grantRuntimePermissions(); + } else { + final boolean grantedByDefault = app.hasGrantedByDefaultPermissions(); + if (grantedByDefault || (!app.hasRuntimePermissions() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + app.revokeRuntimePermissions(); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + app.revokeRuntimePermissions(); + } + } + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(String packageName, AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayMap<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.containsKey(packageName)) { + mToggledGroups.remove(packageName); + } else { + mToggledGroups.put(packageName, group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + final int groupCount = mToggledGroups.size(); + for (int i = 0; i < groupCount; i++) { + String packageName = mToggledGroups.keyAt(i); + List groups = new ArrayList<>(); + groups.add(mToggledGroups.valueAt(i)); + SafetyNetLogger.logPermissionsToggled(packageName, groups); + } + mToggledGroups = null; + } + } + + public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback { + PermissionAppsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (PermissionAppsFragment) getTargetFragment(); + setLoading(true /* loading */, false /* animate */); + super.onCreate(savedInstanceState); + if (mOuterFragment.mExtraScreen != null) { + setPreferenceScreen(); + } else { + mOuterFragment.setOnPermissionsLoadedListener(this); + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + PermissionApps permissionApps = new PermissionApps(getActivity(), groupName, null); + bindUi(this, permissionApps); + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + setPreferenceScreen(); + mOuterFragment.setOnPermissionsLoadedListener(null); + } + + private void setPreferenceScreen() { + setPreferenceScreen(mOuterFragment.mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java new file mode 100644 index 00000000..e7f63b23 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/PermissionsFrameFragment.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.os.Bundle; +import android.preference.PreferenceFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.Animation.AnimationListener; +import android.view.animation.AnimationUtils; +import android.widget.ListView; +import android.widget.TextView; +import com.android.packageinstaller.R; + +public abstract class PermissionsFrameFragment extends PreferenceFragment { + private ViewGroup mPreferencesContainer; + + private View mLoadingView; + private ViewGroup mPrefsView; + private boolean mIsLoading; + + /** + * Returns the view group that holds the preferences objects. This will + * only be set after {@link #onCreateView} has been called. + */ + protected final ViewGroup getPreferencesContainer() { + return mPreferencesContainer; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container, + false); + mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container); + if (mPrefsView == null) { + mPrefsView = rootView; + } + mLoadingView = rootView.findViewById(R.id.loading_container); + mPreferencesContainer = (ViewGroup) super.onCreateView( + inflater, mPrefsView, savedInstanceState); + setLoading(mIsLoading, false, true /* force */); + mPrefsView.addView(mPreferencesContainer); + return rootView; + } + + protected void setLoading(boolean loading, boolean animate) { + setLoading(loading, animate, false); + } + + private void setLoading(boolean loading, boolean animate, boolean force) { + if (mIsLoading != loading || force) { + mIsLoading = loading; + if (getView() == null) { + // If there is no created view, there is no reason to animate. + animate = false; + } + if (mPrefsView != null) { + setViewShown(mPrefsView, !loading, animate); + } + if (mLoadingView != null) { + setViewShown(mLoadingView, loading, animate); + } + } + } + + @Override + public ListView getListView() { + ListView listView = super.getListView(); + if (listView.getEmptyView() == null) { + TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions); + listView.setEmptyView(emptyView); + } + return listView; + } + + private void setViewShown(final View view, boolean shown, boolean animate) { + if (animate) { + Animation animation = AnimationUtils.loadAnimation(getContext(), + shown ? android.R.anim.fade_in : android.R.anim.fade_out); + if (shown) { + view.setVisibility(View.VISIBLE); + } else { + animation.setAnimationListener(new AnimationListener() { + @Override + public void onAnimationStart(Animation animation) { + } + + @Override + public void onAnimationRepeat(Animation animation) { + } + + @Override + public void onAnimationEnd(Animation animation) { + view.setVisibility(View.INVISIBLE); + } + }); + } + view.startAnimation(animation); + } else { + view.clearAnimation(); + view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java new file mode 100644 index 00000000..acb3c61e --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld; + +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +public abstract class SettingsWithHeader extends PermissionsFrameFragment + implements OnClickListener { + + private View mHeader; + protected Intent mInfoIntent; + protected Drawable mIcon; + protected CharSequence mLabel; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); + + if (!Utils.isTelevision(getContext())) { + mHeader = inflater.inflate(R.layout.header, root, false); + getPreferencesContainer().addView(mHeader, 0); + updateHeader(); + } + + return root; + } + + public void setHeader(Drawable icon, CharSequence label, Intent infoIntent) { + mIcon = icon; + mLabel = label; + mInfoIntent = infoIntent; + updateHeader(); + } + + private void updateHeader() { + if (mHeader != null) { + final ImageView appIcon = (ImageView) mHeader.findViewById(R.id.icon); + appIcon.setImageDrawable(mIcon); + + final TextView appName = (TextView) mHeader.findViewById(R.id.name); + appName.setText(mLabel); + + final View info = mHeader.findViewById(R.id.info); + if (mInfoIntent == null) { + info.setVisibility(View.GONE); + } else { + info.setVisibility(View.VISIBLE); + info.setClickable(true); + info.setOnClickListener(this); + } + } + } + + @Override + public void onClick(View v) { + getActivity().startActivity(mInfoIntent); + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java new file mode 100644 index 00000000..d4910128 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/AllAppPermissionsFragment.java @@ -0,0 +1,217 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageItemInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionGroupInfo; +import android.content.pm.PermissionInfo; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceCategory; +import android.support.v7.preference.PreferenceGroup; +import android.util.Log; +import android.view.MenuItem; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public final class AllAppPermissionsFragment extends SettingsWithHeader { + + private static final String LOG_TAG = "AllAppPermissionsFragment"; + + private static final String KEY_OTHER = "other_perms"; + + public static AllAppPermissionsFragment newInstance(String packageName) { + AllAppPermissionsFragment instance = new AllAppPermissionsFragment(); + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + instance.setArguments(arguments); + return instance; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(R.string.all_permissions); + ab.setDisplayHomeAsUpEnabled(true); + } + } + + @Override + public void onResume() { + super.onResume(); + updateUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getFragmentManager().popBackStack(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + private void updateUi() { + if (getPreferenceScreen() != null) { + getPreferenceScreen().removeAll(); + } + addPreferencesFromResource(R.xml.all_permissions); + PreferenceGroup otherGroup = (PreferenceGroup) findPreference(KEY_OTHER); + ArrayList prefs = new ArrayList<>(); // Used for sorting. + prefs.add(otherGroup); + String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + otherGroup.removeAll(); + PackageManager pm = getContext().getPackageManager(); + + try { + PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS); + + ApplicationInfo appInfo = info.applicationInfo; + final Drawable icon = appInfo.loadIcon(pm); + final CharSequence label = appInfo.loadLabel(pm); + Intent infoIntent = null; + if (!getActivity().getIntent().getBooleanExtra( + AppPermissionsFragment.EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", pkg, null)); + } + setHeader(icon, label, infoIntent); + + if (info.requestedPermissions != null) { + for (int i = 0; i < info.requestedPermissions.length; i++) { + PermissionInfo perm; + try { + perm = pm.getPermissionInfo(info.requestedPermissions[i], 0); + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, + "Can't get permission info for " + info.requestedPermissions[i], e); + continue; + } + + if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0 + || (perm.flags & PermissionInfo.FLAG_HIDDEN) != 0) { + continue; + } + + if (perm.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS) { + PermissionGroupInfo group = getGroup(perm.group, pm); + PreferenceGroup pref = + findOrCreate(group != null ? group : perm, pm, prefs); + pref.addPreference(getPreference(perm, group, pm)); + } else if (perm.protectionLevel == PermissionInfo.PROTECTION_NORMAL) { + PermissionGroupInfo group = getGroup(perm.group, pm); + otherGroup.addPreference(getPreference(perm, group, pm)); + } + } + } + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, "Problem getting package info for " + pkg, e); + } + // Sort an ArrayList of the groups and then set the order from the sorting. + Collections.sort(prefs, new Comparator() { + @Override + public int compare(Preference lhs, Preference rhs) { + String lKey = lhs.getKey(); + String rKey = rhs.getKey(); + if (lKey.equals(KEY_OTHER)) { + return 1; + } else if (rKey.equals(KEY_OTHER)) { + return -1; + } else if (Utils.isModernPermissionGroup(lKey) + != Utils.isModernPermissionGroup(rKey)) { + return Utils.isModernPermissionGroup(lKey) ? -1 : 1; + } + return lhs.getTitle().toString().compareTo(rhs.getTitle().toString()); + } + }); + for (int i = 0; i < prefs.size(); i++) { + prefs.get(i).setOrder(i); + } + } + + private PermissionGroupInfo getGroup(String group, PackageManager pm) { + try { + return pm.getPermissionGroupInfo(group, 0); + } catch (NameNotFoundException e) { + return null; + } + } + + private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm, + ArrayList prefs) { + PreferenceGroup pref = (PreferenceGroup) findPreference(group.name); + if (pref == null) { + pref = new PreferenceCategory(getContext()); + pref.setKey(group.name); + pref.setLayoutResource(R.layout.preference_category_material); + pref.setTitle(group.loadLabel(pm)); + prefs.add(pref); + getPreferenceScreen().addPreference(pref); + } + return pref; + } + + private Preference getPreference(PermissionInfo perm, PermissionGroupInfo group, + PackageManager pm) { + Preference pref = new Preference(getContext()); + pref.setLayoutResource(R.layout.preference_permissions); + Drawable icon = null; + if (perm.icon != 0) { + icon = perm.loadIcon(pm); + } else if (group != null && group.icon != 0) { + icon = group.loadIcon(pm); + } else { + icon = getContext().getDrawable(R.drawable.ic_perm_device_info); + } + pref.setIcon(Utils.applyTint(getContext(), icon, android.R.attr.colorControlNormal)); + pref.setTitle(perm.loadLabel(pm)); + final CharSequence desc = perm.loadDescription(pm); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + new AlertDialog.Builder(getContext()) + .setMessage(desc) + .setPositiveButton(android.R.string.ok, null) + .show(); + return true; + } + }); + + return pref; + } +} \ No newline at end of file diff --git a/src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java new file mode 100644 index 00000000..42a2661c --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/AppPermissionsFragment.java @@ -0,0 +1,404 @@ +/* +* Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.support.v14.preference.SwitchPreference; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceChangeListener; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceScreen; +import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.AppPermissions; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class AppPermissionsFragment extends SettingsWithHeader + implements OnPreferenceChangeListener { + + private static final String LOG_TAG = "ManagePermsFragment"; + + static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"; + + private static final int MENU_ALL_PERMS = 0; + + private List mToggledGroups; + private AppPermissions mAppPermissions; + private PreferenceScreen mExtraScreen; + + private boolean mHasConfirmedRevoke; + + public static AppPermissionsFragment newInstance(String packageName) { + return setPackageName(new AppPermissionsFragment(), packageName); + } + + private static T setPackageName(T fragment, String packageName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); + fragment.setArguments(arguments); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + Activity activity = getActivity(); + PackageInfo packageInfo = getPackageInfo(activity, packageName); + if (packageInfo == null) { + Toast.makeText(activity, R.string.app_not_found_dlg_title, Toast.LENGTH_LONG).show(); + activity.finish(); + return; + } + + mAppPermissions = new AppPermissions(activity, packageInfo, null, true, new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); + loadPreferences(); + } + + @Override + public void onResume() { + super.onResume(); + mAppPermissions.refresh(); + setPreferencesCheckedState(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + getActivity().finish(); + return true; + } + + case MENU_ALL_PERMS: { + Fragment frag = AllAppPermissionsFragment.newInstance( + getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("AllPerms") + .commit(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (mAppPermissions != null) { + bindUi(this, mAppPermissions.getPackageInfo()); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions); + } + + private static void bindUi(SettingsWithHeader fragment, PackageInfo packageInfo) { + Activity activity = fragment.getActivity(); + PackageManager pm = activity.getPackageManager(); + ApplicationInfo appInfo = packageInfo.applicationInfo; + Intent infoIntent = null; + if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) { + infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + .setData(Uri.fromParts("package", packageInfo.packageName, null)); + } + + Drawable icon = appInfo.loadIcon(pm); + CharSequence label = appInfo.loadLabel(pm); + fragment.setHeader(icon, label, infoIntent); + + ActionBar ab = activity.getActionBar(); + if (ab != null) { + ab.setTitle(R.string.app_permissions); + } + + ViewGroup rootView = (ViewGroup) fragment.getView(); + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + iconView.setImageDrawable(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(label); + } + } + + private void loadPreferences() { + Context context = getPreferenceManager().getContext(); + if (context == null) { + return; + } + + PreferenceScreen screen = getPreferenceScreen(); + screen.removeAll(); + + if (mExtraScreen != null) { + mExtraScreen.removeAll(); + } + + final Preference extraPerms = new Preference(context); + extraPerms.setIcon(R.drawable.ic_toc); + extraPerms.setTitle(R.string.additional_permissions); + + for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { + if (!Utils.shouldShowPermission(group, mAppPermissions.getPackageInfo().packageName)) { + continue; + } + + boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG); + + SwitchPreference preference = new SwitchPreference(context); + preference.setOnPreferenceChangeListener(this); + preference.setKey(group.getName()); + Drawable icon = Utils.loadDrawable(context.getPackageManager(), + group.getIconPkg(), group.getIconResId()); + preference.setIcon(Utils.applyTint(getContext(), icon, + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + if (group.isPolicyFixed()) { + preference.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + preference.setPersistent(false); + preference.setEnabled(!group.isPolicyFixed()); + preference.setChecked(group.areRuntimePermissionsGranted()); + + if (isPlatform) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + + if (mExtraScreen != null) { + extraPerms.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + setPackageName(frag, getArguments().getString(Intent.EXTRA_PACKAGE_NAME)); + frag.setTargetFragment(AppPermissionsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack(null) + .commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraPerms.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraPerms); + } + + setLoading(false /* loading */, true /* animate */); + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String groupName = preference.getKey(); + final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName); + + if (group == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(group); + + if (LocationUtils.isLocationGroupAndProvider(group.getName(), group.getApp().packageName)) { + LocationUtils.showLocationDialog(getContext(), mAppPermissions.getAppLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + group.grantRuntimePermissions(false); + } else { + final boolean grantedByDefault = group.hasGrantedByDefaultPermission(); + if (grantedByDefault || (!group.hasRuntimePermission() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + group.revokeRuntimePermissions(false); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + group.revokeRuntimePermissions(false); + } + } + + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayList<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.contains(group)) { + mToggledGroups.remove(group); + } else { + mToggledGroups.add(group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + String packageName = mAppPermissions.getPackageInfo().packageName; + SafetyNetLogger.logPermissionsToggled(packageName, mToggledGroups); + mToggledGroups = null; + } + } + + private void setPreferencesCheckedState() { + setPreferencesCheckedState(getPreferenceScreen()); + if (mExtraScreen != null) { + setPreferencesCheckedState(mExtraScreen); + } + } + + private void setPreferencesCheckedState(PreferenceScreen screen) { + int preferenceCount = screen.getPreferenceCount(); + for (int i = 0; i < preferenceCount; i++) { + Preference preference = screen.getPreference(i); + if (preference instanceof SwitchPreference) { + SwitchPreference switchPref = (SwitchPreference) preference; + AppPermissionGroup group = mAppPermissions.getPermissionGroup(switchPref.getKey()); + if (group != null) { + switchPref.setChecked(group.areRuntimePermissionsGranted()); + } + } + } + } + + private static PackageInfo getPackageInfo(Activity activity, String packageName) { + try { + return activity.getPackageManager().getPackageInfo( + packageName, PackageManager.GET_PERMISSIONS); + } catch (PackageManager.NameNotFoundException e) { + Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e); + return null; + } + } + + public static class AdditionalPermissionsFragment extends SettingsWithHeader { + AppPermissionsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (AppPermissionsFragment) getTargetFragment(); + super.onCreate(savedInstanceState); + setHeader(mOuterFragment.mIcon, mOuterFragment.mLabel, mOuterFragment.mInfoIntent); + setHasOptionsMenu(true); + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferenceScreen(mOuterFragment.mExtraScreen); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME); + bindUi(this, getPackageInfo(getActivity(), packageName)); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java b/src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java new file mode 100644 index 00000000..a2538821 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/GrantPermissionsViewHandlerImpl.java @@ -0,0 +1,131 @@ +package com.android.packageinstaller.permission.ui.television; + +import android.content.Context; +import android.graphics.PixelFormat; +import android.graphics.drawable.Icon; +import android.os.Bundle; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.ui.GrantPermissionsViewHandler; + +/** + * TV-specific view handler for the grant permissions activity. + */ +public final class GrantPermissionsViewHandlerImpl implements GrantPermissionsViewHandler, OnClickListener { + + private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME"; + + private final Context mContext; + + private ResultListener mResultListener; + + private String mGroupName; + + private LinearLayout mRootView; + private TextView mMessageView; + private ImageView mIconView; + private TextView mCurrentGroupView; + private Button mAllowButton; + private Button mSoftDenyButton; + private Button mHardDenyButton; + + public GrantPermissionsViewHandlerImpl(Context context) { + mContext = context; + } + + @Override + public GrantPermissionsViewHandlerImpl setResultListener(ResultListener listener) { + mResultListener = listener; + return this; + } + + @Override + public View createView() { + mRootView = (LinearLayout) LayoutInflater.from(mContext) + .inflate(R.layout.grant_permissions, null); + + mMessageView = (TextView) mRootView.findViewById(R.id.permission_message); + mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon); + mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text); + mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button); + mSoftDenyButton = (Button) mRootView.findViewById(R.id.permission_deny_button); + mHardDenyButton = (Button) mRootView.findViewById( + R.id.permission_deny_dont_ask_again_button); + + mAllowButton.setOnClickListener(this); + mSoftDenyButton.setOnClickListener(this); + mHardDenyButton.setOnClickListener(this); + + return mRootView; + } + + @Override + public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) { + outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; + outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; + outLayoutParams.format = PixelFormat.OPAQUE; + outLayoutParams.gravity = Gravity.BOTTOM; + outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; + outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + } + + @Override + public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon, + CharSequence message, boolean showDoNotAsk) { + mGroupName = groupName; + + mMessageView.setText(message); + mIconView.setImageIcon(icon); + mHardDenyButton.setVisibility(showDoNotAsk ? View.VISIBLE : View.GONE); + if (groupCount > 1) { + mCurrentGroupView.setVisibility(View.VISIBLE); + mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template, + groupIndex + 1, groupCount)); + } else { + mCurrentGroupView.setVisibility(View.INVISIBLE); + } + } + + @Override + public void saveInstanceState(Bundle outState) { + outState.putString(ARG_GROUP_NAME, mGroupName); + } + + @Override + public void loadInstanceState(Bundle savedInstanceState) { + mGroupName = savedInstanceState.getString(ARG_GROUP_NAME); + } + + @Override + public void onClick(View view) { + boolean granted = false; + boolean doNotAskAgain = false; + switch (view.getId()) { + case R.id.permission_allow_button: + granted = true; + break; + case R.id.permission_deny_dont_ask_again_button: + doNotAskAgain = true; + break; + } + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain); + } + } + + @Override + public void onBackPressed() { + if (mResultListener != null) { + mResultListener.onPermissionGrantResult(mGroupName, false, false); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java b/src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java new file mode 100644 index 00000000..47301f48 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/ManagePermissionsFragment.java @@ -0,0 +1,267 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.annotation.Nullable; +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceScreen; +import android.util.ArraySet; +import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.PmCache; +import com.android.packageinstaller.permission.model.PermissionGroup; +import com.android.packageinstaller.permission.model.PermissionGroups; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.List; + +public final class ManagePermissionsFragment extends PermissionsFrameFragment + implements PermissionGroups.PermissionsGroupsChangeCallback, OnPreferenceClickListener { + private static final String LOG_TAG = "ManagePermissionsFragment"; + + private static final String OS_PKG = "android"; + + private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; + + private ArraySet mLauncherPkgs; + + private PermissionGroups mPermissions; + + private PreferenceScreen mExtraScreen; + + public static ManagePermissionsFragment newInstance() { + return new ManagePermissionsFragment(); + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + mPermissions = new PermissionGroups(getActivity(), getLoaderManager(), this); + } + + @Override + public void onResume() { + super.onResume(); + mPermissions.refresh(); + updatePermissionsUi(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + getActivity().finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + String key = preference.getKey(); + + PermissionGroup group = mPermissions.getGroup(key); + if (group == null) { + return false; + } + + Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) + .putExtra(Intent.EXTRA_PERMISSION_NAME, key); + try { + getActivity().startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.w(LOG_TAG, "No app to handle " + intent); + } + + return true; + } + + @Override + public void onPermissionGroupsChanged() { + updatePermissionsUi(); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + + private static void bindPermissionUi(@Nullable Context context, @Nullable View rootView) { + if (context == null || rootView == null) { + return; + } + + ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + Drawable icon = context.getDrawable(R.drawable.ic_lock); + icon.setTint(context.getColor(R.color.off_white)); + iconView.setBackground(icon); + } + TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(R.string.app_permissions); + } + TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions_breadcrumb); + } + } + + private void updatePermissionsUi() { + Context context = getPreferenceManager().getContext(); + if (context == null) { + return; + } + + List groups = mPermissions.getGroups(); + PreferenceScreen screen = getPreferenceScreen(); + + // Use this to speed up getting the info for all of the PermissionApps below. + // Create a new one for each refresh to make sure it has fresh data. + PmCache cache = new PmCache(getContext().getPackageManager()); + for (PermissionGroup group : groups) { + boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); + + Preference preference = findPreference(group.getName()); + if (preference == null && mExtraScreen != null) { + preference = mExtraScreen.findPreference(group.getName()); + } + if (preference == null) { + preference = new Preference(context); + preference.setOnPreferenceClickListener(this); + preference.setKey(group.getName()); + preference.setIcon(Utils.applyTint(context, group.getIcon(), + android.R.attr.colorControlNormal)); + preference.setTitle(group.getLabel()); + // Set blank summary so that no resizing/jumping happens when the summary is loaded. + preference.setSummary(" "); + preference.setPersistent(false); + if (isSystemPermission) { + screen.addPreference(preference); + } else { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(preference); + } + } + final Preference finalPref = preference; + + new PermissionApps(getContext(), group.getName(), new PermissionApps.Callback() { + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + if (getActivity() == null) { + return; + } + int granted = permissionApps.getGrantedCount(mLauncherPkgs); + int total = permissionApps.getTotalCount(mLauncherPkgs); + finalPref.setSummary(getString(R.string.app_permissions_group_summary, + granted, total)); + } + }, cache).refresh(false); + } + + if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 + && screen.findPreference(EXTRA_PREFS_KEY) == null) { + Preference extraScreenPreference = new Preference(context); + extraScreenPreference.setKey(EXTRA_PREFS_KEY); + extraScreenPreference.setIcon(Utils.applyTint(context, + R.drawable.ic_more_items, + android.R.attr.colorControlNormal)); + extraScreenPreference.setTitle(R.string.additional_permissions); + extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); + frag.setTargetFragment(ManagePermissionsFragment.this, 0); + FragmentTransaction ft = getFragmentManager().beginTransaction(); + ft.replace(android.R.id.content, frag); + ft.addToBackStack(null); + ft.commit(); + return true; + } + }); + int count = mExtraScreen.getPreferenceCount(); + extraScreenPreference.setSummary(getResources().getQuantityString( + R.plurals.additional_permissions_more, count, count)); + screen.addPreference(extraScreenPreference); + } + if (screen.getPreferenceCount() != 0) { + setLoading(false /* loading */, true /* animate */); + } + } + + public static class AdditionalPermissionsFragment extends PermissionsFrameFragment { + @Override + public void onCreate(Bundle icicle) { + setLoading(true /* loading */, false /* animate */); + super.onCreate(icicle); + getActivity().setTitle(R.string.additional_permissions); + setHasOptionsMenu(true); + } + + @Override + public void onDestroy() { + getActivity().setTitle(R.string.app_permissions); + super.onDestroy(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getFragmentManager().popBackStack(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindPermissionUi(getActivity(), getView()); + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java new file mode 100644 index 00000000..0f240bef --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/PermissionAppsFragment.java @@ -0,0 +1,435 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.v14.preference.SwitchPreference; +import android.support.v4.util.ArrayMap; +import android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceChangeListener; +import android.support.v7.preference.Preference.OnPreferenceClickListener; +import android.support.v7.preference.PreferenceScreen; +import android.util.ArraySet; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.DeviceUtils; +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.model.AppPermissionGroup; +import com.android.packageinstaller.permission.model.PermissionApps; +import com.android.packageinstaller.permission.model.PermissionApps.Callback; +import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp; +import com.android.packageinstaller.permission.ui.OverlayTouchActivity; +import com.android.packageinstaller.permission.utils.LocationUtils; +import com.android.packageinstaller.permission.utils.SafetyNetLogger; +import com.android.packageinstaller.permission.utils.Utils; + +import java.util.ArrayList; +import java.util.List; + +public final class PermissionAppsFragment extends PermissionsFrameFragment implements Callback, + OnPreferenceChangeListener { + + private static final int MENU_SHOW_SYSTEM = Menu.FIRST; + private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 1; + private static final String KEY_SHOW_SYSTEM_PREFS = "_showSystem"; + + public static PermissionAppsFragment newInstance(String permissionName) { + return setPermissionName(new PermissionAppsFragment(), permissionName); + } + + private static T setPermissionName(T fragment, String permissionName) { + Bundle arguments = new Bundle(); + arguments.putString(Intent.EXTRA_PERMISSION_NAME, permissionName); + fragment.setArguments(arguments); + return fragment; + } + + private PermissionApps mPermissionApps; + + private PreferenceScreen mExtraScreen; + + private ArrayMap mToggledGroups; + private ArraySet mLauncherPkgs; + private boolean mHasConfirmedRevoke; + + private boolean mShowSystem; + private MenuItem mShowSystemMenu; + private MenuItem mHideSystemMenu; + + private Callback mOnPermissionsLoadedListener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setLoading(true /* loading */, false /* animate */); + setHasOptionsMenu(true); + final ActionBar ab = getActivity().getActionBar(); + if (ab != null) { + ab.setDisplayHomeAsUpEnabled(true); + } + mLauncherPkgs = Utils.getLauncherPackages(getContext()); + + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + mPermissionApps = new PermissionApps(getActivity(), groupName, this); + mPermissionApps.refresh(true); + } + + @Override + public void onResume() { + super.onResume(); + mPermissionApps.refresh(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + mShowSystemMenu = menu.add(Menu.NONE, MENU_SHOW_SYSTEM, Menu.NONE, + R.string.menu_show_system); + mHideSystemMenu = menu.add(Menu.NONE, MENU_HIDE_SYSTEM, Menu.NONE, + R.string.menu_hide_system); + updateMenu(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + getActivity().finish(); + return true; + case MENU_SHOW_SYSTEM: + case MENU_HIDE_SYSTEM: + mShowSystem = item.getItemId() == MENU_SHOW_SYSTEM; + if (mPermissionApps.getApps() != null) { + onPermissionsLoaded(mPermissionApps); + } + updateMenu(); + break; + } + return super.onOptionsItemSelected(item); + } + + private void updateMenu() { + mShowSystemMenu.setVisible(!mShowSystem); + mHideSystemMenu.setVisible(mShowSystem); + } + + @Override + protected void onSetEmptyText(TextView textView) { + textView.setText(R.string.no_apps); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + bindUi(this, mPermissionApps); + } + + private static void bindUi(Fragment fragment, PermissionApps permissionApps) { + final Drawable icon = permissionApps.getIcon(); + final CharSequence label = permissionApps.getLabel(); + final ActionBar ab = fragment.getActivity().getActionBar(); + if (ab != null) { + ab.setTitle(fragment.getString(R.string.permission_title, label)); + } + + final ViewGroup rootView = (ViewGroup) fragment.getView(); + final ImageView iconView = (ImageView) rootView.findViewById(R.id.lb_icon); + if (iconView != null) { + // Set the icon as the background instead of the image because ImageView + // doesn't properly scale vector drawables beyond their intrinsic size + iconView.setBackground(icon); + } + final TextView titleView = (TextView) rootView.findViewById(R.id.lb_title); + if (titleView != null) { + titleView.setText(label); + } + final TextView breadcrumbView = (TextView) rootView.findViewById(R.id.lb_breadcrumb); + if (breadcrumbView != null) { + breadcrumbView.setText(R.string.app_permissions); + } + } + + private void setOnPermissionsLoadedListener(Callback callback) { + mOnPermissionsLoadedListener = callback; + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + Context context = getPreferenceManager().getContext(); + + if (context == null) { + return; + } + + boolean isTelevision = DeviceUtils.isTelevision(context); + PreferenceScreen screen = getPreferenceScreen(); + + ArraySet preferencesToRemove = new ArraySet<>(); + for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(screen.getPreference(i).getKey()); + } + if (mExtraScreen != null) { + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + preferencesToRemove.add(mExtraScreen.getPreference(i).getKey()); + } + } + + for (PermissionApp app : permissionApps.getApps()) { + if (!Utils.shouldShowPermission(app)) { + continue; + } + + String key = app.getKey(); + preferencesToRemove.remove(key); + Preference existingPref = screen.findPreference(key); + if (existingPref == null && mExtraScreen != null) { + existingPref = mExtraScreen.findPreference(key); + } + + boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs); + if (isSystemApp && !isTelevision && !mShowSystem) { + if (existingPref != null) { + screen.removePreference(existingPref); + } + continue; + } + + if (existingPref != null) { + // If existing preference - only update its state. + if (app.isPolicyFixed()) { + existingPref.setSummary(getString( + R.string.permission_summary_enforced_by_policy)); + } + existingPref.setPersistent(false); + existingPref.setEnabled(!app.isPolicyFixed()); + if (existingPref instanceof SwitchPreference) { + ((SwitchPreference) existingPref) + .setChecked(app.areRuntimePermissionsGranted()); + } + continue; + } + + SwitchPreference pref = new SwitchPreference(context); + pref.setOnPreferenceChangeListener(this); + pref.setKey(app.getKey()); + pref.setIcon(app.getIcon()); + pref.setTitle(app.getLabel()); + if (app.isPolicyFixed()) { + pref.setSummary(getString(R.string.permission_summary_enforced_by_policy)); + } + pref.setPersistent(false); + pref.setEnabled(!app.isPolicyFixed()); + pref.setChecked(app.areRuntimePermissionsGranted()); + + if (isSystemApp && isTelevision) { + if (mExtraScreen == null) { + mExtraScreen = getPreferenceManager().createPreferenceScreen(context); + } + mExtraScreen.addPreference(pref); + } else { + screen.addPreference(pref); + } + } + + if (mExtraScreen != null) { + preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS); + Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS); + + if (pref == null) { + pref = new Preference(context); + pref.setKey(KEY_SHOW_SYSTEM_PREFS); + pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, + android.R.attr.colorControlNormal)); + pref.setTitle(R.string.preference_show_system_apps); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + SystemAppsFragment frag = new SystemAppsFragment(); + setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME)); + frag.setTargetFragment(PermissionAppsFragment.this, 0); + getFragmentManager().beginTransaction() + .replace(android.R.id.content, frag) + .addToBackStack("SystemApps") + .commit(); + return true; + } + }); + screen.addPreference(pref); + } + + int grantedCount = 0; + for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) { + if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) { + grantedCount++; + } + } + pref.setSummary(getString(R.string.app_permissions_group_summary, + grantedCount, mExtraScreen.getPreferenceCount())); + } + + for (String key : preferencesToRemove) { + Preference pref = screen.findPreference(key); + if (pref != null) { + screen.removePreference(pref); + } else if (mExtraScreen != null) { + pref = mExtraScreen.findPreference(key); + if (pref != null) { + mExtraScreen.removePreference(pref); + } + } + } + + setLoading(false /* loading */, true /* animate */); + + if (mOnPermissionsLoadedListener != null) { + mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps); + } + } + + @Override + public boolean onPreferenceChange(final Preference preference, Object newValue) { + String pkg = preference.getKey(); + final PermissionApp app = mPermissionApps.getApp(pkg); + + if (app == null) { + return false; + } + + OverlayTouchActivity activity = (OverlayTouchActivity) getActivity(); + if (activity.isObscuredTouch()) { + activity.showOverlayDialog(); + return false; + } + + addToggledGroup(app.getPackageName(), app.getPermissionGroup()); + + if (LocationUtils.isLocationGroupAndProvider(mPermissionApps.getGroupName(), + app.getPackageName())) { + LocationUtils.showLocationDialog(getContext(), app.getLabel()); + return false; + } + if (newValue == Boolean.TRUE) { + app.grantRuntimePermissions(); + } else { + final boolean grantedByDefault = app.hasGrantedByDefaultPermissions(); + if (grantedByDefault || (!app.hasRuntimePermissions() && !mHasConfirmedRevoke)) { + new AlertDialog.Builder(getContext()) + .setMessage(grantedByDefault ? R.string.system_warning + : R.string.old_sdk_deny_warning) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.grant_dialog_button_deny, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((SwitchPreference) preference).setChecked(false); + app.revokeRuntimePermissions(); + if (!grantedByDefault) { + mHasConfirmedRevoke = true; + } + } + }) + .show(); + return false; + } else { + app.revokeRuntimePermissions(); + } + } + return true; + } + + @Override + public void onPause() { + super.onPause(); + logToggledGroups(); + } + + private void addToggledGroup(String packageName, AppPermissionGroup group) { + if (mToggledGroups == null) { + mToggledGroups = new ArrayMap<>(); + } + // Double toggle is back to initial state. + if (mToggledGroups.containsKey(packageName)) { + mToggledGroups.remove(packageName); + } else { + mToggledGroups.put(packageName, group); + } + } + + private void logToggledGroups() { + if (mToggledGroups != null) { + final int groupCount = mToggledGroups.size(); + for (int i = 0; i < groupCount; i++) { + String packageName = mToggledGroups.keyAt(i); + List groups = new ArrayList<>(); + groups.add(mToggledGroups.valueAt(i)); + SafetyNetLogger.logPermissionsToggled(packageName, groups); + } + mToggledGroups = null; + } + } + + public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback { + PermissionAppsFragment mOuterFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + mOuterFragment = (PermissionAppsFragment) getTargetFragment(); + setLoading(true /* loading */, false /* animate */); + super.onCreate(savedInstanceState); + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + if (mOuterFragment.mExtraScreen != null) { + setPreferenceScreen(); + } else { + mOuterFragment.setOnPermissionsLoadedListener(this); + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + String groupName = getArguments().getString(Intent.EXTRA_PERMISSION_NAME); + PermissionApps permissionApps = new PermissionApps(getActivity(), groupName, null); + bindUi(this, permissionApps); + } + + @Override + public void onPermissionsLoaded(PermissionApps permissionApps) { + setPreferenceScreen(); + mOuterFragment.setOnPermissionsLoadedListener(null); + } + + private void setPreferenceScreen() { + setPreferenceScreen(mOuterFragment.mExtraScreen); + setLoading(false /* loading */, true /* animate */); + } + } +} diff --git a/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java new file mode 100644 index 00000000..bc0e8457 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.annotation.Nullable; +import android.os.Bundle; +import android.support.v14.preference.PreferenceFragment; +import android.support.v17.leanback.widget.VerticalGridView; +import android.support.v7.preference.PreferenceScreen; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.RecyclerView.AdapterDataObserver; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.Animation.AnimationListener; +import android.view.animation.AnimationUtils; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +public abstract class PermissionsFrameFragment extends PreferenceFragment { + + private static final float WINDOW_ALIGNMENT_OFFSET_PERCENT = 50; + + private ViewGroup mPreferencesContainer; + + // TV-specific instance variables + @Nullable private VerticalGridView mGridView; + + private View mLoadingView; + private ViewGroup mPrefsView; + private boolean mIsLoading; + + /** + * Returns the view group that holds the preferences objects. This will + * only be set after {@link #onCreateView} has been called. + */ + protected final ViewGroup getPreferencesContainer() { + return mPreferencesContainer; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container, + false); + mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container); + if (mPrefsView == null) { + mPrefsView = rootView; + } + mLoadingView = rootView.findViewById(R.id.loading_container); + mPreferencesContainer = (ViewGroup) super.onCreateView( + inflater, mPrefsView, savedInstanceState); + setLoading(mIsLoading, false, true /* force */); + mPrefsView.addView(mPreferencesContainer); + return rootView; + } + + @Override + public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) { + PreferenceScreen preferences = getPreferenceScreen(); + if (preferences == null) { + preferences = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(preferences); + } + } + + protected void setLoading(boolean loading, boolean animate) { + setLoading(loading, animate, false); + } + + private void setLoading(boolean loading, boolean animate, boolean force) { + if (mIsLoading != loading || force) { + mIsLoading = loading; + if (getView() == null) { + // If there is no created view, there is no reason to animate. + animate = false; + } + if (mPrefsView != null) { + setViewShown(mPrefsView, !loading, animate); + } + if (mLoadingView != null) { + setViewShown(mLoadingView, loading, animate); + } + } + } + + private void setViewShown(final View view, boolean shown, boolean animate) { + if (animate) { + Animation animation = AnimationUtils.loadAnimation(getContext(), + shown ? android.R.anim.fade_in : android.R.anim.fade_out); + if (shown) { + view.setVisibility(View.VISIBLE); + } else { + animation.setAnimationListener(new AnimationListener() { + @Override + public void onAnimationStart(Animation animation) { + } + + @Override + public void onAnimationRepeat(Animation animation) { + } + + @Override + public void onAnimationEnd(Animation animation) { + view.setVisibility(View.INVISIBLE); + } + }); + } + view.startAnimation(animation); + } else { + view.clearAnimation(); + view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); + } + } + + @Override + public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, + Bundle savedInstanceState) { + if (Utils.isTelevision(getContext())) { + mGridView = (VerticalGridView) inflater.inflate( + R.layout.leanback_preferences_list, parent, false); + mGridView.setWindowAlignmentOffset(0); + mGridView.setWindowAlignmentOffsetPercent(WINDOW_ALIGNMENT_OFFSET_PERCENT); + mGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE); + mGridView.setFocusScrollStrategy(VerticalGridView.FOCUS_SCROLL_ALIGNED); + return mGridView; + } else { + return super.onCreateRecyclerView(inflater, parent, savedInstanceState); + } + } + + @Override + protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { + final RecyclerView.Adapter adapter = super.onCreateAdapter(preferenceScreen); + + if (adapter != null) { + final TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions); + onSetEmptyText(emptyView); + final RecyclerView recyclerView = getListView(); + adapter.registerAdapterDataObserver(new AdapterDataObserver() { + @Override + public void onChanged() { + checkEmpty(); + } + + @Override + public void onItemRangeInserted(int positionStart, int itemCount) { + checkEmpty(); + } + + @Override + public void onItemRangeRemoved(int positionStart, int itemCount) { + checkEmpty(); + } + + private void checkEmpty() { + boolean isEmpty = adapter.getItemCount() == 0; + emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); + recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE); + if (!isEmpty && mGridView != null) { + mGridView.requestFocus(); + } + } + }); + + boolean isEmpty = adapter.getItemCount() == 0; + emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); + recyclerView.setVisibility(isEmpty ? View.GONE : View.VISIBLE); + if (!isEmpty && mGridView != null) { + mGridView.requestFocus(); + } + } + + return adapter; + } + + /** + * Hook for subclasses to change the default text of the empty view. + * Base implementation leaves the default empty view text. + * + * @param textView the empty text view + */ + protected void onSetEmptyText(TextView textView) { + } +} + diff --git a/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java new file mode 100644 index 00000000..c7f5cda3 --- /dev/null +++ b/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 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.packageinstaller.permission.ui.television; + +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.packageinstaller.R; +import com.android.packageinstaller.permission.utils.Utils; + +public abstract class SettingsWithHeader extends PermissionsFrameFragment + implements OnClickListener { + + private View mHeader; + protected Intent mInfoIntent; + protected Drawable mIcon; + protected CharSequence mLabel; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); + + if (!Utils.isTelevision(getContext())) { + mHeader = inflater.inflate(R.layout.header, root, false); + getPreferencesContainer().addView(mHeader, 0); + updateHeader(); + } + + return root; + } + + public void setHeader(Drawable icon, CharSequence label, Intent infoIntent) { + mIcon = icon; + mLabel = label; + mInfoIntent = infoIntent; + updateHeader(); + } + + private void updateHeader() { + if (mHeader != null) { + final ImageView appIcon = (ImageView) mHeader.findViewById(R.id.icon); + appIcon.setImageDrawable(mIcon); + + final TextView appName = (TextView) mHeader.findViewById(R.id.name); + appName.setText(mLabel); + + final View info = mHeader.findViewById(R.id.info); + if (mInfoIntent == null) { + info.setVisibility(View.GONE); + } else { + info.setVisibility(View.VISIBLE); + info.setClickable(true); + info.setOnClickListener(this); + } + } + } + + @Override + public void onClick(View v) { + getActivity().startActivity(mInfoIntent); + } + +} diff --git a/src/com/android/packageinstaller/permission/utils/LocationUtils.java b/src/com/android/packageinstaller/permission/utils/LocationUtils.java index 512fcf44..0296ae80 100644 --- a/src/com/android/packageinstaller/permission/utils/LocationUtils.java +++ b/src/com/android/packageinstaller/permission/utils/LocationUtils.java @@ -36,23 +36,9 @@ public class LocationUtils { public static final String LOCATION_PERMISSION = Manifest.permission_group.LOCATION; - public static ArrayList getLocationProviders() { - ArrayList providers = new ArrayList<>(); - Resources res = Resources.getSystem(); - providers.add(res.getString( - com.android.internal.R.string.config_networkLocationProviderPackageName)); - - for (String provider : - res.getStringArray(com.android.internal.R.array.config_locationProviderPackageNames)) { - providers.add(provider); - } - - return providers; - } - public static void showLocationDialog(final Context context, CharSequence label) { new AlertDialog.Builder(context) - .setIcon(com.android.internal.R.drawable.ic_dialog_alert_material) + .setIcon(R.drawable.ic_dialog_alert_material) .setTitle(android.R.string.dialog_alert_title) .setMessage(context.getString(R.string.location_warning, label)) .setNegativeButton(R.string.ok, null) @@ -83,5 +69,4 @@ public class LocationUtils { return false; } } - } -- cgit v1.2.3 From 09370123353d8b925e644c05a9cf7434927e3ac9 Mon Sep 17 00:00:00 2001 From: Svetoslav Date: Fri, 20 Nov 2015 16:41:06 -0800 Subject: Fix build Change-Id: I2a3e235bb13f1920c14f6776ee3a1ef7285ea548 --- .../permission/ui/ManagePermissionsActivity.java | 10 +++------- .../permission/ui/handheld/PermissionAppsFragment.java | 3 ++- .../permission/ui/handheld/SettingsWithHeader.java | 3 ++- .../permission/ui/television/PermissionsFrameFragment.java | 3 ++- .../permission/ui/television/SettingsWithHeader.java | 3 ++- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java index a92b0ea4..38dbf8f5 100644 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java @@ -20,13 +20,9 @@ import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.util.Log; -import com.android.packageinstaller.permission.utils.Utils; -<<<<<<< HEAD import com.android.packageinstaller.permission.ui.wear.AppPermissionsFragmentWear; import com.android.packageinstaller.DeviceUtils; -======= ->>>>>>> c10abb25f3864e56b2a24ef3661511cd78921225 public final class ManagePermissionsActivity extends OverlayTouchActivity { private static final String LOG_TAG = "ManagePermissionsActivity"; @@ -44,7 +40,7 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { switch (action) { case Intent.ACTION_MANAGE_PERMISSIONS: { - if (Utils.isTelevision(this)) { + if (DeviceUtils.isTelevision(this)) { fragment = com.android.packageinstaller.permission.ui.television .ManagePermissionsFragment.newInstance(); } else { @@ -62,7 +58,7 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { } if (DeviceUtils.isWear(this)) { fragment = AppPermissionsFragmentWear.newInstance(packageName); - } else if (Utils.isTelevision(this)) { + } else if (DeviceUtils.isTelevision(this)) { fragment = com.android.packageinstaller.permission.ui.television .AppPermissionsFragment.newInstance(packageName); } else { @@ -78,7 +74,7 @@ public final class ManagePermissionsActivity extends OverlayTouchActivity { finish(); return; } - if (Utils.isTelevision(this)) { + if (DeviceUtils.isTelevision(this)) { fragment = com.android.packageinstaller.permission.ui.television .PermissionAppsFragment.newInstance(permissionName); } else { diff --git a/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java b/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java index 554830a7..eee2f716 100644 --- a/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java +++ b/src/com/android/packageinstaller/permission/ui/handheld/PermissionAppsFragment.java @@ -37,6 +37,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.model.AppPermissionGroup; import com.android.packageinstaller.permission.model.PermissionApps; @@ -179,7 +180,7 @@ public final class PermissionAppsFragment extends PermissionsFrameFragment imple return; } - boolean isTelevision = Utils.isTelevision(context); + boolean isTelevision = DeviceUtils.isTelevision(context); PreferenceScreen screen = getPreferenceScreen(); if (screen == null) { screen = getPreferenceManager().createPreferenceScreen(getActivity()); diff --git a/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java index acb3c61e..c15a4287 100644 --- a/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java +++ b/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithHeader.java @@ -26,6 +26,7 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; @@ -42,7 +43,7 @@ public abstract class SettingsWithHeader extends PermissionsFrameFragment Bundle savedInstanceState) { ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); - if (!Utils.isTelevision(getContext())) { + if (!DeviceUtils.isTelevision(getContext())) { mHeader = inflater.inflate(R.layout.header, root, false); getPreferencesContainer().addView(mHeader, 0); updateHeader(); diff --git a/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java b/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java index bc0e8457..e81aee86 100644 --- a/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java +++ b/src/com/android/packageinstaller/permission/ui/television/PermissionsFrameFragment.java @@ -31,6 +31,7 @@ import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; @@ -133,7 +134,7 @@ public abstract class PermissionsFrameFragment extends PreferenceFragment { @Override public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { - if (Utils.isTelevision(getContext())) { + if (DeviceUtils.isTelevision(getContext())) { mGridView = (VerticalGridView) inflater.inflate( R.layout.leanback_preferences_list, parent, false); mGridView.setWindowAlignmentOffset(0); diff --git a/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java b/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java index c7f5cda3..4dae629c 100644 --- a/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java +++ b/src/com/android/packageinstaller/permission/ui/television/SettingsWithHeader.java @@ -26,6 +26,7 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import com.android.packageinstaller.DeviceUtils; import com.android.packageinstaller.R; import com.android.packageinstaller.permission.utils.Utils; @@ -42,7 +43,7 @@ public abstract class SettingsWithHeader extends PermissionsFrameFragment Bundle savedInstanceState) { ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); - if (!Utils.isTelevision(getContext())) { + if (!DeviceUtils.isTelevision(getContext())) { mHeader = inflater.inflate(R.layout.header, root, false); getPreferencesContainer().addView(mHeader, 0); updateHeader(); -- cgit v1.2.3 From 741ac03b4367c06d33190b80e2f6a6003a803517 Mon Sep 17 00:00:00 2001 From: Sharvil Nanavati Date: Sat, 21 Nov 2015 00:06:33 -0800 Subject: Fix build break. Bug: 25822682 Change-Id: Ib5b40f014a8b6d0fb756d7f4c4802c9e8dfa7a44 --- .../packageinstaller/permission/ui/ManagePermissionsActivity.java | 6 +----- src/com/android/packageinstaller/permission/utils/Utils.java | 4 ++++ 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java index a92b0ea4..fad35e17 100644 --- a/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java +++ b/src/com/android/packageinstaller/permission/ui/ManagePermissionsActivity.java @@ -20,13 +20,9 @@ import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.util.Log; -import com.android.packageinstaller.permission.utils.Utils; -<<<<<<< HEAD - import com.android.packageinstaller.permission.ui.wear.AppPermissionsFragmentWear; +import com.android.packageinstaller.permission.utils.Utils; import com.android.packageinstaller.DeviceUtils; -======= ->>>>>>> c10abb25f3864e56b2a24ef3661511cd78921225 public final class ManagePermissionsActivity extends OverlayTouchActivity { private static final String LOG_TAG = "ManagePermissionsActivity"; diff --git a/src/com/android/packageinstaller/permission/utils/Utils.java b/src/com/android/packageinstaller/permission/utils/Utils.java index 21830378..b8a79d54 100644 --- a/src/com/android/packageinstaller/permission/utils/Utils.java +++ b/src/com/android/packageinstaller/permission/utils/Utils.java @@ -146,4 +146,8 @@ public class Utils { return info.isSystemApp() && (info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 && !launcherPkgs.contains(info.packageName); } + + public static boolean isTelevision(Object o) { + return false; + } } -- cgit v1.2.3 From cfc6b97c7d4dbaf7a50433a0f459ee4e25725e82 Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Fri, 4 Dec 2015 13:30:49 -0800 Subject: Fix wake lock leak Wake lock was not being released when installation failed due to early bail out call. I wrapped the calls into a try/finally to ensure it gets released. Code was tested by tweaking the code to force a failure and test installing/uninstalling apps. BUG: 25814793 Change-Id: I2e81e5d76edcfb601ce734cf571705e979c51f32 --- .../wear/WearPackageInstallerService.java | 59 ++++++++++++---------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java index 6dc5aa70..229a3df6 100644 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -586,31 +586,33 @@ public class WearPackageInstallerService extends Service { } public void packageInstalled(String packageName, int returnCode) { - // If installation failed, bail out and remove the ShowPermsStore entry - if (returnCode < 0) { - Log.e(TAG, "Package install failed " + mApplicationPackageName - + ", returnCode " + returnCode); - WearPackageUtil.removeFromPermStore(mContext, mApplicationPackageName); - return; - } + try { + // If installation failed, bail out and remove the ShowPermsStore entry + if (returnCode < 0) { + Log.e(TAG, "Package install failed " + mApplicationPackageName + + ", returnCode " + returnCode); + WearPackageUtil.removeFromPermStore(mContext, mApplicationPackageName); + return; + } - Log.i(TAG, "Package " + packageName + " was installed."); + Log.i(TAG, "Package " + packageName + " was installed."); - // Delete tempFile from the file system. - File tempFile = WearPackageUtil.getTemporaryFile(mContext, packageName); - if (tempFile != null) { - tempFile.delete(); - } + // Delete tempFile from the file system. + File tempFile = WearPackageUtil.getTemporaryFile(mContext, packageName); + if (tempFile != null) { + tempFile.delete(); + } - // Broadcast the "UPDATED" gmscore intent, normally sent by play store. - // TODO: Remove this broadcast if/when we get the play store to do this for us. - if (GMS_PACKAGE_NAME.equals(packageName)) { - Intent gmsInstalledIntent = new Intent(GMS_UPDATED_BROADCAST); - gmsInstalledIntent.setPackage(GMS_PACKAGE_NAME); - mContext.sendBroadcast(gmsInstalledIntent); + // Broadcast the "UPDATED" gmscore intent, normally sent by play store. + // TODO: Remove this broadcast if/when we get the play store to do this for us. + if (GMS_PACKAGE_NAME.equals(packageName)) { + Intent gmsInstalledIntent = new Intent(GMS_UPDATED_BROADCAST); + gmsInstalledIntent.setPackage(GMS_PACKAGE_NAME); + mContext.sendBroadcast(gmsInstalledIntent); + } + } finally { + finishService(mWakeLock, mStartId); } - - finishService(mWakeLock, mStartId); } } @@ -624,13 +626,16 @@ public class WearPackageInstallerService extends Service { } public void packageDeleted(String packageName, int returnCode) { - if (returnCode >= 0) { - Log.i(TAG, "Package " + packageName + " was uninstalled."); - } else { - Log.e(TAG, "Package uninstall failed " + packageName + ", returnCode " + - returnCode); + try { + if (returnCode >= 0) { + Log.i(TAG, "Package " + packageName + " was uninstalled."); + } else { + Log.e(TAG, "Package uninstall failed " + packageName + ", returnCode " + + returnCode); + } + } finally { + finishService(mWakeLock, mStartId); } - finishService(mWakeLock, mStartId); } } } -- cgit v1.2.3 From b3d46fff6958c7a74f1ab2082f932dd98c5cdc19 Mon Sep 17 00:00:00 2001 From: Vinod Krishnan Date: Wed, 2 Dec 2015 17:34:10 -0800 Subject: [PkgInstaller] Refactoring arguments of WearPackageInstaller - Creating a WearPackageArgs object Change-Id: I9616fe5e6621856e5d07da5cee205e7162dad451 --- .../packageinstaller/wear/WearPackageArgs.java | 92 ++++++++++++++++++++++ .../wear/WearPackageInstallerService.java | 91 ++++++--------------- 2 files changed, 118 insertions(+), 65 deletions(-) create mode 100644 src/com/android/packageinstaller/wear/WearPackageArgs.java (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/wear/WearPackageArgs.java b/src/com/android/packageinstaller/wear/WearPackageArgs.java new file mode 100644 index 00000000..67051da0 --- /dev/null +++ b/src/com/android/packageinstaller/wear/WearPackageArgs.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2015 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.packageinstaller.wear; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; + +/** + * Installation Util that contains a list of parameters that are needed for + * installing/uninstalling. + */ +public class WearPackageArgs { + private static final String KEY_ASSET_URI = + "com.google.android.clockwork.EXTRA_ASSET_URI"; + private static final String KEY_START_ID = + "com.google.android.clockwork.EXTRA_START_ID"; + private static final String KEY_PERM_URI = + "com.google.android.clockwork.EXTRA_PERM_URI"; + private static final String KEY_CHECK_PERMS = + "com.google.android.clockwork.EXTRA_CHECK_PERMS"; + private static final String KEY_SKIP_IF_SAME_VERSION = + "com.google.android.clockwork.EXTRA_SKIP_IF_SAME_VERSION"; + private static final String KEY_COMPRESSION_ALG = + "com.google.android.clockwork.EXTRA_KEY_COMPRESSION_ALG"; + private static final String KEY_COMPANION_SDK_VERSION = + "com.google.android.clockwork.EXTRA_KEY_COMPANION_SDK_VERSION"; + private static final String KEY_COMPANION_DEVICE_VERSION = + "com.google.android.clockwork.EXTRA_KEY_COMPANION_DEVICE_VERSION"; + private static final String KEY_SHOULD_CHECK_GMS_DEPENDENCY = + "com.google.android.clockwork.EXTRA_KEY_SHOULD_CHECK_GMS_DEPENDENCY"; + + public static String getPackageName(Bundle b) { + return b.getString(Intent.EXTRA_INSTALLER_PACKAGE_NAME); + } + + public static Uri getAssetUri(Bundle b) { + return b.getParcelable(KEY_ASSET_URI); + } + + public static Bundle setAssetUri(Bundle b, Uri assetUri) { + b.putParcelable(KEY_ASSET_URI, assetUri); + return b; + } + + public static Uri getPermUri(Bundle b) { + return b.getParcelable(KEY_PERM_URI); + } + + public static boolean checkPerms(Bundle b) { + return b.getBoolean(KEY_CHECK_PERMS); + } + + public static boolean skipIfSameVersion(Bundle b) { + return b.getBoolean(KEY_SKIP_IF_SAME_VERSION); + } + + public static int getCompanionSdkVersion(Bundle b) { + return b.getInt(KEY_COMPANION_SDK_VERSION); + } + + public static int getCompanionDeviceVersion(Bundle b) { + return b.getInt(KEY_COMPANION_DEVICE_VERSION); + } + + public static String getCompressionAlg(Bundle b) { + return b.getString(KEY_COMPRESSION_ALG); + } + + public static int getStartId(Bundle b) { + return b.getInt(KEY_START_ID); + } + + public static Bundle setStartId(Bundle b, int startId) { + b.putInt(KEY_START_ID, startId); + return b; + } +} diff --git a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java index 229a3df6..3874c0a4 100644 --- a/src/com/android/packageinstaller/wear/WearPackageInstallerService.java +++ b/src/com/android/packageinstaller/wear/WearPackageInstallerService.java @@ -16,7 +16,6 @@ package com.android.packageinstaller.wear; -import android.annotation.Nullable; import android.app.Service; import android.content.ComponentName; import android.content.Context; @@ -81,19 +80,6 @@ import java.util.Set; public class WearPackageInstallerService extends Service { private static final String TAG = "WearPkgInstallerService"; - private static final String KEY_PERM_URI = - "com.google.android.clockwork.EXTRA_PERM_URI"; - private static final String KEY_CHECK_PERMS = - "com.google.android.clockwork.EXTRA_CHECK_PERMS"; - private static final String KEY_SKIP_IF_SAME_VERSION = - "com.google.android.clockwork.EXTRA_SKIP_IF_SAME_VERSION"; - private static final String KEY_COMPRESSION_ALG = - "com.google.android.clockwork.EXTRA_KEY_COMPRESSION_ALG"; - private static final String KEY_COMPANION_SDK_VERSION = - "com.google.android.clockwork.EXTRA_KEY_COMPANION_SDK_VERSION"; - private static final String KEY_COMPANION_DEVICE_VERSION = - "com.google.android.clockwork.EXTRA_KEY_COMPANION_DEVICE_VERSION"; - private static final String KEY_PACKAGE_NAME = "com.google.android.clockwork.EXTRA_PACKAGE_NAME"; private static final String KEY_APP_LABEL = "com.google.android.clockwork.EXTRA_APP_LABEL"; @@ -107,16 +93,6 @@ public class WearPackageInstallerService extends Service { private static final String SHOW_PERMS_SERVICE_CLASS = "com.google.android.clockwork.packagemanager.ShowPermsService"; - private static final String ASSET_URI_ARG = "assetUri"; - private static final String PACKAGE_NAME_ARG = "packageName"; - private static final String PERM_URI_ARG = "permUri"; - private static final String START_ID_ARG = "startId"; - private static final String CHECK_PERMS_ARG = "checkPerms"; - private static final String SKIP_IF_SAME_VERSION_ARG = "skipIfSameVersion"; - private static final String COMPRESSION_ALG = "compressionAlg"; - private static final String COMPANION_SDK_VERSION = "companionSdkVersion"; - private static final String COMPANION_DEVICE_VERSION = "companionDeviceVersion"; - /** * Normally sent by the Play store (See http://go/playstore-gms_updated), we instead * broadcast, ourselves. http://b/17387718 @@ -135,19 +111,10 @@ public class WearPackageInstallerService extends Service { public void handleMessage(Message msg) { switch (msg.what) { case START_INSTALL: - installPackage(msg.getData().getString(PACKAGE_NAME_ARG), - (Uri) msg.getData().getParcelable(ASSET_URI_ARG), - (Uri) msg.getData().getParcelable(PERM_URI_ARG), - msg.getData().getInt(START_ID_ARG), - msg.getData().getBoolean(CHECK_PERMS_ARG), - msg.getData().getBoolean(SKIP_IF_SAME_VERSION_ARG), - msg.getData().getString(COMPRESSION_ALG), - msg.getData().getInt(COMPANION_SDK_VERSION), - msg.getData().getInt(COMPANION_DEVICE_VERSION)); + installPackage(msg.getData()); break; case START_UNINSTALL: - uninstallPackage(msg.getData().getString(PACKAGE_NAME_ARG), - msg.getData().getInt(START_ID_ARG)); + uninstallPackage(msg.getData()); break; } } @@ -185,45 +152,35 @@ public class WearPackageInstallerService extends Service { Log.d(TAG, "Got install/uninstall request " + intent); } if (intent != null) { + Bundle intentBundle = intent.getExtras(); + WearPackageArgs.setStartId(intentBundle, startId); if (Intent.ACTION_INSTALL_PACKAGE.equals(intent.getAction())) { final Message msg = mServiceHandler.obtainMessage(START_INSTALL); - final Bundle startInstallArgs = new Bundle(); - startInstallArgs.putParcelable(ASSET_URI_ARG, intent.getData()); - startInstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( - Intent.EXTRA_INSTALLER_PACKAGE_NAME)); - startInstallArgs.putInt(START_ID_ARG, startId); - Uri permUri = intent.getParcelableExtra(KEY_PERM_URI); - startInstallArgs.putParcelable(PERM_URI_ARG, permUri); - startInstallArgs.putBoolean(CHECK_PERMS_ARG, - intent.getBooleanExtra(KEY_CHECK_PERMS, true)); - startInstallArgs.putBoolean(SKIP_IF_SAME_VERSION_ARG, - intent.getBooleanExtra(KEY_SKIP_IF_SAME_VERSION, false)); - startInstallArgs.putString(COMPRESSION_ALG, - intent.getStringExtra(KEY_COMPRESSION_ALG)); - startInstallArgs.putInt(COMPANION_SDK_VERSION, - intent.getIntExtra(KEY_COMPANION_SDK_VERSION, 0)); - startInstallArgs.putInt(COMPANION_DEVICE_VERSION, - intent.getIntExtra(KEY_COMPANION_DEVICE_VERSION, 0)); - msg.setData(startInstallArgs); + WearPackageArgs.setAssetUri(intentBundle, intent.getData()); + msg.setData(intentBundle); mServiceHandler.sendMessage(msg); } else if (Intent.ACTION_UNINSTALL_PACKAGE.equals(intent.getAction())) { Message msg = mServiceHandler.obtainMessage(START_UNINSTALL); - Bundle startUninstallArgs = new Bundle(); - startUninstallArgs.putString(PACKAGE_NAME_ARG, intent.getStringExtra( - Intent.EXTRA_INSTALLER_PACKAGE_NAME)); - startUninstallArgs.putInt(START_ID_ARG, startId); - msg.setData(startUninstallArgs); + msg.setData(intentBundle); mServiceHandler.sendMessage(msg); } } return START_NOT_STICKY; } - private void installPackage(String packageName, Uri packageUri, Uri permUri, int startId, - boolean checkPerms, boolean skipIfSameVersion, @Nullable String compressionAlg, - int companionSdkVersion, int companionDeviceVersion) { + private void installPackage(Bundle argsBundle) { + int startId = WearPackageArgs.getStartId(argsBundle); + final String packageName = WearPackageArgs.getPackageName(argsBundle); + final Uri assetUri = WearPackageArgs.getAssetUri(argsBundle); + final Uri permUri = WearPackageArgs.getPermUri(argsBundle); + boolean checkPerms = WearPackageArgs.checkPerms(argsBundle); + boolean skipIfSameVersion = WearPackageArgs.skipIfSameVersion(argsBundle); + int companionSdkVersion = WearPackageArgs.getCompanionSdkVersion(argsBundle); + int companionDeviceVersion = WearPackageArgs.getCompanionDeviceVersion(argsBundle); + String compressionAlg = WearPackageArgs.getCompressionAlg(argsBundle); + if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Installing package: " + packageName + ", packageUri: " + packageUri + + Log.d(TAG, "Installing package: " + packageName + ", assetUri: " + assetUri + ",permUri: " + permUri + ", startId: " + startId + ", checkPerms: " + checkPerms + ", skipIfSameVersion: " + skipIfSameVersion + ", compressionAlg: " + compressionAlg + ", companionSdkVersion: " + @@ -252,7 +209,7 @@ public class WearPackageInstallerService extends Service { } } ParcelFileDescriptor parcelFd = getContentResolver() - .openFileDescriptor(packageUri, "r"); + .openFileDescriptor(assetUri, "r"); tempFile = WearPackageUtil.getFileFromFd(WearPackageInstallerService.this, parcelFd, packageName, compressionAlg); if (tempFile == null) { @@ -339,10 +296,11 @@ public class WearPackageInstallerService extends Service { pm.installPackage(Uri.fromFile(tempFile), new PackageInstallObserver(this, lock, startId, packageName), installFlags, packageName); + messageSent = true; Log.i(TAG, "Sent installation request for " + packageName); } catch (FileNotFoundException e) { - Log.e(TAG, "Could not find the file with URI " + packageUri, e); + Log.e(TAG, "Could not find the file with URI " + assetUri, e); } finally { if (!messageSent) { // Some error happened. If the message has been sent, we can wait for the observer @@ -355,7 +313,10 @@ public class WearPackageInstallerService extends Service { } } - private void uninstallPackage(String packageName, int startId) { + private void uninstallPackage(Bundle argsBundle) { + int startId = WearPackageArgs.getStartId(argsBundle); + final String packageName = WearPackageArgs.getPackageName(argsBundle); + final PackageManager pm = getPackageManager(); PowerManager.WakeLock lock = getLock(this.getApplicationContext()); pm.deletePackage(packageName, new PackageDeleteObserver(lock, startId), -- cgit v1.2.3 From 58045ad49af12b202b08a4f054a54e2774ca48f1 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Tue, 24 Nov 2015 11:52:03 -0800 Subject: Always have an icon The permission icon can be an external icon and thus might not exist. So, ensure we always have an icon for the permissions UI. Bug: 25602523 Change-Id: Ibdb1f296ff8c5f3664fe8056c8637844b580cce9 --- .../android/packageinstaller/permission/model/PermissionGroups.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/packageinstaller/permission/model/PermissionGroups.java b/src/com/android/packageinstaller/permission/model/PermissionGroups.java index 59eba856..c496e898 100644 --- a/src/com/android/packageinstaller/permission/model/PermissionGroups.java +++ b/src/com/android/packageinstaller/permission/model/PermissionGroups.java @@ -212,11 +212,12 @@ public final class PermissionGroups implements LoaderCallbacks 0) { icon = Utils.loadDrawable(getContext().getPackageManager(), itemInfo.packageName, itemInfo.icon); - } else { + } + if (icon == null) { icon = getContext().getDrawable(R.drawable.ic_perm_device_info); } return icon; -- cgit v1.2.3