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 | |
|
pkotwicz
2017/05/26 22:38:39
Nit: WebAPKs -> WebAPK
Xi Han
2017/05/29 21:18:55
Done.
| |
| 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. | |
|
pkotwicz
2017/05/26 22:38:39
Nit: WebAPKs -> WebAPK
Xi Han
2017/05/29 21:18:55
Done.
| |
| 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 contains: | |
| 62 // 1) a description of the dialog. | |
| 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 TextView desc = (TextView) view.findViewById(R.id.desc); | |
| 67 ListView browserList = (ListView) view.findViewById(R.id.browser_list); | |
| 68 desc.setText(getActivity().getString(R.string.choose_host_browser, url)) ; | |
| 69 browserList.setAdapter(new BrowserArrayAdapter(getActivity(), browserIte ms)); | |
| 70 | |
| 71 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWr apper( | |
| 72 getActivity(), android.R.style.Theme_DeviceDefault_Light_Dialog) ); | |
| 73 builder.setTitle(getActivity().getString(R.string.choose_host_browser_di alog_title, url)) | |
| 74 .setView(view) | |
| 75 .setNegativeButton(R.string.choose_host_browser_dialog_quit, | |
| 76 new DialogInterface.OnClickListener() { | |
| 77 @Override | |
| 78 public void onClick(DialogInterface dialog, int whic h) { | |
| 79 mListener.onQuit(); | |
| 80 } | |
| 81 }); | |
| 82 | |
| 83 final AlertDialog dialog = builder.create(); | |
| 84 browserList.setOnItemClickListener(new OnItemClickListener() { | |
| 85 @Override | |
| 86 public void onItemClick(AdapterView<?> parent, View view, int positi on, long id) { | |
| 87 WebApkUtils.BrowserItem browserItem = browserItems.get(position) ; | |
| 88 if (browserItem.isEnabled()) { | |
| 89 mListener.onHostBrowserSelected(browserItem.getPackageName() ); | |
| 90 dialog.cancel(); | |
| 91 } | |
| 92 } | |
| 93 }); | |
| 94 | |
| 95 return dialog; | |
| 96 }; | |
| 97 | |
| 98 @Override | |
| 99 public void onAttach(Context context) { | |
| 100 super.onAttach(context); | |
| 101 try { | |
| 102 mListener = (DialogListener) context; | |
| 103 } catch (ClassCastException e) { | |
| 104 throw new ClassCastException(context.toString() + "must implement Di alogListener"); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * Item adaptor for the list of browsers. | |
| 110 */ | |
| 111 private static class BrowserArrayAdapter extends ArrayAdapter<WebApkUtils.Br owserItem> { | |
| 112 private List<WebApkUtils.BrowserItem> mBrowsers; | |
| 113 private Context mContext; | |
| 114 | |
| 115 public BrowserArrayAdapter(Context context, List<WebApkUtils.BrowserItem > browsers) { | |
| 116 super(context, R.layout.choose_host_browser_dialog_list, browsers); | |
| 117 mContext = context; | |
| 118 mBrowsers = browsers; | |
| 119 } | |
| 120 | |
| 121 @Override | |
| 122 public View getView(int position, View convertView, ViewGroup parent) { | |
| 123 if (convertView == null) { | |
| 124 convertView = LayoutInflater.from(mContext).inflate( | |
| 125 R.layout.choose_host_browser_dialog_list, null); | |
| 126 } | |
| 127 | |
| 128 WebApkUtils.BrowserItem item = mBrowsers.get(position); | |
| 129 TextView name = (TextView) convertView.findViewById(R.id.browser_nam e); | |
| 130 ImageView icon = (ImageView) convertView.findViewById(R.id.browser_i con); | |
| 131 name.setText(item.getApplicationName()); | |
| 132 name.setEnabled(item.isEnabled()); | |
| 133 icon.setImageDrawable(item.getApplicationIcon()); | |
| 134 return convertView; | |
| 135 } | |
| 136 | |
| 137 @Override | |
| 138 public boolean isEnabled(int position) { | |
| 139 return mBrowsers.get(position).isEnabled(); | |
| 140 } | |
| 141 } | |
| 142 } | |
| OLD | NEW |