OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.graphics.drawable.Drawable; |
10 import android.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
13 import android.widget.AdapterView; | 13 import android.widget.AdapterView; |
14 import android.widget.ArrayAdapter; | 14 import android.widget.ArrayAdapter; |
15 import android.widget.ListView; | 15 import android.widget.ListView; |
| 16 import android.widget.TextView; |
16 | 17 |
17 import org.chromium.chromoting.help.CreditsActivity; | 18 import org.chromium.base.ApiCompatibilityUtils; |
18 import org.chromium.chromoting.help.HelpContext; | 19 import org.chromium.chromoting.help.HelpContext; |
19 import org.chromium.chromoting.help.HelpSingleton; | 20 import org.chromium.chromoting.help.HelpSingleton; |
20 | 21 |
21 /** | 22 /** |
22 * Describes the appearance and behavior of the navigation menu. This also imple
ments | 23 * Describes the appearance and behavior of the navigation menu. This also imple
ments |
23 * AdapterView.OnItemClickListener so it can be used as the ListView's onItemCli
ckListener. | 24 * AdapterView.OnItemClickListener so it can be used as the ListView's onItemCli
ckListener. |
24 */ | 25 */ |
25 public class NavigationMenuAdapter extends ArrayAdapter<NavigationMenuAdapter.Na
vigationMenuItem> | 26 public class NavigationMenuAdapter extends ArrayAdapter<NavigationMenuAdapter.Na
vigationMenuItem> |
26 implements AdapterView.OnItemClickListener { | 27 implements AdapterView.OnItemClickListener { |
27 /** | 28 /** |
28 * Defines a menu item. | 29 * Defines a menu item. |
29 */ | 30 */ |
30 public static class NavigationMenuItem { | 31 public static class NavigationMenuItem { |
31 private int mLayoutResourceId; | 32 private String mText; |
| 33 private Drawable mIcon; |
32 private Runnable mCallback; | 34 private Runnable mCallback; |
33 public NavigationMenuItem(int layoutResourceId, Runnable callback) { | 35 |
34 mLayoutResourceId = layoutResourceId; | 36 public NavigationMenuItem(String text, Drawable icon, Runnable callback)
{ |
| 37 mText = text; |
| 38 mIcon = icon; |
35 mCallback = callback; | 39 mCallback = callback; |
36 } | 40 } |
37 } | 41 } |
38 | 42 |
39 public static ListView createNavigationMenu(final Activity activity) { | 43 public static ListView createNavigationMenu(final Activity activity) { |
40 ListView navigationMenu = (ListView) activity.getLayoutInflater() | 44 ListView navigationMenu = (ListView) activity.getLayoutInflater() |
41 .inflate(R.layout.navigation_list, null); | 45 .inflate(R.layout.navigation_list, null); |
42 | 46 |
43 NavigationMenuItem creditsItem = new NavigationMenuItem(R.menu.credits_l
ist_item, | 47 NavigationMenuItem feedbackItem = new NavigationMenuItem( |
| 48 activity.getResources().getString(R.string.actionbar_send_feedba
ck), |
| 49 getIcon(activity, R.drawable.ic_announcement), |
44 new Runnable() { | 50 new Runnable() { |
45 @Override | 51 @Override |
46 public void run() { | 52 public void run() { |
47 activity.startActivity(new Intent(activity, CreditsActiv
ity.class)); | |
48 } | |
49 }); | |
50 | |
51 NavigationMenuItem feedbackItem = new NavigationMenuItem(R.menu.feedback
_list_item, | |
52 new Runnable() { | |
53 @Override | |
54 public void run() { | |
55 HelpSingleton.getInstance().launchFeedback(activity); | 53 HelpSingleton.getInstance().launchFeedback(activity); |
56 } | 54 } |
57 }); | 55 }); |
58 | 56 |
59 NavigationMenuItem helpItem = new NavigationMenuItem(R.menu.help_list_it
em, | 57 NavigationMenuItem helpItem = new NavigationMenuItem( |
| 58 activity.getResources().getString(R.string.actionbar_help), |
| 59 getIcon(activity, R.drawable.ic_help), |
60 new Runnable() { | 60 new Runnable() { |
61 @Override | 61 @Override |
62 public void run() { | 62 public void run() { |
63 HelpSingleton.getInstance().launchHelp(activity, | 63 HelpSingleton.getInstance().launchHelp(activity, |
64 HelpContext.HOST_LIST); | 64 HelpContext.HOST_LIST); |
65 } | 65 } |
66 }); | 66 }); |
67 | 67 |
68 NavigationMenuItem[] navigationMenuItems = { creditsItem, feedbackItem,
helpItem }; | 68 NavigationMenuItem[] navigationMenuItems = { feedbackItem, helpItem }; |
69 NavigationMenuAdapter adapter = new NavigationMenuAdapter(activity, navi
gationMenuItems); | 69 NavigationMenuAdapter adapter = new NavigationMenuAdapter(activity, navi
gationMenuItems); |
70 navigationMenu.setAdapter(adapter); | 70 navigationMenu.setAdapter(adapter); |
71 navigationMenu.setOnItemClickListener(adapter); | 71 navigationMenu.setOnItemClickListener(adapter); |
72 return navigationMenu; | 72 return navigationMenu; |
73 } | 73 } |
74 | 74 |
75 public NavigationMenuAdapter(Context context, NavigationMenuItem[] objects)
{ | 75 /** Returns the drawable of |drawableId| that can be used to draw icon for a
navigation item. */ |
| 76 private static Drawable getIcon(Activity activity, int drawableId) { |
| 77 Drawable drawable = ApiCompatibilityUtils.getDrawable(activity.getResour
ces(), drawableId); |
| 78 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntri
nsicHeight()); |
| 79 return drawable; |
| 80 } |
| 81 |
| 82 private NavigationMenuAdapter(Context context, NavigationMenuItem[] objects)
{ |
76 super(context, -1, objects); | 83 super(context, -1, objects); |
77 } | 84 } |
78 | 85 |
79 /** Generates a View corresponding to the particular navigation item. */ | 86 /** Generates a View corresponding to the particular navigation item. */ |
80 @Override | 87 @Override |
81 public View getView(int position, View convertView, ViewGroup parent) { | 88 public View getView(int position, View convertView, ViewGroup parent) { |
82 if (convertView == null) { | 89 if (convertView == null) { |
83 NavigationMenuItem item = getItem(position); | |
84 LayoutInflater inflater = | 90 LayoutInflater inflater = |
85 (LayoutInflater) getContext().getSystemService(Context.LAYOU
T_INFLATER_SERVICE); | 91 (LayoutInflater) getContext().getSystemService(Context.LAYOU
T_INFLATER_SERVICE); |
86 convertView = inflater.inflate(item.mLayoutResourceId, null); | 92 convertView = inflater.inflate(R.layout.navigation_list_item, parent
, false); |
87 } | 93 } |
| 94 TextView textView = (TextView) convertView.findViewById(R.id.navigation_
item_label); |
| 95 NavigationMenuItem item = getItem(position); |
| 96 textView.setCompoundDrawables(item.mIcon, null, null, null); |
| 97 textView.setText(item.mText); |
88 return convertView; | 98 return convertView; |
89 } | 99 } |
90 | 100 |
91 | 101 |
92 /** AdapterView.OnItemClickListener override. */ | 102 /** AdapterView.OnItemClickListener override. */ |
93 @Override | 103 @Override |
94 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { | 104 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { |
95 getItem(position).mCallback.run(); | 105 getItem(position).mCallback.run(); |
96 } | 106 } |
97 } | 107 } |
OLD | NEW |