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

Commit ccb37bb8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactor the OpenSupportedLinks" into rvc-dev

parents b5a0ff7e 44b74c9c
Loading
Loading
Loading
Loading
+3 −11
Original line number Diff line number Diff line
@@ -18,17 +18,9 @@
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/app_launch_open_domain_urls_title">

    <com.android.settingslib.widget.LayoutPreference
        android:key="app_header_view"
        android:layout="@layout/settings_entity_header"
        android:selectable="false"
        settings:controller="com.android.settings.applications.AppHeaderPreferenceController"
        settings:allowDividerBelow="true"/>

    <PreferenceCategory
        android:key="app_launch_allow_app_to_open_supported_links"
        android:title="@string/app_link_open_always"
        settings:controller="com.android.settings.applications.AppOpenSupportedLinksPreferenceController">
        android:key="supported_links_radio_group"
        android:title="@string/app_link_open_always">
    </PreferenceCategory>

    <com.android.settingslib.widget.FooterPreference
+0 −106
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.applications;

import static com.android.settings.widget.EntityHeaderController.ActionType;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.widget.LayoutPreference;

/**
 *  The header controller displays on the top of the page.
 */
public class AppHeaderPreferenceController extends BasePreferenceController implements
        LifecycleObserver, OnResume {
    private DashboardFragment mParent;
    private PackageInfo mPackageInfo;
    private Lifecycle mLifecycle;
    private LayoutPreference mHeaderPreference;

    public AppHeaderPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    /**
     * @param fragment set the parent fragment.
     * @return return controller-self.
     */
    public AppHeaderPreferenceController setParentFragment(DashboardFragment fragment) {
        mParent = fragment;
        return this;
    }

    /**
     * @param packageInfo set the {@link PackageInfo}.
     * @return return controller-self.
     */
    public AppHeaderPreferenceController setPackageInfo(PackageInfo packageInfo) {
        mPackageInfo = packageInfo;
        return this;
    }

    /**
     * @param lifeCycle set the {@link Lifecycle}.
     * @return return controller-self.
     */
    public AppHeaderPreferenceController setLifeCycle(Lifecycle lifeCycle) {
        mLifecycle = lifeCycle;
        return this;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mHeaderPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public void onResume() {
        final Activity activity = mParent.getActivity();
        final PackageManager packageManager = activity.getPackageManager();
        EntityHeaderController
                .newInstance(activity, mParent, mHeaderPreference.findViewById(R.id.entity_header))
                .setRecyclerView(mParent.getListView(), mLifecycle)
                .setIcon(Utils.getBadgedIcon(mParent.getContext(), mPackageInfo.applicationInfo))
                .setLabel(mPackageInfo.applicationInfo.loadLabel(packageManager))
                .setSummary(mPackageInfo)
                .setIsInstantApp(AppUtils.isInstant(mPackageInfo.applicationInfo))
                .setPackageName(mPackageInfo.packageName)
                .setUid(mPackageInfo.applicationInfo.uid)
                .setButtonActions(ActionType.ACTION_NONE, ActionType.ACTION_NONE)
                .done(mParent.getActivity(), true /* rebindActions */);
    }
}
+2 −5
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

@@ -50,8 +49,6 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
    private static final String FRAGMENT_OPEN_SUPPORTED_LINKS =
            "com.android.settings.applications.OpenSupportedLinks";

    public static final String KEY_PACKAGE_INFO = "pkg_info";

    private static final Intent sBrowserIntent;

    static {
@@ -79,7 +76,8 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
        mAppLinkState = findPreference(KEY_APP_LINK_STATE);
        mAppLinkState.setOnPreferenceClickListener(preference -> {
            final Bundle args = new Bundle();
            args.putParcelable(KEY_PACKAGE_INFO, this.mPackageInfo);
            args.putString(ARG_PACKAGE_NAME, mPackageName);
            args.putInt(ARG_PACKAGE_UID, mUserId);

            new SubSettingLauncher(this.getContext())
                    .setDestination(FRAGMENT_OPEN_SUPPORTED_LINKS)
@@ -145,7 +143,6 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
    private void setAppLinkStateSummary() {
        final int state = mPm.getIntentVerificationStatusAsUser(mPackageName,
                UserHandle.myUserId());
        Log.d("[sunny]", "setAppLinkStateSummary+ state=" + state);
        mAppLinkState.setSummary(linkStateToResourceId(state));
    }

+0 −184
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.applications;

import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.RadioButtonPreference;

/**
 *  The radio group controller supports users to choose what kind supported links they need.
 */
public class AppOpenSupportedLinksPreferenceController extends BasePreferenceController
        implements RadioButtonPreference.OnClickListener {
    private static final String TAG = "OpenLinksPrefCtrl";
    private static final String KEY_LINK_OPEN_ALWAYS = "app_link_open_always";
    private static final String KEY_LINK_OPEN_ASK = "app_link_open_ask";
    private static final String KEY_LINK_OPEN_NEVER = "app_link_open_never";

    private Context mContext;
    private PackageManager mPackageManager;
    private String mPackageName;
    private int mCurrentIndex;
    private PreferenceCategory mPreferenceCategory;
    private String[] mRadioKeys = {KEY_LINK_OPEN_ALWAYS, KEY_LINK_OPEN_ASK, KEY_LINK_OPEN_NEVER};

    @VisibleForTesting
    RadioButtonPreference mAllowOpening;
    @VisibleForTesting
    RadioButtonPreference mAskEveryTime;
    @VisibleForTesting
    RadioButtonPreference mNotAllowed;

    public AppOpenSupportedLinksPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
        mContext = context;
        mPackageManager = context.getPackageManager();
    }

    /**
     * @param pkg selected package name.
     * @return return controller-self.
     */
    public AppOpenSupportedLinksPreferenceController setInit(String pkg) {
        mPackageName = pkg;
        return this;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreferenceCategory = screen.findPreference(getPreferenceKey());
        mAllowOpening = makeRadioPreference(KEY_LINK_OPEN_ALWAYS, R.string.app_link_open_always);
        final int entriesNo = getEntriesNo();
        //This to avoid the summary line wrap
        mAllowOpening.setAppendixVisibility(View.GONE);
        mAllowOpening.setSummary(
                mContext.getResources().getQuantityString(R.plurals.app_link_open_always_summary,
                        entriesNo, entriesNo));
        mAskEveryTime = makeRadioPreference(KEY_LINK_OPEN_ASK, R.string.app_link_open_ask);
        mNotAllowed = makeRadioPreference(KEY_LINK_OPEN_NEVER, R.string.app_link_open_never);

        final int state = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
                UserHandle.myUserId());
        mCurrentIndex = linkStateToIndex(state);
        setRadioStatus(mCurrentIndex);
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public void onRadioButtonClicked(RadioButtonPreference preference) {
        final int clickedIndex = preferenceKeyToIndex(preference.getKey());
        if (mCurrentIndex != clickedIndex) {
            mCurrentIndex = clickedIndex;
            setRadioStatus(mCurrentIndex);
            updateAppLinkState(indexToLinkState(mCurrentIndex));
        }
    }

    private RadioButtonPreference makeRadioPreference(String key, int resourceId) {
        RadioButtonPreference pref = new RadioButtonPreference(mPreferenceCategory.getContext());
        pref.setKey(key);
        pref.setTitle(resourceId);
        pref.setOnClickListener(this);
        mPreferenceCategory.addPreference(pref);
        return pref;
    }

    private int linkStateToIndex(int state) {
        switch (state) {
            case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS:
                return 0; // Always
            case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER:
                return 2; // Never
            default:
                return 1; // Ask
        }
    }

    private int indexToLinkState(int index) {
        switch (index) {
            case 0:
                return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
            case 2:
                return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
            default:
                return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
        }
    }

    private int preferenceKeyToIndex(String key) {
        for (int i = 0; i < mRadioKeys.length; i++) {
            if (TextUtils.equals(key, mRadioKeys[i])) {
                return i;
            }
        }
        return 1; // Ask
    }

    private void setRadioStatus(int index) {
        mAllowOpening.setChecked(index == 0 ? true : false);
        mAskEveryTime.setChecked(index == 1 ? true : false);
        mNotAllowed.setChecked(index == 2 ? true : false);
    }

    private boolean updateAppLinkState(final int newState) {
        final int userId = UserHandle.myUserId();
        final int priorState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
                userId);

        if (priorState == newState) {
            return false;
        }

        boolean success = mPackageManager.updateIntentVerificationStatusAsUser(mPackageName,
                newState, userId);
        if (success) {
            // Read back the state to see if the change worked
            final int updatedState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
                    userId);
            success = (newState == updatedState);
        } else {
            Log.e(TAG, "Couldn't update intent verification status!");
        }
        return success;
    }

    @VisibleForTesting
    int getEntriesNo() {
        return Utils.getHandledDomains(mPackageManager, mPackageName).size();
    }
}
+157 −35
Original line number Diff line number Diff line
@@ -15,75 +15,187 @@
 */
package com.android.settings.applications;

import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;

import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceCategory;

import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.widget.RadioButtonPreference;

/**
 * Display the Open Supported Links page. Allow users choose what kind supported links they need.
 */
public class OpenSupportedLinks extends DashboardFragment {
public class OpenSupportedLinks extends AppInfoWithHeader implements
        RadioButtonPreference.OnClickListener {
    private static final String TAG = "OpenSupportedLinks";
    private static final String RADIO_GROUP_KEY = "supported_links_radio_group";
    private static final String FOOTER_KEY = "supported_links_footer";
    private static final String KEY_LINK_OPEN_ALWAYS = "app_link_open_always";
    private static final String KEY_LINK_OPEN_ASK = "app_link_open_ask";
    private static final String KEY_LINK_OPEN_NEVER = "app_link_open_never";

    private static final int ALLOW_ALWAYS_OPENING = 0;
    private static final int ASK_EVERY_TIME = 1;
    private static final int NOT_ALLOWED_OPENING = 2;

    private int mCurrentIndex;
    private String[] mRadioKeys = {KEY_LINK_OPEN_ALWAYS, KEY_LINK_OPEN_ASK, KEY_LINK_OPEN_NEVER};

    @VisibleForTesting
    PackageManager mPackageManager;
    @VisibleForTesting
    PackageInfo mPackageInfo;
    PreferenceCategory mPreferenceCategory;
    @VisibleForTesting
    RadioButtonPreference mAllowOpening;
    @VisibleForTesting
    RadioButtonPreference mAskEveryTime;
    @VisibleForTesting
    RadioButtonPreference mNotAllowed;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        final Bundle args = getArguments();
        mPackageInfo = (args != null) ? args.getParcelable(AppLaunchSettings.KEY_PACKAGE_INFO)
                : null;
        if (mPackageInfo == null) {
            Log.w(TAG, "Missing PackageInfo; maybe reinstalling?");
            return;
        }
        use(AppHeaderPreferenceController.class).setParentFragment(this).setPackageInfo(
                mPackageInfo).setLifeCycle(getSettingsLifecycle());
        use(AppOpenSupportedLinksPreferenceController.class).setInit(mPackageInfo.packageName);
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPackageManager = getPackageManager();
        addPreferencesFromResource(R.xml.open_supported_links);
        initRadioPreferencesGroup();
        updateFooterPreference();
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        super.onCreatePreferences(savedInstanceState, rootKey);
        final FooterPreference footer = findPreference(FOOTER_KEY);
        if (footer == null) {
            Log.w(TAG, "Can't find the footer preference.");
            return;
    public int getMetricsCategory() {
        return SettingsEnums.OPEN_SUPPORTED_LINKS;
    }
        addLinksToFooter(footer);

    /**
     * Here to handle radio group and generate the radios.
     */
    @VisibleForTesting
    void initRadioPreferencesGroup() {
        mPreferenceCategory = findPreference(RADIO_GROUP_KEY);
        mAllowOpening = makeRadioPreference(KEY_LINK_OPEN_ALWAYS, R.string.app_link_open_always);
        final int entriesNo = getEntriesNo();
        //This to avoid the summary line wrap
        mAllowOpening.setAppendixVisibility(View.GONE);
        mAllowOpening.setSummary(getResources().getQuantityString(
                R.plurals.app_link_open_always_summary, entriesNo, entriesNo));
        mAskEveryTime = makeRadioPreference(KEY_LINK_OPEN_ASK, R.string.app_link_open_ask);
        mNotAllowed = makeRadioPreference(KEY_LINK_OPEN_NEVER, R.string.app_link_open_never);

        final int state = mPackageManager.getIntentVerificationStatusAsUser(mPackageName, mUserId);
        mCurrentIndex = linkStateToIndex(state);
        setRadioStatus(mCurrentIndex);
    }

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.open_supported_links;
    public void onRadioButtonClicked(RadioButtonPreference preference) {
        final int clickedIndex = preferenceKeyToIndex(preference.getKey());
        if (mCurrentIndex != clickedIndex) {
            mCurrentIndex = clickedIndex;
            setRadioStatus(mCurrentIndex);
            updateAppLinkState(indexToLinkState(mCurrentIndex));
        }
    }

    @Override
    protected String getLogTag() {
        return TAG;
    private RadioButtonPreference makeRadioPreference(String key, int stringId) {
        final RadioButtonPreference pref = new RadioButtonPreference(
                mPreferenceCategory.getContext());
        pref.setKey(key);
        pref.setTitle(stringId);
        pref.setOnClickListener(this);
        mPreferenceCategory.addPreference(pref);
        return pref;
    }

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.OPEN_SUPPORTED_LINKS;
    @VisibleForTesting
    int getEntriesNo() {
        return Utils.getHandledDomains(mPackageManager, mPackageName).size();
    }

    private int linkStateToIndex(int state) {
        switch (state) {
            case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS:
                return ALLOW_ALWAYS_OPENING;
            case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER:
                return NOT_ALLOWED_OPENING;
            default:
                return ASK_EVERY_TIME;
        }
    }

    private int indexToLinkState(int index) {
        switch (index) {
            case 0:
                return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
            case 2:
                return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
            default:
                return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
        }
    }

    private void setRadioStatus(int index) {
        mAllowOpening.setChecked(index == ALLOW_ALWAYS_OPENING);
        mAskEveryTime.setChecked(index == ASK_EVERY_TIME);
        mNotAllowed.setChecked(index == NOT_ALLOWED_OPENING);
    }

    private int preferenceKeyToIndex(String key) {
        for (int i = 0; i < mRadioKeys.length; i++) {
            if (TextUtils.equals(key, mRadioKeys[i])) {
                return i;
            }
        }
        return ASK_EVERY_TIME;
    }

    private void updateAppLinkState(final int newState) {
        final int priorState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
                mUserId);

        if (priorState == newState) {
            return;
        }

        final boolean success = mPackageManager.updateIntentVerificationStatusAsUser(mPackageName,
                newState, mUserId);
        if (success) {
            // Read back the state to see if the change worked
            final int updatedState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
                    mUserId);
        } else {
            Log.e(TAG, "Couldn't update intent verification status!");
        }
    }

    /**
     * Here is handle the Footer.
     */
    private void updateFooterPreference() {
        final FooterPreference footer = findPreference(FOOTER_KEY);
        if (footer == null) {
            Log.w(TAG, "Can't find the footer preference.");
            return;
        }
        addLinksToFooter(footer);
    }

    @VisibleForTesting
    void addLinksToFooter(FooterPreference footer) {
        final ArraySet<String> result = Utils.getHandledDomains(getPackageManager(),
                mPackageInfo.packageName);
        final ArraySet<String> result = Utils.getHandledDomains(mPackageManager, mPackageName);
        if (result.isEmpty()) {
            Log.w(TAG, "Can't find any app links.");
            return;
@@ -94,4 +206,14 @@ public class OpenSupportedLinks extends DashboardFragment {
        }
        footer.setTitle(title);
    }

    @Override
    protected boolean refreshUi() {
        return true;
    }

    @Override
    protected AlertDialog createDialog(int id, int errorCode) {
        return null;
    }
}
Loading