| Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
|  | 18 | package com.android.phone; | 
|  | 19 |  | 
|  | 20 | import android.app.Activity; | 
|  | 21 | import android.app.ActivityManager; | 
|  | 22 | import android.app.AlertDialog; | 
|  | 23 | import android.content.ComponentName; | 
|  | 24 | import android.content.Context; | 
|  | 25 | import android.content.DialogInterface; | 
|  | 26 | import android.content.Intent; | 
|  | 27 | import android.content.SharedPreferences; | 
|  | 28 | import android.content.pm.ApplicationInfo; | 
|  | 29 | import android.content.pm.PackageManager; | 
| Christine Chen | 32f4a8d | 2013-09-18 21:11:17 -0700 | [diff] [blame] | 30 | import android.content.res.Resources; | 
| Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; | 
|  | 32 | import android.os.Bundle; | 
|  | 33 | import android.util.Log; | 
|  | 34 | import android.view.LayoutInflater; | 
|  | 35 | import android.view.View; | 
|  | 36 | import android.view.ViewGroup; | 
|  | 37 | import android.widget.BaseAdapter; | 
|  | 38 | import android.widget.CheckBox; | 
|  | 39 | import android.widget.CompoundButton; | 
|  | 40 | import android.widget.ImageView; | 
|  | 41 | import android.widget.TextView; | 
| Christine Chen | 32f4a8d | 2013-09-18 21:11:17 -0700 | [diff] [blame] | 42 | import android.widget.Toast; | 
| Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | import java.util.ArrayList; | 
|  | 45 | import java.util.List; | 
|  | 46 |  | 
|  | 47 | public class TextMessagePackageChooser extends Activity { | 
|  | 48 | private static final String TAG = TextMessagePackageChooser.class.getSimpleName(); | 
|  | 49 |  | 
|  | 50 | /** SharedPreferences file name for our persistent settings. */ | 
|  | 51 | private static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs"; | 
|  | 52 |  | 
|  | 53 | private int mIconSize = -1; | 
|  | 54 |  | 
|  | 55 | @Override | 
|  | 56 | public void onCreate(Bundle savedInstanceState) { | 
|  | 57 | super.onCreate(savedInstanceState); | 
|  | 58 | final ArrayList<ComponentName> components = getIntent().getParcelableArrayListExtra( | 
|  | 59 | RejectWithTextMessageManager.TAG_ALL_SMS_SERVICES); | 
|  | 60 | BaseAdapter adapter = new PackageSelectionAdapter(this, components); | 
|  | 61 |  | 
|  | 62 | PackageClickListener clickListener = new PackageClickListener(components); | 
|  | 63 |  | 
|  | 64 | final CharSequence title = getResources().getText( | 
|  | 65 | com.android.internal.R.string.whichApplication); | 
|  | 66 | LayoutInflater inflater = | 
|  | 67 | (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); | 
|  | 68 |  | 
|  | 69 | final View view = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, | 
|  | 70 | null); | 
|  | 71 | final CheckBox alwaysUse = (CheckBox) view.findViewById( | 
|  | 72 | com.android.internal.R.id.alwaysUse); | 
|  | 73 | alwaysUse.setText(com.android.internal.R.string.alwaysUse); | 
|  | 74 | alwaysUse.setOnCheckedChangeListener(clickListener); | 
|  | 75 |  | 
|  | 76 | AlertDialog.Builder builder = new AlertDialog.Builder(this) | 
|  | 77 | .setTitle(title) | 
|  | 78 | .setCancelable(true) | 
|  | 79 | .setOnCancelListener(new RespondViaSmsCancelListener()) | 
|  | 80 | .setAdapter(adapter, clickListener) | 
|  | 81 | .setView(view); | 
|  | 82 |  | 
|  | 83 | builder.create().show(); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | private class PackageSelectionAdapter extends BaseAdapter { | 
|  | 87 | private final LayoutInflater mInflater; | 
|  | 88 | private final List<ComponentName> mComponents; | 
|  | 89 |  | 
|  | 90 | public PackageSelectionAdapter(Context context, List<ComponentName> components) { | 
|  | 91 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | 
|  | 92 | mComponents = components; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | @Override | 
|  | 96 | public int getCount() { | 
|  | 97 | return mComponents.size(); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | @Override | 
|  | 101 | public Object getItem(int position) { | 
|  | 102 | return mComponents.get(position); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | @Override | 
|  | 106 | public long getItemId(int position) { | 
|  | 107 | return position; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | @Override | 
|  | 111 | public View getView(int position, View convertView, ViewGroup parent) { | 
|  | 112 | if (convertView == null) { | 
|  | 113 | convertView = mInflater.inflate( | 
|  | 114 | com.android.internal.R.layout.activity_chooser_view_list_item, parent, | 
|  | 115 | false); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | final ComponentName component = mComponents.get(position); | 
|  | 119 | final String packageName = component.getPackageName(); | 
|  | 120 | final PackageManager packageManager = getPackageManager(); | 
|  | 121 |  | 
|  | 122 | // Set the application label | 
|  | 123 | final TextView text = (TextView) convertView.findViewById( | 
|  | 124 | com.android.internal.R.id.title); | 
|  | 125 |  | 
|  | 126 | text.setText(""); | 
|  | 127 | try { | 
|  | 128 | final ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, 0); | 
|  | 129 | final CharSequence label = packageManager.getApplicationLabel(appInfo); | 
|  | 130 | if (label != null) { | 
|  | 131 | text.setText(label); | 
|  | 132 | } | 
|  | 133 | } catch (PackageManager.NameNotFoundException e) { | 
|  | 134 | Log.w(TAG, "Failed to load app label because package was not found."); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | // Set the application icon | 
|  | 138 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); | 
|  | 139 | Drawable drawable = null; | 
|  | 140 | try { | 
|  | 141 | drawable = getPackageManager().getApplicationIcon(packageName); | 
|  | 142 | } catch (PackageManager.NameNotFoundException e) { | 
|  | 143 | Log.w(TAG, "Failed to load icon because it wasn't found."); | 
|  | 144 | } | 
|  | 145 | if (drawable == null) { | 
|  | 146 | drawable = getPackageManager().getDefaultActivityIcon(); | 
|  | 147 | } | 
|  | 148 | icon.setImageDrawable(drawable); | 
|  | 149 | ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) icon.getLayoutParams(); | 
|  | 150 | lp.width = lp.height = getIconSize(); | 
|  | 151 |  | 
|  | 152 | return convertView; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | private class PackageClickListener implements DialogInterface.OnClickListener, | 
|  | 158 | CompoundButton.OnCheckedChangeListener { | 
|  | 159 | final private List<ComponentName> mComponents; | 
|  | 160 | private boolean mMakeDefault = false; | 
|  | 161 |  | 
|  | 162 | public PackageClickListener(List<ComponentName> components) { | 
|  | 163 | mComponents = components; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | @Override | 
|  | 167 | public void onClick(DialogInterface dialog, int which) { | 
|  | 168 | final ComponentName component = mComponents.get(which); | 
|  | 169 |  | 
|  | 170 | if (mMakeDefault) { | 
|  | 171 | final SharedPreferences prefs = PhoneGlobals.getInstance().getSharedPreferences( | 
|  | 172 | SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); | 
|  | 173 | prefs.edit().putString( | 
|  | 174 | RejectWithTextMessageManager.KEY_INSTANT_TEXT_DEFAULT_COMPONENT, | 
|  | 175 | component.flattenToString()).apply(); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | final Intent messageIntent = (Intent) getIntent().getParcelableExtra( | 
|  | 179 | RejectWithTextMessageManager.TAG_SEND_SMS); | 
|  | 180 | if (messageIntent != null) { | 
|  | 181 | messageIntent.setComponent(component); | 
|  | 182 | PhoneGlobals.getInstance().startService(messageIntent); | 
| Christine Chen | 32f4a8d | 2013-09-18 21:11:17 -0700 | [diff] [blame] | 183 |  | 
|  | 184 | // ...and show a brief confirmation to the user (since | 
|  | 185 | // otherwise it's hard to be sure that anything actually | 
|  | 186 | // happened.) | 
|  | 187 | final Resources res = getResources(); | 
|  | 188 | final String formatString = res.getString( | 
|  | 189 | R.string.respond_via_sms_confirmation_format); | 
|  | 190 | final String phoneNumber = (String) getIntent().getStringExtra( | 
|  | 191 | RejectWithTextMessageManager.TAG_SMS_DESTINATION); | 
|  | 192 | final String confirmationMsg = String.format(formatString, phoneNumber); | 
|  | 193 | Toast.makeText(PhoneGlobals.getInstance(), confirmationMsg, Toast.LENGTH_LONG).show(); | 
| Christine Chen | 0ce0e85 | 2013-08-09 18:26:31 -0700 | [diff] [blame] | 194 | } | 
|  | 195 | finish(); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | @Override | 
|  | 199 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | 
|  | 200 | Log.i(TAG, "mMakeDefault : " + isChecked); | 
|  | 201 | mMakeDefault = isChecked; | 
|  | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | /** | 
|  | 206 | * OnCancelListener for the "Respond via SMS" popup. | 
|  | 207 | */ | 
|  | 208 | public class RespondViaSmsCancelListener implements DialogInterface.OnCancelListener { | 
|  | 209 | public RespondViaSmsCancelListener() { | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | /** | 
|  | 213 | * Handles the user canceling the popup, either by touching | 
|  | 214 | * outside the popup or by pressing Back. | 
|  | 215 | */ | 
|  | 216 | @Override | 
|  | 217 | public void onCancel(DialogInterface dialog) { | 
|  | 218 | finish(); | 
|  | 219 | } | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | private int getIconSize() { | 
|  | 223 | if (mIconSize < 0) { | 
|  | 224 | final ActivityManager am = | 
|  | 225 | (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); | 
|  | 226 | mIconSize = am.getLauncherLargeIconSize(); | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | return mIconSize; | 
|  | 230 | } | 
|  | 231 | } |