Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.webapk.shell_apk; | 5 package org.chromium.webapk.shell_apk; |
| 6 | 6 |
| 7 import android.app.Activity; | |
| 8 import android.app.AlertDialog; | 7 import android.app.AlertDialog; |
| 9 import android.content.Context; | 8 import android.content.Context; |
| 10 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
| 10 import android.content.pm.PackageManager; | |
| 11 import android.content.pm.ResolveInfo; | |
| 12 import android.graphics.drawable.Drawable; | |
| 11 import android.view.ContextThemeWrapper; | 13 import android.view.ContextThemeWrapper; |
| 12 import android.view.LayoutInflater; | 14 import android.view.LayoutInflater; |
| 13 import android.view.View; | 15 import android.view.View; |
| 14 import android.view.ViewGroup; | 16 import android.view.ViewGroup; |
| 15 import android.widget.AdapterView; | 17 import android.widget.AdapterView; |
| 16 import android.widget.ArrayAdapter; | 18 import android.widget.ArrayAdapter; |
| 17 import android.widget.ImageView; | 19 import android.widget.ImageView; |
| 18 import android.widget.ListView; | 20 import android.widget.ListView; |
| 19 import android.widget.TextView; | 21 import android.widget.TextView; |
| 20 | 22 |
| 23 import java.util.ArrayList; | |
| 21 import java.util.List; | 24 import java.util.List; |
| 22 | 25 |
| 23 /** | 26 /** |
| 24 * Shows the dialog to choose a host browser to launch WebAPK. Calls the listene r callback when the | 27 * Shows the dialog to choose a host browser to launch WebAPK. Calls the listene r callback when the |
| 25 * host browser is chosen. | 28 * host browser is chosen. |
| 26 */ | 29 */ |
| 27 | 30 |
| 28 public class ChooseHostBrowserDialog { | 31 public class ChooseHostBrowserDialog { |
| 29 /** | 32 /** |
| 30 * A listener to receive updates when user chooses a host browser for the We bAPK, or dismiss the | 33 * A listener to receive updates when user chooses a host browser for the We bAPK, or dismiss the |
| 31 * dialog. | 34 * dialog. |
| 32 */ | 35 */ |
| 33 public interface DialogListener { | 36 public interface DialogListener { |
| 34 void onHostBrowserSelected(String packageName); | 37 void onHostBrowserSelected(String selectedHostBrowser); |
| 35 void onQuit(); | 38 void onQuit(); |
| 36 } | 39 } |
| 37 | 40 |
| 41 /** Stores information about a potential host browser for the WebAPK. */ | |
| 42 public static class BrowserItem { | |
| 43 private String mPackageName; | |
| 44 private CharSequence mApplicationLabel; | |
| 45 private Drawable mIcon; | |
| 46 private boolean mSupportWebApks; | |
| 47 | |
| 48 public BrowserItem(String packageName, CharSequence applicationLabel, Dr awable icon, | |
| 49 boolean supportWebApks) { | |
| 50 mPackageName = packageName; | |
| 51 mApplicationLabel = applicationLabel; | |
| 52 mIcon = icon; | |
| 53 mSupportWebApks = supportWebApks; | |
| 54 } | |
| 55 | |
| 56 /** Returns the package name of a browser. */ | |
| 57 public String getPackageName() { | |
| 58 return mPackageName; | |
| 59 } | |
| 60 | |
| 61 /** Returns the application name of a browser. */ | |
| 62 public CharSequence getApplicationName() { | |
| 63 return mApplicationLabel; | |
| 64 } | |
| 65 | |
| 66 /** Returns a drawable of the browser icon. */ | |
| 67 public Drawable getApplicationIcon() { | |
| 68 return mIcon; | |
| 69 } | |
| 70 | |
| 71 /** Returns whether the browser supports WebAPKs. */ | |
| 72 public boolean supportWebApks() { | |
| 73 return mSupportWebApks; | |
| 74 } | |
| 75 } | |
| 76 | |
| 38 /** Listens to which browser is chosen by the user to launch WebAPK. */ | 77 /** Listens to which browser is chosen by the user to launch WebAPK. */ |
| 39 private DialogListener mListener; | 78 private DialogListener mListener; |
| 40 | 79 |
| 41 /** | 80 /** |
| 42 * Shows the dialog for choosing a host browser. | 81 * Shows the dialog for choosing a host browser. |
| 43 * @param activity The current activity in which to create the dialog. | 82 * @param context The current Context. |
| 44 * @param url URL of the WebAPK that is shown on the dialog. | 83 * @param listener The listener for the dialog. |
| 84 * @param infos The list of resolved infos of the browsers that is shown on the dialog. | |
|
pkotwicz
2017/06/05 19:18:20
Nit: "resolved infos" -> ResolveInfos
Xi Han
2017/06/06 15:12:28
Done.
| |
| 85 * @param url URL of the WebAPK for which the dialog is shown. | |
| 45 */ | 86 */ |
| 46 public void show(Activity activity, String url) { | 87 public static void show( |
| 47 if (!(activity instanceof DialogListener)) { | 88 Context context, final DialogListener listener, List<ResolveInfo> in fos, String url) { |
| 48 throw new IllegalArgumentException( | 89 final List<BrowserItem> browserItems = |
| 49 activity.toString() + " must implement DialogListener"); | 90 getBrowserInfosForHostBrowserSelection(context.getPackageManager (), infos); |
| 50 } | |
| 51 | |
| 52 mListener = (DialogListener) activity; | |
| 53 final List<WebApkUtils.BrowserItem> browserItems = | |
| 54 WebApkUtils.getBrowserInfosForHostBrowserSelection(activity.getP ackageManager()); | |
| 55 | 91 |
| 56 // The dialog contains: | 92 // The dialog contains: |
| 57 // 1) a description of the dialog. | 93 // 1) a description of the dialog. |
| 58 // 2) a list of browsers for user to choose from. | 94 // 2) a list of browsers for user to choose from. |
| 59 View view = | 95 View view = LayoutInflater.from(context).inflate(R.layout.choose_host_br owser_dialog, null); |
| 60 LayoutInflater.from(activity).inflate(R.layout.choose_host_brows er_dialog, null); | |
| 61 TextView desc = (TextView) view.findViewById(R.id.desc); | 96 TextView desc = (TextView) view.findViewById(R.id.desc); |
| 62 ListView browserList = (ListView) view.findViewById(R.id.browser_list); | 97 ListView browserList = (ListView) view.findViewById(R.id.browser_list); |
| 63 desc.setText(activity.getString(R.string.choose_host_browser, url)); | 98 desc.setText(context.getString(R.string.choose_host_browser, url)); |
| 64 browserList.setAdapter(new BrowserArrayAdapter(activity, browserItems)); | 99 browserList.setAdapter(new BrowserArrayAdapter(context, browserItems)); |
| 65 | 100 |
| 66 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWr apper( | 101 AlertDialog.Builder builder = new AlertDialog.Builder( |
| 67 activity, android.R.style.Theme_DeviceDefault_Light_Dialog)); | 102 new ContextThemeWrapper(context, android.R.style.Theme_DeviceDef ault_Light_Dialog)); |
| 68 builder.setTitle(activity.getString(R.string.choose_host_browser_dialog_ title, url)) | 103 builder.setTitle(context.getString(R.string.choose_host_browser_dialog_t itle, url)) |
| 69 .setView(view) | 104 .setView(view) |
| 70 .setNegativeButton(R.string.choose_host_browser_dialog_quit, | 105 .setNegativeButton(R.string.choose_host_browser_dialog_quit, |
| 71 new DialogInterface.OnClickListener() { | 106 new DialogInterface.OnClickListener() { |
| 72 @Override | 107 @Override |
| 73 public void onClick(DialogInterface dialog, int whic h) { | 108 public void onClick(DialogInterface dialog, int whic h) { |
| 74 mListener.onQuit(); | 109 listener.onQuit(); |
| 75 } | 110 } |
| 76 }); | 111 }); |
| 77 | 112 |
| 78 final AlertDialog dialog = builder.create(); | 113 final AlertDialog dialog = builder.create(); |
| 79 browserList.setOnItemClickListener(new AdapterView.OnItemClickListener() { | 114 browserList.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 80 @Override | 115 @Override |
| 81 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { | 116 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { |
| 82 WebApkUtils.BrowserItem browserItem = browserItems.get(position) ; | 117 BrowserItem browserItem = browserItems.get(position); |
| 83 if (browserItem.supportWebApks()) { | 118 if (browserItem.supportWebApks()) { |
| 84 mListener.onHostBrowserSelected(browserItem.getPackageName() ); | 119 listener.onHostBrowserSelected(browserItem.getPackageName()) ; |
| 85 dialog.cancel(); | 120 dialog.cancel(); |
| 86 } | 121 } |
| 87 } | 122 } |
| 88 }); | 123 }); |
| 89 | 124 |
| 90 dialog.show(); | 125 dialog.show(); |
| 91 }; | 126 }; |
| 92 | 127 |
| 128 /** Returns a list of BrowserItem for all of the installed browsers. */ | |
| 129 private static List<BrowserItem> getBrowserInfosForHostBrowserSelection( | |
| 130 PackageManager packageManager, List<ResolveInfo> resolveInfos) { | |
| 131 List<BrowserItem> browsers = new ArrayList<>(); | |
| 132 List<String> browsersSupportingWebApk = WebApkUtils.getBrowsersSupportin gWebApk(); | |
| 133 | |
| 134 for (ResolveInfo info : resolveInfos) { | |
| 135 browsers.add(new BrowserItem(info.activityInfo.packageName, | |
| 136 info.loadLabel(packageManager), info.loadIcon(packageManager ), | |
| 137 browsersSupportingWebApk.contains(info.activityInfo.packageN ame))); | |
| 138 } | |
| 139 return browsers; | |
|
pkotwicz
2017/06/05 19:18:19
A friendly reminder to add the Comparable logic fr
Xi Han
2017/06/06 15:12:28
Thanks!
| |
| 140 } | |
| 141 | |
| 93 /** Item adaptor for the list of browsers. */ | 142 /** Item adaptor for the list of browsers. */ |
| 94 private static class BrowserArrayAdapter extends ArrayAdapter<WebApkUtils.Br owserItem> { | 143 private static class BrowserArrayAdapter extends ArrayAdapter<BrowserItem> { |
| 95 private List<WebApkUtils.BrowserItem> mBrowsers; | 144 private List<BrowserItem> mBrowsers; |
| 96 private LayoutInflater mLayoutInflater; | 145 private LayoutInflater mLayoutInflater; |
| 97 | 146 |
| 98 public BrowserArrayAdapter(Context context, List<WebApkUtils.BrowserItem > browsers) { | 147 public BrowserArrayAdapter(Context context, List<BrowserItem> browsers) { |
| 99 super(context, R.layout.choose_host_browser_dialog_list, browsers); | 148 super(context, R.layout.choose_host_browser_dialog_list, browsers); |
| 100 mLayoutInflater = LayoutInflater.from(context); | 149 mLayoutInflater = LayoutInflater.from(context); |
| 101 mBrowsers = browsers; | 150 mBrowsers = browsers; |
| 102 } | 151 } |
| 103 | 152 |
| 104 @Override | 153 @Override |
| 105 public View getView(int position, View convertView, ViewGroup parent) { | 154 public View getView(int position, View convertView, ViewGroup parent) { |
| 106 if (convertView == null) { | 155 if (convertView == null) { |
| 107 convertView = | 156 convertView = |
| 108 mLayoutInflater.inflate(R.layout.choose_host_browser_dia log_list, null); | 157 mLayoutInflater.inflate(R.layout.choose_host_browser_dia log_list, null); |
| 109 } | 158 } |
| 110 | 159 |
| 111 TextView name = (TextView) convertView.findViewById(R.id.browser_nam e); | 160 TextView name = (TextView) convertView.findViewById(R.id.browser_nam e); |
| 112 ImageView icon = (ImageView) convertView.findViewById(R.id.browser_i con); | 161 ImageView icon = (ImageView) convertView.findViewById(R.id.browser_i con); |
| 113 WebApkUtils.BrowserItem item = mBrowsers.get(position); | 162 BrowserItem item = mBrowsers.get(position); |
| 114 | 163 |
| 115 name.setText(item.getApplicationName()); | 164 name.setText(item.getApplicationName()); |
| 116 name.setEnabled(item.supportWebApks()); | 165 name.setEnabled(item.supportWebApks()); |
| 117 icon.setImageDrawable(item.getApplicationIcon()); | 166 icon.setImageDrawable(item.getApplicationIcon()); |
| 118 icon.setEnabled(item.supportWebApks()); | 167 icon.setEnabled(item.supportWebApks()); |
| 119 return convertView; | 168 return convertView; |
| 120 } | 169 } |
| 121 | 170 |
| 122 @Override | 171 @Override |
| 123 public boolean isEnabled(int position) { | 172 public boolean isEnabled(int position) { |
| 124 return mBrowsers.get(position).supportWebApks(); | 173 return mBrowsers.get(position).supportWebApks(); |
| 125 } | 174 } |
| 126 } | 175 } |
| 127 } | 176 } |
| OLD | NEW |