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.graphics.Color; |
| 12 import android.view.ContextThemeWrapper; |
| 13 import android.view.LayoutInflater; |
| 14 import android.view.View; |
| 15 import android.view.ViewGroup; |
| 16 import android.widget.AdapterView; |
| 17 import android.widget.ArrayAdapter; |
| 18 import android.widget.ImageView; |
| 19 import android.widget.ListView; |
| 20 import android.widget.TextView; |
| 21 |
| 22 import java.util.List; |
| 23 |
| 24 /** |
| 25 * Shows the dialog to choose a host browser to launch WebAPK. Calls the listene
r callback when the |
| 26 * host browser is chosen. |
| 27 */ |
| 28 public class ChooseHostBrowserDialog { |
| 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) { |
| 47 if (!(activity instanceof DialogListener)) { |
| 48 throw new IllegalArgumentException( |
| 49 activity.toString() + " must implement DialogListener"); |
| 50 } |
| 51 |
| 52 mListener = (DialogListener) activity; |
| 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, u
rl)); |
| 65 |
| 66 // The context theme wrapper is needed for pre-L. |
| 67 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWr
apper( |
| 68 activity, android.R.style.Theme_DeviceDefault_Light_Dialog)); |
| 69 builder.setTitle(activity.getString(R.string.choose_host_browser_dialog_
title, url)) |
| 70 .setView(view) |
| 71 .setNegativeButton(R.string.choose_host_browser_dialog_quit, |
| 72 new DialogInterface.OnClickListener() { |
| 73 @Override |
| 74 public void onClick(DialogInterface dialog, int whic
h) { |
| 75 mListener.onQuit(); |
| 76 } |
| 77 }); |
| 78 |
| 79 final AlertDialog dialog = builder.create(); |
| 80 browserList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{ |
| 81 @Override |
| 82 public void onItemClick(AdapterView<?> parent, View view, int positi
on, long id) { |
| 83 WebApkUtils.BrowserItem browserItem = browserItems.get(position)
; |
| 84 if (browserItem.supportsWebApks()) { |
| 85 mListener.onHostBrowserSelected(browserItem.getPackageName()
); |
| 86 dialog.cancel(); |
| 87 } |
| 88 } |
| 89 }); |
| 90 |
| 91 dialog.show(); |
| 92 }; |
| 93 |
| 94 /** Item adaptor for the list of browsers. */ |
| 95 private static class BrowserArrayAdapter extends ArrayAdapter<WebApkUtils.Br
owserItem> { |
| 96 private List<WebApkUtils.BrowserItem> mBrowsers; |
| 97 private Context mContext; |
| 98 private String mUrl; |
| 99 |
| 100 public BrowserArrayAdapter( |
| 101 Context context, List<WebApkUtils.BrowserItem> browsers, String
url) { |
| 102 super(context, R.layout.choose_host_browser_dialog_list, browsers); |
| 103 mContext = context; |
| 104 mBrowsers = browsers; |
| 105 mUrl = url; |
| 106 } |
| 107 |
| 108 @Override |
| 109 public View getView(int position, View convertView, ViewGroup parent) { |
| 110 if (convertView == null) { |
| 111 convertView = LayoutInflater.from(mContext).inflate( |
| 112 R.layout.choose_host_browser_dialog_list, null); |
| 113 } |
| 114 |
| 115 TextView name = (TextView) convertView.findViewById(R.id.browser_nam
e); |
| 116 ImageView icon = (ImageView) convertView.findViewById(R.id.browser_i
con); |
| 117 WebApkUtils.BrowserItem item = mBrowsers.get(position); |
| 118 |
| 119 name.setEnabled(item.supportsWebApks()); |
| 120 if (item.supportsWebApks()) { |
| 121 name.setText(item.getApplicationName()); |
| 122 name.setTextColor(Color.BLACK); |
| 123 } else { |
| 124 name.setText(mContext.getString(R.string.host_browser_item_not_s
upporting_webapks, |
| 125 item.getApplicationName(), mUrl)); |
| 126 name.setSingleLine(false); |
| 127 name.setTextColor(Color.LTGRAY); |
| 128 } |
| 129 icon.setImageDrawable(item.getApplicationIcon()); |
| 130 icon.setEnabled(item.supportsWebApks()); |
| 131 return convertView; |
| 132 } |
| 133 |
| 134 @Override |
| 135 public boolean isEnabled(int position) { |
| 136 return mBrowsers.get(position).supportsWebApks(); |
| 137 } |
| 138 } |
| 139 } |
OLD | NEW |