diff options
author | Erik Wolsheimer <ewol@google.com> | 2015-09-16 06:18:50 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-09-16 06:18:50 +0000 |
commit | fd755fe714ad8a29fac2ac7c1db1bbb65b28ef6d (patch) | |
tree | d9c414a738759a89ebacd69f45b763776daeb32b /src | |
parent | 982f4b072b2e74ecc06fbc0eeb69175999f0134c (diff) | |
parent | df4d4542c2cf2fa0a7539412af2a99d60b1c2a15 (diff) | |
download | android_packages_apps_PackageInstaller-fd755fe714ad8a29fac2ac7c1db1bbb65b28ef6d.tar.gz android_packages_apps_PackageInstaller-fd755fe714ad8a29fac2ac7c1db1bbb65b28ef6d.tar.bz2 android_packages_apps_PackageInstaller-fd755fe714ad8a29fac2ac7c1db1bbb65b28ef6d.zip |
Merge "Build initial Permissions Settings UI page for Wear" into cw-e-dev
Diffstat (limited to 'src')
6 files changed, 599 insertions, 2 deletions
diff --git a/src/android/support/wearable/view/CircledImageView.java b/src/android/support/wearable/view/CircledImageView.java index de95c548..53cb78cf 100644 --- a/src/android/support/wearable/view/CircledImageView.java +++ b/src/android/support/wearable/view/CircledImageView.java @@ -37,6 +37,7 @@ import android.util.AttributeSet; import android.view.View; import java.util.Objects; +import com.android.packageinstaller.R; import com.android.packageinstaller.R; 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<AppPermissionGroup> mToggledGroups; + private AppPermissions mAppPermissions; + private PermissionsSettingsAdapter mAdapter; + + private boolean mHasConfirmedRevoke; + + public static AppPermissionsFragmentWear newInstance(String packageName) { + return setPackageName(new AppPermissionsFragmentWear(), packageName); + } + + private static <T extends Fragment> 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<AppPermissionGroup> 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<AppPermissionGroup> setting = + new SettingsAdapter.Setting<AppPermissionGroup>( + 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<AppPermissionGroup> 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<AppPermissionGroup> 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<AppPermissionGroup> { + 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<T> 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); |