blob: ae638cb8e5ba414a2277b66a0f01a08d22607262 [file] [log] [blame]
Christine Chen0ce0e852013-08-09 18:26:31 -07001/*
2
3 * Copyright (C) 2013 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.phone;
19
20import android.app.Activity;
21import android.app.ActivityManager;
22import android.app.AlertDialog;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.content.SharedPreferences;
28import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
30import android.graphics.drawable.Drawable;
31import android.os.Bundle;
32import android.util.Log;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.view.ViewGroup;
36import android.widget.BaseAdapter;
37import android.widget.CheckBox;
38import android.widget.CompoundButton;
39import android.widget.ImageView;
40import android.widget.TextView;
41
42import java.util.ArrayList;
43import java.util.List;
44
45public class TextMessagePackageChooser extends Activity {
46 private static final String TAG = TextMessagePackageChooser.class.getSimpleName();
47
48 /** SharedPreferences file name for our persistent settings. */
49 private static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs";
50
51 private int mIconSize = -1;
52
53 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 final ArrayList<ComponentName> components = getIntent().getParcelableArrayListExtra(
57 RejectWithTextMessageManager.TAG_ALL_SMS_SERVICES);
58 BaseAdapter adapter = new PackageSelectionAdapter(this, components);
59
60 PackageClickListener clickListener = new PackageClickListener(components);
61
62 final CharSequence title = getResources().getText(
63 com.android.internal.R.string.whichApplication);
64 LayoutInflater inflater =
65 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
66
67 final View view = inflater.inflate(com.android.internal.R.layout.always_use_checkbox,
68 null);
69 final CheckBox alwaysUse = (CheckBox) view.findViewById(
70 com.android.internal.R.id.alwaysUse);
71 alwaysUse.setText(com.android.internal.R.string.alwaysUse);
72 alwaysUse.setOnCheckedChangeListener(clickListener);
73
74 AlertDialog.Builder builder = new AlertDialog.Builder(this)
75 .setTitle(title)
76 .setCancelable(true)
77 .setOnCancelListener(new RespondViaSmsCancelListener())
78 .setAdapter(adapter, clickListener)
79 .setView(view);
80
81 builder.create().show();
82 }
83
84 private class PackageSelectionAdapter extends BaseAdapter {
85 private final LayoutInflater mInflater;
86 private final List<ComponentName> mComponents;
87
88 public PackageSelectionAdapter(Context context, List<ComponentName> components) {
89 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
90 mComponents = components;
91 }
92
93 @Override
94 public int getCount() {
95 return mComponents.size();
96 }
97
98 @Override
99 public Object getItem(int position) {
100 return mComponents.get(position);
101 }
102
103 @Override
104 public long getItemId(int position) {
105 return position;
106 }
107
108 @Override
109 public View getView(int position, View convertView, ViewGroup parent) {
110 if (convertView == null) {
111 convertView = mInflater.inflate(
112 com.android.internal.R.layout.activity_chooser_view_list_item, parent,
113 false);
114 }
115
116 final ComponentName component = mComponents.get(position);
117 final String packageName = component.getPackageName();
118 final PackageManager packageManager = getPackageManager();
119
120 // Set the application label
121 final TextView text = (TextView) convertView.findViewById(
122 com.android.internal.R.id.title);
123
124 text.setText("");
125 try {
126 final ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, 0);
127 final CharSequence label = packageManager.getApplicationLabel(appInfo);
128 if (label != null) {
129 text.setText(label);
130 }
131 } catch (PackageManager.NameNotFoundException e) {
132 Log.w(TAG, "Failed to load app label because package was not found.");
133 }
134
135 // Set the application icon
136 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
137 Drawable drawable = null;
138 try {
139 drawable = getPackageManager().getApplicationIcon(packageName);
140 } catch (PackageManager.NameNotFoundException e) {
141 Log.w(TAG, "Failed to load icon because it wasn't found.");
142 }
143 if (drawable == null) {
144 drawable = getPackageManager().getDefaultActivityIcon();
145 }
146 icon.setImageDrawable(drawable);
147 ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) icon.getLayoutParams();
148 lp.width = lp.height = getIconSize();
149
150 return convertView;
151 }
152
153 }
154
155 private class PackageClickListener implements DialogInterface.OnClickListener,
156 CompoundButton.OnCheckedChangeListener {
157 final private List<ComponentName> mComponents;
158 private boolean mMakeDefault = false;
159
160 public PackageClickListener(List<ComponentName> components) {
161 mComponents = components;
162 }
163
164 @Override
165 public void onClick(DialogInterface dialog, int which) {
166 final ComponentName component = mComponents.get(which);
167
168 if (mMakeDefault) {
169 final SharedPreferences prefs = PhoneGlobals.getInstance().getSharedPreferences(
170 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
171 prefs.edit().putString(
172 RejectWithTextMessageManager.KEY_INSTANT_TEXT_DEFAULT_COMPONENT,
173 component.flattenToString()).apply();
174 }
175
176 final Intent messageIntent = (Intent) getIntent().getParcelableExtra(
177 RejectWithTextMessageManager.TAG_SEND_SMS);
178 if (messageIntent != null) {
179 messageIntent.setComponent(component);
180 PhoneGlobals.getInstance().startService(messageIntent);
181 }
182 finish();
183 }
184
185 @Override
186 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
187 Log.i(TAG, "mMakeDefault : " + isChecked);
188 mMakeDefault = isChecked;
189 }
190 }
191
192 /**
193 * OnCancelListener for the "Respond via SMS" popup.
194 */
195 public class RespondViaSmsCancelListener implements DialogInterface.OnCancelListener {
196 public RespondViaSmsCancelListener() {
197 }
198
199 /**
200 * Handles the user canceling the popup, either by touching
201 * outside the popup or by pressing Back.
202 */
203 @Override
204 public void onCancel(DialogInterface dialog) {
205 finish();
206 }
207 }
208
209 private int getIconSize() {
210 if (mIconSize < 0) {
211 final ActivityManager am =
212 (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
213 mIconSize = am.getLauncherLargeIconSize();
214 }
215
216 return mIconSize;
217 }
218}