summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/SpeedDialListActivity.java
blob: 7ab8deecf5d37c8ad943b7fc9f2dfaa287154191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (C) 2013-2014, The Linux Foundation. All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above
          copyright notice, this list of conditions and the following
          disclaimer in the documentation and/or other materials provided
          with the distribution.
        * Neither the name of The Linux Foundation, Inc. nor the names of its
          contributors may be used to endorse or promote products derived
          from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.dialer;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.Settings;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.view.Gravity;

import com.android.contacts.common.ContactPhotoManager;
import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
import com.android.internal.telephony.PhoneConstants;
import com.google.common.base.FinalizablePhantomReference;

import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
import java.util.List;

public class SpeedDialListActivity extends ListActivity implements
        AdapterView.OnItemClickListener, PopupMenu.OnMenuItemClickListener {
    private static final String TAG = "SpeedDial";
    private static final String ACTION_ADD_VOICEMAIL =
            "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
    public static final String EXTRA_INITIAL_PICK_NUMBER = "initialPickNumber";

    private static final String[] LOOKUP_PROJECTION = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.Contacts.PHOTO_ID,
        ContactsContract.PhoneLookup.NUMBER,
        ContactsContract.PhoneLookup.NORMALIZED_NUMBER
    };

    private static final String[] PICK_PROJECTION = new String[] {
        ContactsContract.Data.CONTACT_ID,
        ContactsContract.Data.DISPLAY_NAME,
        ContactsContract.Data.PHOTO_ID,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER
    };
    private static final int COLUMN_ID = 0;
    private static final int COLUMN_NAME = 1;
    private static final int COLUMN_PHOTO = 2;
    private static final int COLUMN_NUMBER = 3;
    private static final int COLUMN_NORMALIZED = 4;
    private static final int MENU_REPLACE = 1001;
    private static final int MENU_DELETE = 1002;

    private static class Record {
        long contactId;
        String name;
        String number;
        String normalizedNumber;
        long photoId;
        public Record(String number) {
            this.number = number;
            this.contactId = -1;
        }
    }

    private SparseArray<Record> mRecords;

    private int mPickNumber;
    private int mInitialPickNumber;
    private SpeedDialAdapter mAdapter;
    private AlertDialog mAddSpeedDialDialog;
    private EditText mEditNumber;
    private Button mCompleteButton;

    private static final int PICK_CONTACT_RESULT = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mInitialPickNumber = getIntent().getIntExtra(EXTRA_INITIAL_PICK_NUMBER, -1);
        mRecords = new SparseArray<Record>();

        //the first item is the "1.voice mail", it never changes
        mRecords.put(1, new Record(getString(R.string.voicemail)));

        ListView listview = getListView();
        listview.setOnItemClickListener(this);

        // compensate for action bar overlay specified in theme
        int actionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
        listview.setPaddingRelative(0, actionBarHeight, 0, 0);

        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

        mAdapter = new SpeedDialAdapter();
        setListAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // get number from shared preferences
        for (int i = 2; i <= 9; i++) {
            String phoneNumber = SpeedDialUtils.getNumber(this, i);
            Record record = null;
            if (phoneNumber != null) {
                Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                        Uri.encode(phoneNumber));
                record = getRecordFromQuery(uri, LOOKUP_PROJECTION);
                if (record == null) {
                    record = new Record(phoneNumber);
                }
            }
            mRecords.put(i, record);
        }

        mAdapter.notifyDataSetChanged();

        if (mInitialPickNumber >= 2 && mInitialPickNumber <= 9) {
            pickContact(mInitialPickNumber);
            // we only want to trigger the picker once
            mInitialPickNumber = -1;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private Record getRecordFromQuery(Uri uri, String[] projection) {
        Record record = null;
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                record = new Record(cursor.getString(COLUMN_NUMBER));
                record.contactId = cursor.getLong(COLUMN_ID);
                record.photoId = cursor.getLong(COLUMN_PHOTO);
                record.name = cursor.getString(COLUMN_NAME);
                record.normalizedNumber = cursor.getString(COLUMN_NORMALIZED);
                if (record.normalizedNumber == null) {
                    record.normalizedNumber = record.number;
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return record;
    }

    private void showAddSpeedDialDialog(final int number) {
        mPickNumber = number;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.set_speed_dial);
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.add_speed_dial_dialog, null);
        builder.setView(contentView);
        ImageButton pickContacts = (ImageButton) contentView
                .findViewById(R.id.select_contact);
        pickContacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickContact(number);
                dismissDialog();
            }
        });
        mEditNumber = (EditText) contentView.findViewById(R.id.edit_container);
        if (null != mRecords.get(number)) {
            mEditNumber.setText(SpeedDialUtils.getNumber(this, number));
        }
        Button cancelButton = (Button) contentView
                .findViewById(R.id.btn_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog();
            }
        });
        mCompleteButton = (Button) contentView.findViewById(R.id.btn_complete);
        mCompleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditNumber.getText().toString().isEmpty()) {
                    dismissDialog();
                    return;
                }
                saveSpeedDial();
                dismissDialog();
            }
        });
        mAddSpeedDialDialog = builder.create();
        mAddSpeedDialDialog.show();
    }

    private void saveSpeedDial() {
        String number = mEditNumber.getText().toString();
        Record record = null;
        if (number != null) {
            Uri uri = Uri.withAppendedPath(
                    ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(number));
            record = getRecordFromQuery(uri, LOOKUP_PROJECTION);
            if (record == null) {
                record = new Record(number);
                record.normalizedNumber = number;
            }
        }
        if (record != null) {
            SpeedDialUtils.saveNumber(this, mPickNumber,
                    record.normalizedNumber);
            mRecords.put(mPickNumber, record);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void dismissDialog() {
        if (null != mAddSpeedDialDialog && mAddSpeedDialDialog.isShowing()) {
            mAddSpeedDialDialog.dismiss();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            Intent intent = new Intent(ACTION_ADD_VOICEMAIL);
            if (TelephonyManager.getDefault().getPhoneCount() > 1) {
                int sub = SubscriptionManager.getDefaultVoiceSubId();
                intent.setClassName("com.android.phone",
                        "com.android.phone.MSimCallFeaturesSubSetting");
                intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, sub);
            } else {
                intent.setClassName("com.android.phone",
                        "com.android.phone.CallFeaturesSetting");
            }
            try {
                startActivity(intent);
            } catch(ActivityNotFoundException e) {
                Log.w(TAG, "Could not find voice mail setup activity");
            }
        } else {
            int number = position + 1;
            final Record record = mRecords.get(number);
            if (record == null) {
                showAddSpeedDialDialog(number);
            } else {
                PopupMenu pm = new PopupMenu(this, view, Gravity.START);
                pm.getMenu().add(number, MENU_REPLACE, 0, R.string.speed_dial_replace);
                pm.getMenu().add(number, MENU_DELETE, 0, R.string.speed_dial_delete);
                pm.setOnMenuItemClickListener(this);
                pm.show();
            }
        }
    }

    private boolean isMultiAccountAvailable() {
        TelecomManager telecomManager = getTelecomManager(this);
        return (telecomManager.getUserSelectedOutgoingPhoneAccount() == null)
                && (telecomManager.getAllPhoneAccountsCount() > 1);
    }

    private void showSelectAccountDialog(Context context) {
        TelecomManager telecomManager = getTelecomManager(context);
        List<PhoneAccountHandle> accountsList = telecomManager
                .getCallCapablePhoneAccounts();
        final PhoneAccountHandle[] accounts = accountsList
                .toArray(new PhoneAccountHandle[accountsList.size()]);
        CharSequence[] accountEntries = new CharSequence[accounts.length];
        for (int i = 0; i < accounts.length; i++) {
            CharSequence label = telecomManager.getPhoneAccount(accounts[i])
                    .getLabel();
            accountEntries[i] = (label == null) ? null : label.toString();
        }
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(R.string.select_account_dialog_title)
                .setItems(accountEntries, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(ACTION_ADD_VOICEMAIL);
                        int sub = Integer.parseInt(accounts[which].getId());
                        intent.setClassName("com.android.phone",
                                "com.android.phone.MSimCallFeaturesSubSetting");
                        intent.putExtra(SUBSCRIPTION_KEY, sub);
                        try {
                            startActivity(intent);
                        } catch (ActivityNotFoundException e) {
                            Log.w(TAG, "can not find activity deal with voice mail");
                        }
                    }
                })
                .create();
        dialog.show();
    }

    private TelecomManager getTelecomManager(Context context) {
        return TelecomManager.from(context);
    }

    /*
     * goto contacts, used to set or replace speed number
     */
    private void pickContact(int number) {
        mPickNumber = number;
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(intent, PICK_CONTACT_RESULT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != PICK_CONTACT_RESULT) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }

        if (resultCode == RESULT_OK) {
            Record record = getRecordFromQuery(data.getData(), PICK_PROJECTION);
            if (record != null) {
                SpeedDialUtils.saveNumber(this, mPickNumber, record.normalizedNumber);
                mRecords.put(mPickNumber, record);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int number = item.getGroupId();

        switch (item.getItemId()) {
            case MENU_REPLACE:
                showAddSpeedDialDialog(number);
                return true;
            case MENU_DELETE:
                mRecords.put(number, null);
                SpeedDialUtils.saveNumber(this, number, null);
                mAdapter.notifyDataSetChanged();
                return true;
        }
        return false;
    }

    private class SpeedDialAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private ContactPhotoManager mPhotoManager;

        public SpeedDialAdapter() {
            mInflater = LayoutInflater.from(SpeedDialListActivity.this);
            mPhotoManager = (ContactPhotoManager) getApplicationContext().getSystemService(
                    ContactPhotoManager.CONTACT_PHOTO_SERVICE);
        }

        @Override
        public int getCount() {
            return mRecords.size();
        }

        @Override
        public long getItemId(int position) {
            return position + 1;
        }

        @Override
        public Object getItem(int position) {
            return mRecords.get(position + 1);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.speed_dial_item, parent, false);
            }

            TextView index = (TextView) convertView.findViewById(R.id.index);
            TextView name = (TextView) convertView.findViewById(R.id.name);
            TextView number = (TextView) convertView.findViewById(R.id.number);
            QuickContactBadge photo = (QuickContactBadge) convertView.findViewById(R.id.photo);
            Record record = mRecords.get(position + 1);

            index.setText(String.valueOf(position + 1));
            if (record != null && record.name != null) {
                name.setText(record.name);
                number.setText(record.number);
                number.setVisibility(View.VISIBLE);
            } else {
                name.setText(record != null ?
                        record.number : getString(R.string.speed_dial_not_set));
                number.setVisibility(View.GONE);
            }

            if (record != null && record.contactId != -1) {
                DefaultImageRequest request = new DefaultImageRequest(record.name,
                        record.normalizedNumber, true /* isCircular */);
                mPhotoManager.removePhoto(photo);
                mPhotoManager.loadThumbnail(photo, record.photoId,
                        false /* darkTheme */, true /* isCircular */, request);
                photo.assignContactUri(ContentUris.withAppendedId(
                        ContactsContract.Contacts.CONTENT_URI, record.contactId));
                photo.setVisibility(View.VISIBLE);
            } else {
                photo.setVisibility(View.GONE);
            }
            photo.setOverlay(null);

            return convertView;
        }
    };
}