Chromium Code Reviews| 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.AlertDialog; | |
| 8 import android.app.Dialog; | |
| 9 import android.content.Context; | |
| 10 import android.content.DialogInterface; | |
| 11 import android.os.Bundle; | |
| 12 import android.support.v4.app.DialogFragment; | |
| 13 import android.view.ContextThemeWrapper; | |
| 14 import android.view.LayoutInflater; | |
| 15 import android.view.View; | |
| 16 import android.view.ViewGroup; | |
| 17 import android.widget.AdapterView; | |
| 18 import android.widget.AdapterView.OnItemClickListener; | |
| 19 import android.widget.ArrayAdapter; | |
| 20 import android.widget.ImageView; | |
| 21 import android.widget.ListView; | |
| 22 import android.widget.TextView; | |
| 23 | |
| 24 import java.util.List; | |
| 25 | |
| 26 /** | |
| 27 * Shows the dialog to choose a host browser to launch WebAPKs. Calls the listen er callback when the | |
| 28 * host browser is chosen. | |
| 29 */ | |
| 30 public class ChooseHostBrowserDialogFragment extends DialogFragment { | |
| 31 /** | |
| 32 * A listener to receive updates when user chooses a host browser for the We bAPK, or dismiss the | |
| 33 * dialog. | |
| 34 */ | |
| 35 public interface DialogListener { | |
| 36 void onHostBrowserSelected(String packageName); | |
| 37 void onQuit(); | |
| 38 } | |
| 39 | |
| 40 private static final String EXTRA_URL = "url"; | |
| 41 | |
| 42 // Listens to which browser is chosen by the user to launch WebAPKs. | |
| 43 private DialogListener mListener; | |
| 44 | |
| 45 public static ChooseHostBrowserDialogFragment newInstance(String url) { | |
| 46 ChooseHostBrowserDialogFragment dialogFragment = new ChooseHostBrowserDi alogFragment(); | |
| 47 Bundle args = new Bundle(); | |
| 48 args.putString(EXTRA_URL, url); | |
| 49 dialogFragment.setArguments(args); | |
| 50 | |
| 51 return dialogFragment; | |
| 52 } | |
| 53 | |
| 54 @Override | |
| 55 public Dialog onCreateDialog(Bundle savedInstanceState) { | |
| 56 String url = getArguments().getString(EXTRA_URL); | |
| 57 final List<WebApkUtils.BrowserItem> browserItems = | |
| 58 WebApkUtils.getBrowserInfosForHostBrowserSelection( | |
| 59 getActivity().getPackageManager()); | |
| 60 | |
| 61 // The dialog content contains: | |
|
pkotwicz
2017/05/24 04:19:29
Nit: "The dialog contains:"
http://www.dictionary
Xi Han
2017/05/24 16:52:36
Thanks!
| |
| 62 // 1) a description of the dialog; | |
|
pkotwicz
2017/05/24 04:19:29
Nit: ';' -> '.'
Xi Han
2017/05/24 16:52:36
Done.
| |
| 63 // 2) a list of browsers for user to choose from. | |
| 64 View view = LayoutInflater.from(getActivity()) | |
| 65 .inflate(R.layout.choose_host_browser_dialog, null); | |
| 66 ContentViewHolder contentViewHolder = new ContentViewHolder(); | |
| 67 contentViewHolder.mDesc = (TextView) view.findViewById(R.id.desc); | |
| 68 contentViewHolder.mDesc.setText(getActivity().getString(R.string.choose_ host_browser, url)); | |
| 69 contentViewHolder.mBrowserList = (ListView) view.findViewById(R.id.brows er_list); | |
| 70 contentViewHolder.mBrowserList.setAdapter( | |
| 71 new BrowserArrayAdapter(getActivity(), browserItems)); | |
| 72 view.setTag(contentViewHolder); | |
| 73 | |
| 74 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWr apper( | |
| 75 getActivity(), android.R.style.Theme_DeviceDefault_Light_Dialog) ); | |
| 76 builder.setTitle(getActivity().getString(R.string.choose_host_browser_di alog_title, url)) | |
| 77 .setView(view) | |
| 78 .setNegativeButton(R.string.choose_host_browser_dialog_cancel, | |
| 79 new DialogInterface.OnClickListener() { | |
| 80 @Override | |
| 81 public void onClick(DialogInterface dialog, int whic h) { | |
| 82 mListener.onQuit(); | |
| 83 } | |
| 84 }); | |
| 85 | |
| 86 final AlertDialog dialog = builder.create(); | |
| 87 contentViewHolder.mBrowserList.setOnItemClickListener(new OnItemClickLis tener() { | |
| 88 @Override | |
| 89 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { | |
| 90 WebApkUtils.BrowserItem browserItem = browserItems.get(position) ; | |
| 91 if (browserItem.isEnabled()) { | |
| 92 mListener.onHostBrowserSelected(browserItem.getPackageName() ); | |
| 93 dialog.cancel(); | |
| 94 } | |
| 95 } | |
| 96 }); | |
| 97 | |
| 98 return dialog; | |
| 99 }; | |
| 100 | |
| 101 @Override | |
| 102 public void onAttach(Context context) { | |
|
pkotwicz
2017/05/24 04:19:29
Would it make more sense to pass a listener as an
Xi Han
2017/05/24 16:52:37
The newInstance() can only interactive with the ne
pkotwicz
2017/05/25 02:59:08
Ok, I see now. Thank you for the explanation.
| |
| 103 super.onAttach(context); | |
| 104 try { | |
| 105 mListener = (DialogListener) context; | |
| 106 } catch (ClassCastException e) { | |
| 107 throw new ClassCastException( | |
| 108 context.toString() + "must implement SelectionDialogListener "); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 /** View holder for the content of the dialog. */ | |
| 113 private static class ContentViewHolder { | |
| 114 TextView mDesc; | |
| 115 ListView mBrowserList; | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Item holder for the browser list. Each item contains the icon and applica tion name of the | |
| 120 * browser. | |
| 121 */ | |
| 122 private static class BrowserListViewHolder { | |
| 123 ImageView mIcon; | |
| 124 TextView mName; | |
| 125 } | |
| 126 | |
| 127 /** | |
| 128 * Item adaptor for the list of browsers. | |
| 129 */ | |
| 130 private static class BrowserArrayAdapter extends ArrayAdapter<WebApkUtils.Br owserItem> { | |
| 131 private List<WebApkUtils.BrowserItem> mBrowsers; | |
| 132 private Context mContext; | |
| 133 | |
| 134 public BrowserArrayAdapter(Context context, List<WebApkUtils.BrowserItem > browsers) { | |
| 135 super(context, R.layout.choose_host_browser_dialog_list, browsers); | |
| 136 mContext = context; | |
| 137 mBrowsers = browsers; | |
| 138 } | |
|
pkotwicz
2017/05/24 04:19:29
Random question: Does overriding ListAdapter#isEna
Xi Han
2017/05/24 16:52:36
Ah, I don't know there is ListAdapter#isEnabled(in
pkotwicz
2017/05/25 02:59:08
Thank you for verifying!
| |
| 139 | |
| 140 @Override | |
| 141 public View getView(int position, View convertView, ViewGroup parent) { | |
| 142 BrowserListViewHolder holder; | |
| 143 | |
| 144 if (convertView == null) { | |
| 145 convertView = LayoutInflater.from(mContext).inflate( | |
| 146 R.layout.choose_host_browser_dialog_list, null); | |
| 147 holder = new BrowserListViewHolder(); | |
| 148 holder.mName = (TextView) convertView.findViewById(R.id.browser_ name); | |
| 149 holder.mIcon = (ImageView) convertView.findViewById(R.id.browser _icon); | |
| 150 convertView.setTag(holder); | |
| 151 } else { | |
| 152 holder = (BrowserListViewHolder) convertView.getTag(); | |
|
pkotwicz
2017/05/24 04:19:29
For the sake of interest, why are you using View#g
Xi Han
2017/05/24 16:52:36
I do use convertView.getTag(). Can you elaborate m
| |
| 153 } | |
| 154 | |
| 155 WebApkUtils.BrowserItem item = mBrowsers.get(position); | |
| 156 holder.mName.setText(item.getApplicationName()); | |
| 157 holder.mIcon.setImageDrawable(item.getApplicationIcon()); | |
| 158 if (item.isEnabled()) { | |
| 159 holder.mName.setEnabled(true); | |
| 160 holder.mIcon.setEnabled(true); | |
| 161 } else { | |
| 162 holder.mName.setEnabled(false); | |
| 163 holder.mIcon.setEnabled(false); | |
| 164 } | |
| 165 | |
| 166 return convertView; | |
| 167 } | |
| 168 } | |
| 169 } | |
| OLD | NEW |