2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.provider.Telephony.Sms.Intents;
28 import android.telephony.TelephonyManager;
29 import android.text.TextUtils;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.BaseAdapter;
34 import android.widget.ImageView;
35 import android.widget.TextView;
37 import com.android.internal.app.AlertActivity;
38 import com.android.internal.app.AlertController;
39 import com.android.internal.telephony.SmsApplication;
40 import com.android.internal.telephony.SmsApplication.SmsApplicationData;
42 import java.util.ArrayList;
43 import java.util.List;
45 public final class SmsDefaultDialog extends AlertActivity implements
46 DialogInterface.OnClickListener {
47 private SmsApplicationData mNewSmsApplicationData;
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
53 Intent intent = getIntent();
54 String packageName = intent.getStringExtra(Intents.EXTRA_PACKAGE_NAME);
56 setResult(RESULT_CANCELED);
57 if (!buildDialog(packageName)) {
63 public void onClick(DialogInterface dialog, int which) {
66 SmsApplication.setDefaultApplication(mNewSmsApplicationData.mPackageName, this);
73 AppListAdapter adapter = (AppListAdapter) mAlertParams.mAdapter;
74 if (!adapter.isSelected(which)) {
75 String packageName = adapter.getPackageName(which);
76 if (!TextUtils.isEmpty(packageName)) {
77 SmsApplication.setDefaultApplication(packageName, this);
86 private boolean buildDialog(String packageName) {
87 TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
88 if (!tm.isSmsCapable()) {
92 final AlertController.AlertParams p = mAlertParams;
93 p.mTitle = getString(R.string.sms_change_default_dialog_title);
94 mNewSmsApplicationData = SmsApplication.getSmsApplicationData(packageName, this);
95 if (mNewSmsApplicationData != null) {
96 // New default SMS app specified, change to that directly after the confirmation
98 SmsApplicationData oldSmsApplicationData = null;
99 ComponentName oldSmsComponent = SmsApplication.getDefaultSmsApplication(this, true);
100 if (oldSmsComponent != null) {
101 oldSmsApplicationData = SmsApplication.getSmsApplicationData(
102 oldSmsComponent.getPackageName(), this);
103 if (oldSmsApplicationData.mPackageName.equals(
104 mNewSmsApplicationData.mPackageName)) {
109 // Compose dialog; get
110 if (oldSmsApplicationData != null) {
111 p.mMessage = getString(R.string.sms_change_default_dialog_text,
112 mNewSmsApplicationData.getApplicationName(this),
113 oldSmsApplicationData.getApplicationName(this));
115 p.mMessage = getString(R.string.sms_change_default_no_previous_dialog_text,
116 mNewSmsApplicationData.getApplicationName(this));
118 p.mPositiveButtonText = getString(R.string.yes);
119 p.mNegativeButtonText = getString(R.string.no);
120 p.mPositiveButtonListener = this;
121 p.mNegativeButtonListener = this;
123 // No new default SMS app specified, show a list of all SMS apps and let user to pick
124 p.mAdapter = new AppListAdapter();
125 p.mOnClickListener = this;
126 p.mNegativeButtonText = getString(R.string.cancel);
127 p.mNegativeButtonListener = this;
128 if (p.mAdapter.isEmpty()) {
129 // If there is nothing to choose from, don't build the dialog.
139 * The list of SMS apps with label, icon. Current default SMS app is marked as "default".
141 private class AppListAdapter extends BaseAdapter {
143 * SMS app item in the list
146 final String label; // app label
147 final Drawable icon; // app icon
148 final String packgeName; // full app package name
150 public Item(String label, Drawable icon, String packageName) {
153 this.packgeName = packageName;
158 private final List<Item> mItems;
159 // The index of selected
160 private final int mSelectedIndex;
162 public AppListAdapter() {
164 int selected = getSelectedIndex();
165 // Move selected up to the top so it is easy to find
167 Item item = mItems.remove(selected);
171 mSelectedIndex = selected;
175 public int getCount() {
176 return mItems != null ? mItems.size() : 0;
180 public Object getItem(int position) {
181 return mItems != null && position < mItems.size() ? mItems.get(position) : null;
185 public long getItemId(int position) {
190 public View getView(int position, View convertView, ViewGroup parent) {
191 Item item = ((Item) getItem(position));
192 LayoutInflater inflater = getLayoutInflater();
193 View view = inflater.inflate(R.layout.app_preference_item, parent, false);
194 TextView textView = (TextView) view.findViewById(android.R.id.title);
195 textView.setText(item.label);
196 if (position == mSelectedIndex) {
197 view.findViewById(R.id.default_label).setVisibility(View.VISIBLE);
199 view.findViewById(R.id.default_label).setVisibility(View.GONE);
201 ImageView imageView = (ImageView)view.findViewById(android.R.id.icon);
202 imageView.setImageDrawable(item.icon);
207 * Get the selected package name by
209 * @param position the index of the item in the list
210 * @return the package name of selected item
212 public String getPackageName(int position) {
213 Item item = (Item) getItem(position);
215 return item.packgeName;
221 * Check if an item at a position is already selected
223 * @param position the index of the item in the list
224 * @return true if the item at the position is already selected, false otherwise
226 public boolean isSelected(int position) {
227 return position == mSelectedIndex;
230 // Get the list items by looking for SMS apps
231 private List<Item> getItems() {
232 PackageManager pm = getPackageManager();
233 List<Item> items = new ArrayList<>();
234 for (SmsApplication.SmsApplicationData app :
235 SmsApplication.getApplicationCollection(SmsDefaultDialog.this)) {
237 String packageName = app.mPackageName;
238 ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0/*flags*/);
239 if (appInfo != null) {
241 appInfo.loadLabel(pm).toString(),
242 appInfo.loadIcon(pm),
245 } catch (PackageManager.NameNotFoundException e) {
246 // Ignore package can't be found
252 // Get the selected item index by looking for the current default SMS app
253 private int getSelectedIndex() {
254 ComponentName appName = SmsApplication.getDefaultSmsApplication(
255 SmsDefaultDialog.this, true);
256 if (appName != null) {
257 String defaultSmsAppPackageName = appName.getPackageName();
258 if (!TextUtils.isEmpty(defaultSmsAppPackageName)) {
259 for (int i = 0; i < mItems.size(); i++) {
260 if (TextUtils.equals(mItems.get(i).packgeName, defaultSmsAppPackageName)) {