Merge "Update WebView implementation Dev Setting - new looks + descriptions."
This commit is contained in:
committed by
Android (Google) Code Review
commit
1c750fb7c0
@@ -78,7 +78,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.accessibility.AccessibilityManager;
|
||||
import android.webkit.IWebViewUpdateService;
|
||||
import android.webkit.WebViewProviderInfo;
|
||||
import android.webkit.WebViewFactory;
|
||||
import android.widget.Switch;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -92,6 +92,7 @@ import com.android.settings.fuelgauge.InactiveApps;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settings.webview.WebViewAppPreferenceController;
|
||||
import com.android.settings.widget.SwitchBar;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
@@ -126,7 +127,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
private static final String ENABLE_TERMINAL = "enable_terminal";
|
||||
private static final String KEEP_SCREEN_ON = "keep_screen_on";
|
||||
private static final String BT_HCI_SNOOP_LOG = "bt_hci_snoop_log";
|
||||
private static final String WEBVIEW_PROVIDER_KEY = "select_webview_provider";
|
||||
private static final String WEBVIEW_MULTIPROCESS_KEY = "enable_webview_multiprocess";
|
||||
private static final String ENABLE_OEM_UNLOCK = "oem_unlock_enable";
|
||||
private static final String HDCP_CHECKING_KEY = "hdcp_checking";
|
||||
@@ -230,6 +230,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
|
||||
private static final int RESULT_DEBUG_APP = 1000;
|
||||
private static final int RESULT_MOCK_LOCATION_APP = 1001;
|
||||
private static final int RESULT_WEBVIEW_APP = 1002;
|
||||
|
||||
private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
|
||||
private static final String FLASH_LOCKED_PROP = "ro.boot.flash.locked";
|
||||
@@ -310,8 +311,8 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
private ListPreference mAnimatorDurationScale;
|
||||
private ListPreference mOverlayDisplayDevices;
|
||||
|
||||
private WebViewAppPreferenceController mWebViewAppPrefController;
|
||||
private SwitchPreference mWebViewMultiprocess;
|
||||
private ListPreference mWebViewProvider;
|
||||
|
||||
private ListPreference mSimulateColorSpace;
|
||||
|
||||
@@ -371,8 +372,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
|
||||
mBackupManager = IBackupManager.Stub.asInterface(
|
||||
ServiceManager.getService(Context.BACKUP_SERVICE));
|
||||
mWebViewUpdateService =
|
||||
IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate"));
|
||||
mWebViewUpdateService = WebViewFactory.getUpdateService();
|
||||
mOemUnlockManager = (PersistentDataBlockManager) getActivity()
|
||||
.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
|
||||
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||
@@ -384,6 +384,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
|
||||
mBugReportController = new BugReportPreferenceController(getActivity());
|
||||
mBugReportInPowerController = new BugReportInPowerPreferenceController(getActivity());
|
||||
mWebViewAppPrefController = new WebViewAppPreferenceController(getActivity());
|
||||
|
||||
setIfOnlyAvailableForAdmins(true);
|
||||
if (isUiRestricted() || !Utils.isDeviceProvisioned(getActivity())) {
|
||||
@@ -414,6 +415,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
|
||||
mBugReportController.displayPreference(getPreferenceScreen());
|
||||
mBugReportInPowerController.displayPreference(getPreferenceScreen());
|
||||
mWebViewAppPrefController.displayPreference(getPreferenceScreen());
|
||||
|
||||
mKeepScreenOn = (RestrictedSwitchPreference) findAndInitSwitchPref(KEEP_SCREEN_ON);
|
||||
mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
|
||||
@@ -483,7 +485,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
mLogpersist = null;
|
||||
}
|
||||
mUsbConfiguration = addListPreference(USB_CONFIGURATION_KEY);
|
||||
mWebViewProvider = addListPreference(WEBVIEW_PROVIDER_KEY);
|
||||
mWebViewMultiprocess = findAndInitSwitchPref(WEBVIEW_MULTIPROCESS_KEY);
|
||||
mBluetoothDisableAbsVolume = findAndInitSwitchPref(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_KEY);
|
||||
|
||||
@@ -544,7 +545,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
removePreference(KEY_COLOR_MODE);
|
||||
mColorModePreference = null;
|
||||
}
|
||||
updateWebViewProviderOptions();
|
||||
|
||||
mColorTemperaturePreference = (SwitchPreference) findPreference(COLOR_TEMPERATURE_KEY);
|
||||
if (getResources().getBoolean(R.bool.config_enableColorTemperature)) {
|
||||
@@ -629,6 +629,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
|
||||
}
|
||||
mBugReportInPowerController.enablePreference(enabled);
|
||||
mWebViewAppPrefController.enablePreference(enabled);
|
||||
updateAllOptions();
|
||||
}
|
||||
|
||||
@@ -793,8 +794,8 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
updateSimulateColorSpace();
|
||||
updateUSBAudioOptions();
|
||||
updateForceResizableOptions();
|
||||
mWebViewAppPrefController.updateState(null);
|
||||
updateWebViewMultiprocessOptions();
|
||||
updateWebViewProviderOptions();
|
||||
updateOemUnlockOptions();
|
||||
if (mColorTemperaturePreference != null) {
|
||||
updateColorTemperature();
|
||||
@@ -831,39 +832,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
pokeSystemProperties();
|
||||
}
|
||||
|
||||
private void updateWebViewProviderOptions() {
|
||||
try {
|
||||
WebViewProviderInfo[] providers = mWebViewUpdateService.getValidWebViewPackages();
|
||||
if (providers == null) {
|
||||
Log.e(TAG, "No WebView providers available");
|
||||
return;
|
||||
}
|
||||
ArrayList<String> options = new ArrayList<String>();
|
||||
ArrayList<String> values = new ArrayList<String>();
|
||||
for (int n = 0; n < providers.length; n++) {
|
||||
if (Utils.isPackageEnabled(getActivity(), providers[n].packageName)) {
|
||||
options.add(providers[n].description);
|
||||
values.add(providers[n].packageName);
|
||||
}
|
||||
}
|
||||
mWebViewProvider.setEntries(options.toArray(new String[options.size()]));
|
||||
mWebViewProvider.setEntryValues(values.toArray(new String[values.size()]));
|
||||
|
||||
String value = mWebViewUpdateService.getCurrentWebViewPackageName();
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
if (value.contentEquals(values.get(i))) {
|
||||
mWebViewProvider.setValueIndex(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateWebViewMultiprocessOptions() {
|
||||
try {
|
||||
updateSwitchPreference(mWebViewMultiprocess,
|
||||
@@ -918,17 +886,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
mBtHciSnoopLog.isChecked() ? 1 : 0);
|
||||
}
|
||||
|
||||
private boolean writeWebViewProviderOptions(Object newValue) {
|
||||
try {
|
||||
String updatedProvider = mWebViewUpdateService.changeProviderAndSetting(
|
||||
newValue == null ? "" : newValue.toString());
|
||||
updateWebViewProviderOptions();
|
||||
return newValue != null && newValue.equals(updatedProvider);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void writeDebuggerOptions() {
|
||||
try {
|
||||
ActivityManager.getService().setDebugApp(
|
||||
@@ -2327,6 +2284,8 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
writeMockLocation();
|
||||
updateMockLocation();
|
||||
}
|
||||
} else if (requestCode == RESULT_WEBVIEW_APP) {
|
||||
mWebViewAppPrefController.onActivityResult(resultCode, data);
|
||||
} else if (requestCode == REQUEST_CODE_ENABLE_OEM_UNLOCK) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
if (mEnableOemUnlock.isChecked()) {
|
||||
@@ -2349,6 +2308,10 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
if (mBugReportInPowerController.handlePreferenceTreeClick(preference)) {
|
||||
return true;
|
||||
}
|
||||
if (mWebViewAppPrefController.handlePreferenceTreeClick(preference)) {
|
||||
startActivityForResult(
|
||||
mWebViewAppPrefController.getActivityIntent(), RESULT_WEBVIEW_APP);
|
||||
}
|
||||
|
||||
if (preference == mEnableAdb) {
|
||||
if (mEnableAdb.isChecked()) {
|
||||
@@ -2502,21 +2465,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
||||
updateHdcpValues();
|
||||
pokeSystemProperties();
|
||||
return true;
|
||||
} else if (preference == mWebViewProvider) {
|
||||
if (newValue == null) {
|
||||
Log.e(TAG, "Tried to set a null WebView provider");
|
||||
return false;
|
||||
}
|
||||
if (writeWebViewProviderOptions(newValue)) {
|
||||
return true;
|
||||
} else {
|
||||
// The user chose a package that became invalid since the list was last updated,
|
||||
// show a Toast to explain the situation.
|
||||
Toast toast = Toast.makeText(getActivity(),
|
||||
R.string.select_webview_provider_toast_text, Toast.LENGTH_SHORT);
|
||||
toast.show();
|
||||
}
|
||||
return false;
|
||||
} else if ((preference == mBluetoothSelectA2dpCodec) ||
|
||||
(preference == mBluetoothSelectA2dpSampleRate) ||
|
||||
(preference == mBluetoothSelectA2dpBitsPerSample) ||
|
||||
|
||||
@@ -44,6 +44,8 @@ public class SettingsInitialize extends BroadcastReceiver {
|
||||
private static final String TAG = "Settings";
|
||||
private static final String PRIMARY_PROFILE_SETTING =
|
||||
"com.android.settings.PRIMARY_PROFILE_CONTROLLED";
|
||||
private static final String SETTINGS_PACKAGE = "com.android.settings";
|
||||
private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent broadcast) {
|
||||
@@ -100,7 +102,7 @@ public class SettingsInitialize extends BroadcastReceiver {
|
||||
return;
|
||||
}
|
||||
ComponentName settingsComponentName =
|
||||
new ComponentName(context, WebViewImplementation.class);
|
||||
new ComponentName(SETTINGS_PACKAGE, SETTINGS_PACKAGE + WEBVIEW_IMPLEMENTATION_ACTIVITY);
|
||||
pm.setComponentEnabledSetting(settingsComponentName,
|
||||
userInfo.isAdmin() ?
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnCancelListener;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.DialogInterface.OnDismissListener;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.UserManager;
|
||||
import android.util.Log;
|
||||
import android.webkit.IWebViewUpdateService;
|
||||
import android.webkit.WebViewProviderInfo;
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.core.InstrumentedActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class WebViewImplementation extends InstrumentedActivity implements
|
||||
OnCancelListener, OnDismissListener {
|
||||
|
||||
private static final String TAG = "WebViewImplementation";
|
||||
|
||||
private IWebViewUpdateService mWebViewUpdateService;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (!UserManager.get(this).isAdminUser()) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
mWebViewUpdateService =
|
||||
IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate"));
|
||||
try {
|
||||
WebViewProviderInfo[] providers = mWebViewUpdateService.getValidWebViewPackages();
|
||||
if (providers == null) {
|
||||
Log.e(TAG, "No WebView providers available");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
String currentValue = mWebViewUpdateService.getCurrentWebViewPackageName();
|
||||
if (currentValue == null) {
|
||||
currentValue = "";
|
||||
}
|
||||
|
||||
int currentIndex = -1;
|
||||
ArrayList<String> options = new ArrayList<>();
|
||||
final ArrayList<String> values = new ArrayList<>();
|
||||
for (WebViewProviderInfo provider : providers) {
|
||||
if (Utils.isPackageEnabled(this, provider.packageName)) {
|
||||
options.add(provider.description);
|
||||
values.add(provider.packageName);
|
||||
if (currentValue.contentEquals(provider.packageName)) {
|
||||
currentIndex = values.size() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle(R.string.select_webview_provider_dialog_title)
|
||||
.setSingleChoiceItems(options.toArray(new String[0]), currentIndex,
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
try {
|
||||
mWebViewUpdateService.changeProviderAndSetting(values.get(which));
|
||||
} catch (RemoteException e) {
|
||||
Log.w(TAG, "Problem reaching webviewupdate service", e);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
}).setNegativeButton(android.R.string.cancel, null)
|
||||
.setOnCancelListener(this)
|
||||
.setOnDismissListener(this)
|
||||
.show();
|
||||
} catch (RemoteException e) {
|
||||
Log.w(TAG, "Problem reaching webviewupdate service", e);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.WEBVIEW_IMPLEMENTATION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
33
src/com/android/settings/webview/UserPackageWrapper.java
Normal file
33
src/com/android/settings/webview/UserPackageWrapper.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.webkit.UserPackage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Wrapper class around android.webkit.UserPackage - to be able to use UserPackage in Robolectric
|
||||
* tests (such tests currently don't support mocking hidden classes).
|
||||
*/
|
||||
interface UserPackageWrapper {
|
||||
UserInfo getUserInfo();
|
||||
PackageInfo getPackageInfo();
|
||||
boolean isEnabledPackage();
|
||||
boolean isInstalledPackage();
|
||||
}
|
||||
50
src/com/android/settings/webview/UserPackageWrapperImpl.java
Normal file
50
src/com/android/settings/webview/UserPackageWrapperImpl.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.webkit.UserPackage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Default implementation of UserPackageWrapper.
|
||||
*/
|
||||
class UserPackageWrapperImpl implements UserPackageWrapper {
|
||||
private final UserPackage mUserPackage;
|
||||
|
||||
UserPackageWrapperImpl(UserPackage userPackage) {
|
||||
mUserPackage = userPackage;
|
||||
}
|
||||
|
||||
public UserInfo getUserInfo() {
|
||||
return mUserPackage.getUserInfo();
|
||||
}
|
||||
|
||||
public PackageInfo getPackageInfo() {
|
||||
return mUserPackage.getPackageInfo();
|
||||
}
|
||||
|
||||
public boolean isEnabledPackage() {
|
||||
return mUserPackage.isEnabledPackage();
|
||||
}
|
||||
|
||||
public boolean isInstalledPackage() {
|
||||
return mUserPackage.isInstalledPackage();
|
||||
}
|
||||
}
|
||||
126
src/com/android/settings/webview/WebViewAppListAdapter.java
Normal file
126
src/com/android/settings/webview/WebViewAppListAdapter.java
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.RemoteException;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
||||
import com.android.settings.applications.AppViewHolder;
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Custom list adapter for Settings to choose WebView package.
|
||||
* Note: parts of this class are copied from AppPicker.java.
|
||||
*/
|
||||
class WebViewAppListAdapter extends ArrayAdapter<WebViewApplicationInfo> {
|
||||
private final LayoutInflater mInflater;
|
||||
|
||||
public WebViewAppListAdapter(Context context,
|
||||
WebViewUpdateServiceWrapper webviewUpdateServiceWrapper) {
|
||||
super(context, 0);
|
||||
mInflater = LayoutInflater.from(context);
|
||||
|
||||
final List<WebViewApplicationInfo> packageInfoList =
|
||||
new ArrayList<WebViewApplicationInfo>();
|
||||
List<ApplicationInfo> pkgs =
|
||||
webviewUpdateServiceWrapper.getValidWebViewApplicationInfos(getContext());
|
||||
for (ApplicationInfo ai : pkgs) {
|
||||
WebViewApplicationInfo info = new WebViewApplicationInfo(ai,
|
||||
ai.loadLabel(context.getPackageManager()).toString(),
|
||||
getDisabledReason(webviewUpdateServiceWrapper, context, ai.packageName));
|
||||
packageInfoList.add(info);
|
||||
}
|
||||
addAll(packageInfoList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
// A ViewHolder keeps references to children views to avoid unnecessary calls
|
||||
// to findViewById() on each row.
|
||||
AppViewHolder holder = AppViewHolder.createOrRecycle(mInflater, convertView);
|
||||
convertView = holder.rootView;
|
||||
WebViewApplicationInfo info = getItem(position);
|
||||
holder.appName.setText(info.label);
|
||||
if (info.info != null) {
|
||||
holder.appIcon.setImageDrawable(info.info.loadIcon(getContext().getPackageManager()));
|
||||
// Allow disable-description to wrap - to be able to show several lines of text in case
|
||||
// a package is disabled/uninstalled for several users.
|
||||
holder.summary.setSingleLine(false);
|
||||
if (!isEnabled(position)) {
|
||||
holder.summary.setText(info.disabledReason);
|
||||
} else {
|
||||
holder.summary.setText("");
|
||||
}
|
||||
} else {
|
||||
holder.appIcon.setImageDrawable(null);
|
||||
holder.summary.setText("");
|
||||
}
|
||||
holder.disabled.setVisibility(View.GONE);
|
||||
// Only allow a package to be chosen if it is enabled and installed for all users.
|
||||
convertView.setEnabled(isEnabled(position));
|
||||
return convertView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled (int position) {
|
||||
WebViewApplicationInfo info = getItem(position);
|
||||
return info.disabledReason == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areAllItemsEnabled() {
|
||||
int numItems = getCount();
|
||||
for (int n = 0; n < numItems; n++) {
|
||||
if (!isEnabled(n)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reason why a package cannot be used as WebView implementation.
|
||||
* This is either because of it being disabled, uninstalled, or hidden for any user.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static String getDisabledReason(WebViewUpdateServiceWrapper webviewUpdateServiceWrapper,
|
||||
Context context, String packageName) {
|
||||
StringBuilder disabledReason = new StringBuilder();
|
||||
List<UserPackageWrapper> userPackages =
|
||||
webviewUpdateServiceWrapper.getPackageInfosAllUsers(context, packageName);
|
||||
for (UserPackageWrapper userPackage : userPackages) {
|
||||
if (!userPackage.isInstalledPackage()) {
|
||||
// Package uninstalled/hidden
|
||||
disabledReason.append(context.getString(
|
||||
R.string.webview_uninstalled_for_user, userPackage.getUserInfo().name));
|
||||
} else if (!userPackage.isEnabledPackage()) {
|
||||
// Package disabled
|
||||
disabledReason.append(context.getString(
|
||||
R.string.webview_disabled_for_user, userPackage.getUserInfo().name));
|
||||
}
|
||||
}
|
||||
if (disabledReason.length() == 0) return null;
|
||||
return disabledReason.toString();
|
||||
}
|
||||
}
|
||||
|
||||
95
src/com/android/settings/webview/WebViewAppPicker.java
Normal file
95
src/com/android/settings/webview/WebViewAppPicker.java
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.app.ListActivity;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.webkit.WebViewFactory;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.core.instrumentation.Instrumentable;
|
||||
import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
|
||||
|
||||
public class WebViewAppPicker extends ListActivity implements Instrumentable {
|
||||
private static final String TAG = WebViewAppPicker.class.getSimpleName();
|
||||
private WebViewAppListAdapter mAdapter;
|
||||
private WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
|
||||
|
||||
private final VisibilityLoggerMixin mVisibilityLoggerMixin =
|
||||
new VisibilityLoggerMixin(getMetricsCategory());
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
if (mWebViewUpdateServiceWrapper == null) {
|
||||
setWebViewUpdateServiceWrapper(createDefaultWebViewUpdateServiceWrapper());
|
||||
}
|
||||
mAdapter = new WebViewAppListAdapter(this, mWebViewUpdateServiceWrapper);
|
||||
setListAdapter(mAdapter);
|
||||
|
||||
mVisibilityLoggerMixin.onAttach(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||
WebViewApplicationInfo app = mAdapter.getItem(position);
|
||||
|
||||
if (mWebViewUpdateServiceWrapper.setWebViewProvider(app.info.packageName)) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(app.info.packageName);
|
||||
setResult(RESULT_OK, intent);
|
||||
} else {
|
||||
mWebViewUpdateServiceWrapper.showInvalidChoiceToast(this);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
private WebViewUpdateServiceWrapper createDefaultWebViewUpdateServiceWrapper() {
|
||||
return new WebViewUpdateServiceWrapper();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setWebViewUpdateServiceWrapper(WebViewUpdateServiceWrapper wvusWrapper) {
|
||||
mWebViewUpdateServiceWrapper = wvusWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mVisibilityLoggerMixin.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mVisibilityLoggerMixin.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.WEBVIEW_IMPLEMENTATION;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.DevelopmentSettings;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
|
||||
public class WebViewAppPreferenceController extends PreferenceController {
|
||||
|
||||
private static final String WEBVIEW_APP_KEY = "select_webview_provider";
|
||||
|
||||
private Context mContext;
|
||||
private Preference mPreference;
|
||||
private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
|
||||
|
||||
public WebViewAppPreferenceController(Context context) {
|
||||
this(context, new WebViewUpdateServiceWrapper());
|
||||
}
|
||||
|
||||
public WebViewAppPreferenceController(Context context,
|
||||
WebViewUpdateServiceWrapper webviewUpdateServiceWrapper) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mWebViewUpdateServiceWrapper = webviewUpdateServiceWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (getPreferenceKey().equals(preference.getKey())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Intent getActivityIntent() {
|
||||
return new Intent(mContext, WebViewAppPicker.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
mPreference.setSummary(getCurrentWebViewPackageLabel(mContext));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
if (isAvailable()) {
|
||||
mPreference = screen.findPreference(WEBVIEW_APP_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the return-value from the WebViewAppPicker Activity.
|
||||
*/
|
||||
public void onActivityResult(int resultCode, Intent data) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
updateState(null);
|
||||
}
|
||||
}
|
||||
|
||||
private String getCurrentWebViewPackageLabel(Context context) {
|
||||
PackageInfo webViewPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
|
||||
if (webViewPackage == null) return "";
|
||||
return webViewPackage.applicationInfo.loadLabel(context.getPackageManager()).toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return WEBVIEW_APP_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void enablePreference(boolean enabled) {
|
||||
if (isAvailable()) {
|
||||
mPreference.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/com/android/settings/webview/WebViewApplicationInfo.java
Normal file
29
src/com/android/settings/webview/WebViewApplicationInfo.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.content.pm.ApplicationInfo;
|
||||
|
||||
final class WebViewApplicationInfo {
|
||||
final ApplicationInfo info;
|
||||
final String label;
|
||||
final String disabledReason;
|
||||
|
||||
public WebViewApplicationInfo(ApplicationInfo info, String label, String disabledReason) {
|
||||
this.info = info;
|
||||
this.label = label;
|
||||
this.disabledReason = disabledReason;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.webview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
import android.webkit.UserPackage;
|
||||
import android.webkit.WebViewFactory;
|
||||
import android.webkit.WebViewProviderInfo;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class WebViewUpdateServiceWrapper {
|
||||
private static final String TAG = "WVUSWrapper";
|
||||
|
||||
public WebViewUpdateServiceWrapper() {}
|
||||
|
||||
/**
|
||||
* Fetch the package currently used as WebView implementation.
|
||||
*/
|
||||
public PackageInfo getCurrentWebViewPackage() {
|
||||
try {
|
||||
return WebViewFactory.getUpdateService().getCurrentWebViewPackage();
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, e.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches ApplicationInfo objects for all currently valid WebView packages.
|
||||
* A WebView package is considered valid if it can be used as a WebView implementation. The
|
||||
* validity of a package is not dependent on whether the package is installed/enabled.
|
||||
*/
|
||||
public List<ApplicationInfo> getValidWebViewApplicationInfos(Context context) {
|
||||
WebViewProviderInfo[] providers = null;
|
||||
try {
|
||||
providers = WebViewFactory.getUpdateService().getValidWebViewPackages();
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
List<ApplicationInfo> pkgs = new ArrayList<>();
|
||||
for (WebViewProviderInfo provider : providers) {
|
||||
try {
|
||||
pkgs.add(context.getPackageManager().getApplicationInfo(
|
||||
provider.packageName, PACKAGE_FLAGS));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
}
|
||||
}
|
||||
return pkgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change WebView provider to {@param packageName}.
|
||||
* @return whether the change succeeded.
|
||||
*/
|
||||
public boolean setWebViewProvider(String packageName) {
|
||||
try {
|
||||
return packageName.equals(
|
||||
WebViewFactory.getUpdateService().changeProviderAndSetting(packageName));
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "RemoteException when trying to change provider to " + packageName, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch PackageInfos for the package named {@param packageName} for all users on the device.
|
||||
*/
|
||||
public List<UserPackageWrapper> getPackageInfosAllUsers(Context context, String packageName) {
|
||||
List<UserPackageWrapper> userPackageWrappers = new ArrayList<>();
|
||||
List<UserPackage> userPackages =
|
||||
UserPackage.getPackageInfosAllUsers(context, packageName, PACKAGE_FLAGS);
|
||||
for (UserPackage userPackage : userPackages) {
|
||||
userPackageWrappers.add(new UserPackageWrapperImpl(userPackage));
|
||||
}
|
||||
return userPackageWrappers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a toast to explain the chosen package can no longer be chosen.
|
||||
*/
|
||||
public void showInvalidChoiceToast(Context context) {
|
||||
// The user chose a package that became invalid since the list was last updated,
|
||||
// show a Toast to explain the situation.
|
||||
Toast toast = Toast.makeText(context,
|
||||
R.string.select_webview_provider_toast_text, Toast.LENGTH_SHORT);
|
||||
toast.show();
|
||||
}
|
||||
|
||||
static final int PACKAGE_FLAGS = PackageManager.MATCH_ANY_USER;
|
||||
}
|
||||
Reference in New Issue
Block a user