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

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

Merge "Create a new Open supported links page"

parents 3a243822 6b032f4e
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -9061,13 +9061,21 @@
    </plurals>
    <!-- Explanation that the app that will ALWAYS be launched to open web links to domains that it understands -->
    <string name="app_link_open_always">Open in this app</string>
    <string name="app_link_open_always">Allow app to open supported links</string>
    <!-- Explanation that the user will be asked whether to launch the app to open web links to domains that it understands -->
    <string name="app_link_open_ask">Ask every time</string>
    <!-- Explanation that the app that will NEVER be launched to open web links to domains that it understands -->
    <string name="app_link_open_never">Don&#8217;t open in this app</string>
    <string name="app_link_open_never">Don&#8217;t allow app to open links</string>
    <plurals name="app_link_open_always_summary">
        <item quantity="one">App claims to handle <xliff:g id="count">%d</xliff:g> link</item>
        <item quantity="other">App claims to handle <xliff:g id="count">%d</xliff:g> links</item>
    </plurals>
    <!-- Footer of open supported links settings [CHAR LIMIT=NONE] -->
    <string name="open_supported_links_footer">App claims to handle following links:</string>
    <!-- Title for Default Apps settings [CHAR LIMIT=30] -->
    <string name="default_apps_title">Default</string>
+9 −9
Original line number Diff line number Diff line
@@ -21,9 +21,9 @@
    <PreferenceCategory android:key="app_launch_domain_links"
                        android:title="@string/app_launch_domain_links_title">

        <DropDownPreference
        <Preference
            android:key="app_link_state"
                android:summary="%s"
            android:summary="@string/summary_placeholder"
            android:title="@string/app_launch_open_domain_urls_title"/>

        <com.android.settings.applications.AppDomainsPreference
+40 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    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">
    </PreferenceCategory>

    <com.android.settingslib.widget.FooterPreference
        android:key="supported_links_footer"
        android:title="@string/open_supported_links_footer"
        android:selectable="false"
        settings:searchable="false"/>

</PreferenceScreen>
 No newline at end of file
+107 −0
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 android.util.IconDrawableFactory;

import androidx.preference.PreferenceScreen;

import com.android.settings.R;
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(IconDrawableFactory.newInstance(activity).getBadgedIcon(
                        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 */);
    }
}
+44 −88
Original line number Diff line number Diff line
@@ -17,14 +17,11 @@
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_ALWAYS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;

import android.app.settings.SettingsEnums;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.IntentFilterVerificationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
@@ -36,24 +33,27 @@ import android.view.View;
import android.view.View.OnClickListener;

import androidx.appcompat.app.AlertDialog;
import androidx.preference.DropDownPreference;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;

import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.SubSettingLauncher;

import java.util.List;

public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListener,
        Preference.OnPreferenceChangeListener {
    private static final String TAG = "AppLaunchSettings";

    private static final String KEY_APP_LINK_STATE = "app_link_state";
    private static final String KEY_SUPPORTED_DOMAIN_URLS = "app_launch_supported_domain_urls";
    private static final String KEY_CLEAR_DEFAULTS = "app_launch_clear_defaults";
    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 {
        sBrowserIntent = new Intent()
                .setAction(Intent.ACTION_VIEW)
@@ -65,7 +65,7 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe

    private boolean mIsBrowser;
    private boolean mHasDomainUrls;
    private DropDownPreference mAppLinkState;
    private Preference mAppLinkState;
    private AppDomainsPreference mAppDomainUrls;
    private ClearDefaultsPreference mClearDefaultsPreference;

@@ -76,7 +76,19 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
        addPreferencesFromResource(R.xml.installed_app_launch_settings);
        mAppDomainUrls = (AppDomainsPreference) findPreference(KEY_SUPPORTED_DOMAIN_URLS);
        mClearDefaultsPreference = (ClearDefaultsPreference) findPreference(KEY_CLEAR_DEFAULTS);
        mAppLinkState = (DropDownPreference) findPreference(KEY_APP_LINK_STATE);
        mAppLinkState = findPreference(KEY_APP_LINK_STATE);
        mAppLinkState.setOnPreferenceClickListener(preference -> {
            final Bundle args = new Bundle();
            args.putParcelable(KEY_PACKAGE_INFO, this.mPackageInfo);

            new SubSettingLauncher(this.getContext())
                    .setDestination(FRAGMENT_OPEN_SUPPORTED_LINKS)
                    .setArguments(args)
                    .setSourceMetricsCategory(SettingsEnums.APPLICATIONS_APP_LAUNCH)
                    .setTitleRes(-1)
                    .launch();
            return true;
        });

        mPm = getActivity().getPackageManager();

@@ -85,13 +97,17 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
                (mAppEntry.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS) != 0;

        if (!mIsBrowser) {
            List<IntentFilterVerificationInfo> iviList = mPm.getIntentFilterVerifications(mPackageName);
            List<IntentFilter> filters = mPm.getAllIntentFilters(mPackageName);
            CharSequence[] entries = getEntries(mPackageName, iviList, filters);
            CharSequence[] entries = getEntries(mPackageName);
            mAppDomainUrls.setTitles(entries);
            mAppDomainUrls.setValues(new int[entries.length]);
            mAppLinkState.setEnabled(mHasDomainUrls);
        } else {
            // Browsers don't show the app-link prefs
            mAppLinkState.setShouldDisableView(true);
            mAppLinkState.setEnabled(false);
            mAppDomainUrls.setShouldDisableView(true);
            mAppDomainUrls.setEnabled(false);
        }
        buildStateDropDown();
    }

    // An app is a "browser" if it has an activity resolution that wound up
@@ -110,95 +126,35 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
        return false;
    }

    private void buildStateDropDown() {
        if (mIsBrowser) {
            // Browsers don't show the app-link prefs
            mAppLinkState.setShouldDisableView(true);
            mAppLinkState.setEnabled(false);
            mAppDomainUrls.setShouldDisableView(true);
            mAppDomainUrls.setEnabled(false);
        } else {
            // Designed order of states in the dropdown:
            //
            // * always
            // * ask
            // * never
            //
            // Make sure to update linkStateToIndex() if this presentation order is changed.
            mAppLinkState.setEntries(new CharSequence[] {
                    getString(R.string.app_link_open_always),
                    getString(R.string.app_link_open_ask),
                    getString(R.string.app_link_open_never),
            });
            mAppLinkState.setEntryValues(new CharSequence[] {
                    Integer.toString(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS),
                    Integer.toString(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK),
                    Integer.toString(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER),
            });

            mAppLinkState.setEnabled(mHasDomainUrls);
            if (mHasDomainUrls) {
                // Present 'undefined' as 'ask' because the OS treats them identically for
                // purposes of the UI (and does the right thing around pending domain
                // verifications that might arrive after the user chooses 'ask' in this UI).
                final int state = mPm.getIntentVerificationStatusAsUser(mPackageName, UserHandle.myUserId());
                mAppLinkState.setValueIndex(linkStateToIndex(state));

                // Set the callback only after setting the initial selected item
                mAppLinkState.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        return updateAppLinkState(Integer.parseInt((String) newValue));
                    }
                });
            }
        }
    }

    private int linkStateToIndex(final int state) {
    private int linkStateToResourceId(int state) {
        switch (state) {
            case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS:
                return 0; // Always
                return R.string.app_link_open_always; // Always
            case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER:
                return 2; // Never
                return R.string.app_link_open_never; // Never
            default:
                return 1; // Ask
        }
                return R.string.app_link_open_ask; // Ask
        }

    private boolean updateAppLinkState(final int newState) {
        if (mIsBrowser) {
            // We shouldn't get into this state, but if we do make sure
            // not to cause any permanent mayhem.
            return false;
    }

        final int userId = UserHandle.myUserId();
        final int priorState = mPm.getIntentVerificationStatusAsUser(mPackageName, userId);

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

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

    private CharSequence[] getEntries(String packageName, List<IntentFilterVerificationInfo> iviList,
            List<IntentFilter> filters) {
    private CharSequence[] getEntries(String packageName) {
        ArraySet<String> result = Utils.getHandledDomains(mPm, packageName);
        return result.toArray(new CharSequence[result.size()]);
    }

    private void setAppLinkStateSummary() {
        final int state = mPm.getIntentVerificationStatusAsUser(mPackageName,
                UserHandle.myUserId());
        Log.d("[sunny]", "setAppLinkStateSummary+ state=" + state);
        mAppLinkState.setSummary(linkStateToResourceId(state));
    }

    @Override
    protected boolean refreshUi() {
        if (mHasDomainUrls) {
            //Update the summary after return from the OpenSupportedLinks
            setAppLinkStateSummary();
        }
        mClearDefaultsPreference.setPackageName(mPackageName);
        mClearDefaultsPreference.setAppEntry(mAppEntry);
        return true;
Loading