1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15package com.android.settings; 16 17import android.annotation.Nullable; 18import android.app.AlertDialog; 19import android.content.DialogInterface; 20import android.content.DialogInterface.OnCancelListener; 21import android.content.DialogInterface.OnClickListener; 22import android.content.DialogInterface.OnDismissListener; 23import android.os.Bundle; 24import android.os.RemoteException; 25import android.os.ServiceManager; 26import android.os.UserManager; 27import android.util.Log; 28import android.webkit.IWebViewUpdateService; 29import android.webkit.WebViewProviderInfo; 30import com.android.internal.logging.MetricsProto.MetricsEvent; 31 32import java.util.ArrayList; 33 34public class WebViewImplementation extends InstrumentedActivity implements 35 OnCancelListener, OnDismissListener { 36 37 private static final String TAG = "WebViewImplementation"; 38 39 private IWebViewUpdateService mWebViewUpdateService; 40 41 @Override 42 protected void onCreate(@Nullable Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 if (!UserManager.get(this).isAdminUser()) { 45 finish(); 46 return; 47 } 48 mWebViewUpdateService = 49 IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate")); 50 try { 51 WebViewProviderInfo[] providers = mWebViewUpdateService.getValidWebViewPackages(); 52 if (providers == null) { 53 Log.e(TAG, "No WebView providers available"); 54 finish(); 55 return; 56 } 57 58 String currentValue = mWebViewUpdateService.getCurrentWebViewPackageName(); 59 if (currentValue == null) { 60 currentValue = ""; 61 } 62 63 int currentIndex = -1; 64 ArrayList<String> options = new ArrayList<>(); 65 final ArrayList<String> values = new ArrayList<>(); 66 for (WebViewProviderInfo provider : providers) { 67 if (Utils.isPackageEnabled(this, provider.packageName)) { 68 options.add(provider.description); 69 values.add(provider.packageName); 70 if (currentValue.contentEquals(provider.packageName)) { 71 currentIndex = values.size() - 1; 72 } 73 } 74 } 75 76 new AlertDialog.Builder(this) 77 .setTitle(R.string.select_webview_provider_dialog_title) 78 .setSingleChoiceItems(options.toArray(new String[0]), currentIndex, 79 new OnClickListener() { 80 @Override 81 public void onClick(DialogInterface dialog, int which) { 82 try { 83 mWebViewUpdateService.changeProviderAndSetting(values.get(which)); 84 } catch (RemoteException e) { 85 Log.w(TAG, "Problem reaching webviewupdate service", e); 86 } 87 finish(); 88 } 89 }).setNegativeButton(android.R.string.cancel, null) 90 .setOnCancelListener(this) 91 .setOnDismissListener(this) 92 .show(); 93 } catch (RemoteException e) { 94 Log.w(TAG, "Problem reaching webviewupdate service", e); 95 finish(); 96 } 97 } 98 99 @Override 100 protected int getMetricsCategory() { 101 return MetricsEvent.WEBVIEW_IMPLEMENTATION; 102 } 103 104 @Override 105 public void onCancel(DialogInterface dialog) { 106 finish(); 107 } 108 109 @Override 110 public void onDismiss(DialogInterface dialog) { 111 finish(); 112 } 113} 114