Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd755fe7 authored by Erik Wolsheimer's avatar Erik Wolsheimer Committed by Android (Google) Code Review
Browse files

Merge "Build initial Permissions Settings UI page for Wear" into cw-e-dev

parents 982f4b07 df4d4542
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <android.support.wearable.view.CircledImageView
        android:id="@+id/image"
        android:alpha="0.5"
        android:layout_height="52dp"
        android:layout_width="52dp"
        android:layout_gravity="center_vertical"
        app:circle_border_color="#FFFFFFFF"
        app:circle_border_width="1dp"
        app:circle_color="#00000000"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/settings_text_margin_left"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:alpha="0.5"
            android:fontFamily="sans-serif-condensed-light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp"
            android:maxLines="2"
            android:ellipsize="end"/>
        <TextView
            android:id="@+id/state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#FFFFFFFF"
            android:fontFamily="sans-serif-condensed-light"
            android:maxLines="1"
            android:ellipsize="end"/>
    </LinearLayout>

</LinearLayout>
+1 −0
Original line number Diff line number Diff line
@@ -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;

+9 −1
Original line number Diff line number Diff line
@@ -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;
                }

                if (DeviceUtils.isWatch(this)) {
                    fragment = AppPermissionsFragmentWear.newInstance(packageName);
                } else {
                    fragment = AppPermissionsFragment.newInstance(packageName);
                }
            } break;

            case Intent.ACTION_MANAGE_PERMISSION_APPS: {
+262 −0
Original line number Diff line number Diff line
/*
* 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();
    }
}
+241 −0
Original line number Diff line number Diff line
/*
 * 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);
    }


}
Loading