Create a new Open supported links page

- Add a new OpenSupportedLinks fragment
- Add a new AppHeaderPreferenceController
- Add a new AppOpenSupportedLinksPreferenceController

Fixes: 143185713
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.applications
Change-Id: I6568950b7b918378baaa80e2ca0d596913620928
This commit is contained in:
Sunny Shao
2019-10-28 20:40:53 +08:00
parent 162e2b92b8
commit 6b032f4e2d
10 changed files with 727 additions and 99 deletions

View File

@@ -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;