OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.webapk.shell_apk; | |
6 | |
7 import android.app.Activity; | |
8 import android.app.AlertDialog; | |
9 import android.content.Context; | |
10 import android.content.DialogInterface; | |
11 import android.view.ContextThemeWrapper; | |
12 import android.view.LayoutInflater; | |
13 import android.view.View; | |
14 import android.view.ViewGroup; | |
15 import android.widget.AdapterView; | |
16 import android.widget.ArrayAdapter; | |
17 import android.widget.ImageView; | |
18 import android.widget.ListView; | |
19 import android.widget.TextView; | |
20 | |
21 import java.util.List; | |
22 | |
23 /** | |
24 * Shows the dialog to choose a host browser to launch WebAPK. Calls the listene r callback when the | |
25 * host browser is chosen. | |
26 */ | |
27 | |
28 public class ChooseHostBrowserDialog { | |
Ted C
2017/06/01 15:18:44
Sorry for being slow to review, but any thoughts o
Xi Han
2017/06/01 17:14:59
Thanks for pointing out this class, I didn't know
| |
29 /** | |
30 * A listener to receive updates when user chooses a host browser for the We bAPK, or dismiss the | |
31 * dialog. | |
32 */ | |
33 public interface DialogListener { | |
34 void onHostBrowserSelected(String packageName); | |
35 void onQuit(); | |
36 } | |
37 | |
38 /** Listens to which browser is chosen by the user to launch WebAPK. */ | |
39 private DialogListener mListener; | |
40 | |
41 /** | |
42 * Shows the dialog for choosing a host browser. | |
43 * @param activity The current activity in which to create the dialog. | |
44 * @param url URL of the WebAPK that is shown on the dialog. | |
45 */ | |
46 public void show(Activity activity, String url) { | |
Ted C
2017/06/01 15:18:44
I don't recall my generics well enough, but would
Xi Han
2017/06/01 17:14:59
SG, thanks!
Ted C
2017/06/05 18:24:48
From pkotwicz's comment here (https://codereview.c
Xi Han
2017/06/05 20:30:02
Sorry for the misunderstanding.
| |
47 try { | |
48 mListener = (DialogListener) activity; | |
49 } catch (ClassCastException e) { | |
Ted C
2017/06/01 15:18:43
instead of catching the exception, I would do:
if
Xi Han
2017/06/01 17:14:59
Done.
| |
50 throw new ClassCastException(activity.toString() + " must implement DialogListener"); | |
51 } | |
52 | |
53 final List<WebApkUtils.BrowserItem> browserItems = | |
54 WebApkUtils.getBrowserInfosForHostBrowserSelection(activity.getP ackageManager()); | |
55 | |
56 // The dialog contains: | |
57 // 1) a description of the dialog. | |
58 // 2) a list of browsers for user to choose from. | |
59 View view = | |
60 LayoutInflater.from(activity).inflate(R.layout.choose_host_brows er_dialog, null); | |
61 TextView desc = (TextView) view.findViewById(R.id.desc); | |
62 ListView browserList = (ListView) view.findViewById(R.id.browser_list); | |
63 desc.setText(activity.getString(R.string.choose_host_browser, url)); | |
64 browserList.setAdapter(new BrowserArrayAdapter(activity, browserItems)); | |
65 | |
66 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWr apper( | |
67 activity, android.R.style.Theme_DeviceDefault_Light_Dialog)); | |
Ted C
2017/06/01 15:18:44
Why do you need the ContextThemeWrapper? Does the
Xi Han
2017/06/01 17:14:59
No, on KK (probably pre-L), the UX looks like two
| |
68 builder.setTitle(activity.getString(R.string.choose_host_browser_dialog_ title, url)) | |
69 .setView(view) | |
70 .setNegativeButton(R.string.choose_host_browser_dialog_quit, | |
71 new DialogInterface.OnClickListener() { | |
72 @Override | |
73 public void onClick(DialogInterface dialog, int whic h) { | |
74 mListener.onQuit(); | |
75 } | |
76 }); | |
77 | |
78 final AlertDialog dialog = builder.create(); | |
79 browserList.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
80 @Override | |
81 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { | |
82 WebApkUtils.BrowserItem browserItem = browserItems.get(position) ; | |
83 if (browserItem.supportWebApks()) { | |
84 mListener.onHostBrowserSelected(browserItem.getPackageName() ); | |
85 dialog.cancel(); | |
86 } | |
87 } | |
88 }); | |
89 | |
90 dialog.show(); | |
91 }; | |
92 | |
93 /** Item adaptor for the list of browsers. */ | |
94 private static class BrowserArrayAdapter extends ArrayAdapter<WebApkUtils.Br owserItem> { | |
95 private List<WebApkUtils.BrowserItem> mBrowsers; | |
96 private LayoutInflater mLayoutInflater; | |
97 | |
98 public BrowserArrayAdapter(Context context, List<WebApkUtils.BrowserItem > browsers) { | |
99 super(context, R.layout.choose_host_browser_dialog_list, browsers); | |
100 mLayoutInflater = LayoutInflater.from(context); | |
101 mBrowsers = browsers; | |
102 } | |
103 | |
104 @Override | |
105 public View getView(int position, View convertView, ViewGroup parent) { | |
106 if (convertView == null) { | |
107 convertView = | |
108 mLayoutInflater.inflate(R.layout.choose_host_browser_dia log_list, null); | |
109 } | |
110 | |
111 TextView name = (TextView) convertView.findViewById(R.id.browser_nam e); | |
112 ImageView icon = (ImageView) convertView.findViewById(R.id.browser_i con); | |
113 WebApkUtils.BrowserItem item = mBrowsers.get(position); | |
114 | |
115 name.setText(item.getApplicationName()); | |
116 name.setEnabled(item.supportWebApks()); | |
117 icon.setImageDrawable(item.getApplicationIcon()); | |
118 icon.setEnabled(item.supportWebApks()); | |
119 return convertView; | |
120 } | |
121 | |
122 @Override | |
123 public boolean isEnabled(int position) { | |
124 return mBrowsers.get(position).supportWebApks(); | |
125 } | |
126 } | |
127 } | |
OLD | NEW |