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 static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.graphics.drawable.Drawable;
28 import android.os.Bundle;
29 import android.provider.Telephony.Sms.Intents;
30 import android.telephony.TelephonyManager;
31 import android.text.TextUtils;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.Window;
36 import android.view.WindowManager;
37 import android.widget.BaseAdapter;
38 import android.widget.ImageView;
39 import android.widget.TextView;
41 import com.android.internal.app.AlertActivity;
42 import com.android.internal.app.AlertController;
43 import com.android.internal.telephony.SmsApplication;
44 import com.android.internal.telephony.SmsApplication.SmsApplicationData;
46 import java.util.ArrayList;
47 import java.util.List;
49 public final class SmsDefaultDialog extends AlertActivity implements
50 DialogInterface.OnClickListener {
51 private SmsApplicationData mNewSmsApplicationData;
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
57 Intent intent = getIntent();
58 String packageName = intent.getStringExtra(Intents.EXTRA_PACKAGE_NAME);
60 setResult(RESULT_CANCELED);
61 if (!buildDialog(packageName)) {
67 protected void onStart() {
69 getWindow().addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
70 android.util.EventLog.writeEvent(0x534e4554, "120484087", -1, "");
74 protected void onStop() {
76 final Window window = getWindow();
77 final WindowManager.LayoutParams attrs = window.getAttributes();
78 attrs.privateFlags &= ~PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
79 window.setAttributes(attrs);
83 public void onClick(DialogInterface dialog, int which) {
86 SmsApplication.setDefaultApplication(mNewSmsApplicationData.mPackageName, this);
93 AppListAdapter adapter = (AppListAdapter) mAlertParams.mAdapter;
94 if (!adapter.isSelected(which)) {
95 String packageName = adapter.getPackageName(which);
96 if (!TextUtils.isEmpty(packageName)) {
97 SmsApplication.setDefaultApplication(packageName, this);
106 private boolean buildDialog(String packageName) {
107 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
108 if (!tm.isSmsCapable()) {
112 final AlertController.AlertParams p = mAlertParams;
113 p.mTitle = getString(R.string.sms_change_default_dialog_title);
114 mNewSmsApplicationData = SmsApplication.getSmsApplicationData(packageName, this);
115 if (mNewSmsApplicationData != null) {
116 // New default SMS app specified, change to that directly after the confirmation
118 SmsApplicationData oldSmsApplicationData = null;
119 ComponentName oldSmsComponent = SmsApplication.getDefaultSmsApplication(this, true);
120 if (oldSmsComponent != null) {
121 oldSmsApplicationData = SmsApplication.getSmsApplicationData(
122 oldSmsComponent.getPackageName(), this);
123 if (oldSmsApplicationData.mPackageName.equals(
124 mNewSmsApplicationData.mPackageName)) {
129 // Compose dialog; get
130 if (oldSmsApplicationData != null) {
131 p.mMessage = getString(R.string.sms_change_default_dialog_text,
132 mNewSmsApplicationData.getApplicationName(this),
133 oldSmsApplicationData.getApplicationName(this));
135 p.mMessage = getString(R.string.sms_change_default_no_previous_dialog_text,
136 mNewSmsApplicationData.getApplicationName(this));
138 p.mPositiveButtonText = getString(R.string.yes);
139 p.mNegativeButtonText = getString(R.string.no);
140 p.mPositiveButtonListener = this;
141 p.mNegativeButtonListener = this;
143 // No new default SMS app specified, show a list of all SMS apps and let user to pick
144 p.mAdapter = new AppListAdapter();
145 p.mOnClickListener = this;
146 p.mNegativeButtonText = getString(R.string.cancel);
147 p.mNegativeButtonListener = this;
148 if (p.mAdapter.isEmpty()) {
149 // If there is nothing to choose from, don't build the dialog.
159 * The list of SMS apps with label, icon. Current default SMS app is marked as "default".
161 private class AppListAdapter extends BaseAdapter {
163 * SMS app item in the list
166 final String label; // app label
167 final Drawable icon; // app icon
168 final String packgeName; // full app package name
170 public Item(String label, Drawable icon, String packageName) {
173 this.packgeName = packageName;
178 private final List<Item> mItems;
179 // The index of selected
180 private final int mSelectedIndex;
182 public AppListAdapter() {
184 int selected = getSelectedIndex();
185 // Move selected up to the top so it is easy to find
187 Item item = mItems.remove(selected);
191 mSelectedIndex = selected;
195 public int getCount() {
196 return mItems != null ? mItems.size() : 0;
200 public Object getItem(int position) {
201 return mItems != null && position < mItems.size() ? mItems.get(position) : null;
205 public long getItemId(int position) {
210 public View getView(int position, View convertView, ViewGroup parent) {
211 Item item = ((Item) getItem(position));
212 LayoutInflater inflater = getLayoutInflater();
213 View view = inflater.inflate(R.layout.app_preference_item, parent, false);
214 TextView textView = (TextView) view.findViewById(android.R.id.title);
215 textView.setText(item.label);
216 if (position == mSelectedIndex) {
217 view.findViewById(R.id.default_label).setVisibility(View.VISIBLE);
219 view.findViewById(R.id.default_label).setVisibility(View.GONE);
221 ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
222 imageView.setImageDrawable(item.icon);
227 * Get the selected package name by
229 * @param position the index of the item in the list
230 * @return the package name of selected item
232 public String getPackageName(int position) {
233 Item item = (Item) getItem(position);
235 return item.packgeName;
241 * Check if an item at a position is already selected
243 * @param position the index of the item in the list
244 * @return true if the item at the position is already selected, false otherwise
246 public boolean isSelected(int position) {
247 return position == mSelectedIndex;
250 // Get the list items by looking for SMS apps
251 private List<Item> getItems() {
252 PackageManager pm = getPackageManager();
253 List<Item> items = new ArrayList<>();
254 for (SmsApplication.SmsApplicationData app :
255 SmsApplication.getApplicationCollection(SmsDefaultDialog.this)) {
257 String packageName = app.mPackageName;
258 ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0/*flags*/);
259 if (appInfo != null) {
261 appInfo.loadLabel(pm).toString(),
262 appInfo.loadIcon(pm),
265 } catch (PackageManager.NameNotFoundException e) {
266 // Ignore package can't be found
272 // Get the selected item index by looking for the current default SMS app
273 private int getSelectedIndex() {
274 ComponentName appName = SmsApplication.getDefaultSmsApplication(
275 SmsDefaultDialog.this, true);
276 if (appName != null) {
277 String defaultSmsAppPackageName = appName.getPackageName();
278 if (!TextUtils.isEmpty(defaultSmsAppPackageName)) {
279 for (int i = 0; i < mItems.size(); i++) {
280 if (TextUtils.equals(mItems.get(i).packgeName, defaultSmsAppPackageName)) {