-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingtonePickerActivity.java
833 lines (716 loc) · 31.5 KB
/
RingtonePickerActivity.java
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.providers.media;
import android.content.ContentProvider;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.database.StaleDataException;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import java.io.IOException;
import java.util.Objects;
import java.util.regex.Pattern;
import com.unisoc.providers.media.RingtonePickerActivityAssist;
/**
* The {@link RingtonePickerActivity} allows the user to choose one from all of the
* available ringtones. The chosen ringtone's URI will be persisted as a string.
*
* @see RingtoneManager#ACTION_RINGTONE_PICKER
*/
public final class RingtonePickerActivity extends AlertActivity implements
AdapterView.OnItemSelectedListener, Runnable, DialogInterface.OnClickListener,
AlertController.AlertParams.OnPrepareListViewListener {
private static final int POS_UNKNOWN = -1;
private static final String TAG = "RingtonePickerActivity";
private static final int DELAY_MS_SELECTION_PLAYED = 300;
private static final String COLUMN_LABEL = MediaStore.Audio.Media.TITLE;
private static final String SAVE_CLICKED_POS = "clicked_pos";
private static final String SOUND_NAME_RES_PREFIX = "sound_name_";
private static final int ADD_FILE_REQUEST_CODE = 300;
// unisoc bug 1147836: added for ensuring sounds respect the rules of audio focus
private AudioManager mAudioManager;
private RingtoneManager mRingtoneManager;
private int mType;
private Cursor mCursor;
private Handler mHandler;
private BadgedRingtoneAdapter mAdapter;
/** The position in the list of the 'Silent' item. */
private int mSilentPos = POS_UNKNOWN;
/** The position in the list of the 'Default' item. */
private int mDefaultRingtonePos = POS_UNKNOWN;
/** The position in the list of the ringtone to sample. */
private int mSampleRingtonePos = POS_UNKNOWN;
/** Whether this list has the 'Silent' item. */
private boolean mHasSilentItem;
/** The Uri to place a checkmark next to. */
private Uri mExistingUri;
/** The number of static items in the list. */
private int mStaticItemCount;
/** Whether this list has the 'Default' item. */
private boolean mHasDefaultItem;
/** The Uri to play when the 'Default' item is clicked. */
private Uri mUriForDefaultItem;
/** Id of the user to which the ringtone picker should list the ringtones */
private int mPickerUserId;
/** Context of the user specified by mPickerUserId */
private Context mTargetContext;
/**
* A Ringtone for the default ringtone. In most cases, the RingtoneManager
* will stop the previous ringtone. However, the RingtoneManager doesn't
* manage the default ringtone for us, so we should stop this one manually.
*/
private Ringtone mDefaultRingtone;
/**
* The ringtone that's currently playing, unless the currently playing one is the default
* ringtone.
*/
private Ringtone mCurrentRingtone;
/**
* Stable ID for the ringtone that is currently checked (may be -1 if no ringtone is checked).
*/
private long mCheckedItemId = -1;
private int mAttributesFlags;
private boolean mShowOkCancelButtons;
/**
* Keep the currently playing ringtone around when changing orientation, so that it
* can be stopped later, after the activity is recreated.
*/
private static Ringtone sPlayingRingtone;
private RingtonePickerActivityAssist mAssist;
private DialogInterface.OnClickListener mRingtoneClickListener =
new DialogInterface.OnClickListener() {
/*
* On item clicked
*/
public void onClick(DialogInterface dialog, int which) {
if (which == mCursor.getCount() + mStaticItemCount) {
// The "Add new ringtone" item was clicked. Start a file picker intent to select
// only audio files (MIME type "audio/*")
final Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("audio/*");
chooseFile.putExtra(Intent.EXTRA_MIME_TYPES,
new String[] { "audio/*", "application/ogg" });
startActivityForResult(chooseFile, ADD_FILE_REQUEST_CODE);
return;
}
// Save the position of most recently clicked item
setCheckedItem(which);
// In the buttonless (watch-only) version, preemptively set our result since we won't
// have another chance to do so before the activity closes.
if (!mShowOkCancelButtons) {
setSuccessResultWithRingtone(getCurrentlySelectedRingtoneUri());
}
// Play clip
playRingtone(which, 0);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
Intent intent = getIntent();
mPickerUserId = UserHandle.myUserId();
mTargetContext = this;
// unisoc bug 1147836: added for ensuring sounds respect the rules of audio focus
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAssist = new RingtonePickerActivityAssist(this);
// Get the types of ringtones to show
mType = intent.getIntExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, -1);
initRingtoneManager();
/*
* Get whether to show the 'Default' item, and the URI to play when the
* default is clicked
*/
mHasDefaultItem = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
mUriForDefaultItem = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI);
if (mUriForDefaultItem == null) {
if (mType == RingtoneManager.TYPE_NOTIFICATION) {
mUriForDefaultItem = Settings.System.DEFAULT_NOTIFICATION_URI;
} else if (mType == RingtoneManager.TYPE_ALARM) {
mUriForDefaultItem = Settings.System.DEFAULT_ALARM_ALERT_URI;
} else if (mType == RingtoneManager.TYPE_RINGTONE) {
mUriForDefaultItem = Settings.System.DEFAULT_RINGTONE_URI;
} else {
// or leave it null for silence.
mUriForDefaultItem = Settings.System.DEFAULT_RINGTONE_URI;
}
}
// Get whether to show the 'Silent' item
mHasSilentItem = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
// AudioAttributes flags
mAttributesFlags |= intent.getIntExtra(
RingtoneManager.EXTRA_RINGTONE_AUDIO_ATTRIBUTES_FLAGS,
0 /*defaultValue == no flags*/);
mShowOkCancelButtons = getResources().getBoolean(R.bool.config_showOkCancelButtons);
// The volume keys will control the stream that we are choosing a ringtone for
setVolumeControlStream(mRingtoneManager.inferStreamType());
// Get the URI whose list item should have a checkmark
mExistingUri = intent
.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI);
// Create the list of ringtones and hold on to it so we can update later.
mAdapter = new BadgedRingtoneAdapter(this, mCursor,
/* isManagedProfile = */ UserManager.get(this).isManagedProfile(mPickerUserId));
if (savedInstanceState != null) {
setCheckedItem(savedInstanceState.getInt(SAVE_CLICKED_POS, POS_UNKNOWN));
}
final AlertController.AlertParams p = mAlertParams;
p.mAdapter = mAdapter;
p.mOnClickListener = mRingtoneClickListener;
p.mLabelColumn = COLUMN_LABEL;
p.mIsSingleChoice = true;
p.mOnItemSelectedListener = this;
if (mShowOkCancelButtons) {
p.mPositiveButtonText = getString(com.android.internal.R.string.ok);
p.mPositiveButtonListener = this;
p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
p.mPositiveButtonListener = this;
}
p.mOnPrepareListViewListener = this;
p.mTitle = intent.getCharSequenceExtra(RingtoneManager.EXTRA_RINGTONE_TITLE);
if (p.mTitle == null) {
if (mType == RingtoneManager.TYPE_ALARM) {
p.mTitle = getString(com.android.internal.R.string.ringtone_picker_title_alarm);
} else if (mType == RingtoneManager.TYPE_NOTIFICATION) {
p.mTitle =
getString(com.android.internal.R.string.ringtone_picker_title_notification);
} else {
p.mTitle = getString(com.android.internal.R.string.ringtone_picker_title);
}
}
setupAlert();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SAVE_CLICKED_POS, getCheckedItem());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_FILE_REQUEST_CODE && resultCode == RESULT_OK) {
// Add the custom ringtone in a separate thread
final AsyncTask<Uri, Void, Uri> installTask = new AsyncTask<Uri, Void, Uri>() {
@Override
protected Uri doInBackground(Uri... params) {
try {
if (!mAssist.isAudioIrregular(params[0], mType)) {
return mRingtoneManager.addCustomExternalRingtone(params[0], mType);
}
} catch (IOException | IllegalArgumentException e) {
Log.e(TAG, "Unable to add new ringtone", e);
}
return null;
}
@Override
protected void onPostExecute(Uri ringtoneUri) {
if (ringtoneUri != null) {
requeryForAdapter();
} else {
// Ringtone was not added, display error Toast
if (mAssist.isPresent()) {
Toast.makeText(RingtonePickerActivity.this, R.string.unable_to_add_ringtone_unique_error,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RingtonePickerActivity.this, R.string.unable_to_add_ringtone,
Toast.LENGTH_SHORT).show();
}
}
}
};
installTask.execute(data.getData());
}
// unisoc bug 1126034: added for adding the relevant logic to display which ringtone is
// selected whatever has been done after choosing Add ringtone
setupAlert();
}
// Disabled because context menus aren't Material Design :(
/*
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
int position = ((AdapterContextMenuInfo) menuInfo).position;
Ringtone ringtone = getRingtone(getRingtoneManagerPosition(position));
if (ringtone != null && mRingtoneManager.isCustomRingtone(ringtone.getUri())) {
// It's a custom ringtone so we display the context menu
menu.setHeaderTitle(ringtone.getTitle(this));
menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, R.string.delete_ringtone_text);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST: {
int deletedRingtonePos = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
Uri deletedRingtoneUri = getRingtone(
getRingtoneManagerPosition(deletedRingtonePos)).getUri();
if(mRingtoneManager.deleteExternalRingtone(deletedRingtoneUri)) {
requeryForAdapter();
} else {
Toast.makeText(this, R.string.unable_to_delete_ringtone, Toast.LENGTH_SHORT)
.show();
}
return true;
}
default: {
return false;
}
}
}
*/
@Override
public void onDestroy() {
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
super.onDestroy();
}
public void onPrepareListView(ListView listView) {
// Reset the static item count, as this method can be called multiple times
mStaticItemCount = 0;
if (mHasDefaultItem) {
mDefaultRingtonePos = addDefaultRingtoneItem(listView);
if (getCheckedItem() == POS_UNKNOWN && RingtoneManager.isDefault(mExistingUri)) {
setCheckedItem(mDefaultRingtonePos);
}
}
if (mHasSilentItem) {
mSilentPos = addSilentItem(listView);
// The 'Silent' item should use a null Uri
if (getCheckedItem() == POS_UNKNOWN && mExistingUri == null) {
setCheckedItem(mSilentPos);
}
}
if (getCheckedItem() == POS_UNKNOWN) {
setCheckedItem(getListPosition(mRingtoneManager.getRingtonePosition(mExistingUri)));
}
// In the buttonless (watch-only) version, preemptively set our result since we won't
// have another chance to do so before the activity closes.
if (!mShowOkCancelButtons) {
setSuccessResultWithRingtone(getCurrentlySelectedRingtoneUri());
}
// If external storage is available, add a button to install sounds from storage.
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
addNewSoundItem(listView);
}
// Enable context menu in ringtone items
registerForContextMenu(listView);
}
/**
* Re-query RingtoneManager for the most recent set of installed ringtones. May move the
* selected item position to match the new position of the chosen sound.
*
* This should only need to happen after adding or removing a ringtone.
*/
private void requeryForAdapter() {
Uri previouslySelectedRingtoneUri = getCurrentlySelectedRingtoneUri();
// Refresh and set a new cursor, closing the old one.
initRingtoneManager();
mAdapter.changeCursor(mCursor);
// Update checked item location.
int checkedPosition = POS_UNKNOWN;
for (int i = 0; i < mAdapter.getCount(); i++) {
if (mAdapter.getItemId(i) == mCheckedItemId
&& mRingtoneManager.getRingtoneUri(i).equals(previouslySelectedRingtoneUri)) {
checkedPosition = getListPosition(i);
break;
}
}
// unisoc bug 1124130: added and modified for fixing notification ringtones logic to
// display which ringtone is selected whatever has been done after choosing Add
// ringtone for a certain app
if (mHasDefaultItem && checkedPosition == POS_UNKNOWN && getCheckedItem() == mDefaultRingtonePos) {
checkedPosition = mDefaultRingtonePos;
}
if (mHasSilentItem && checkedPosition == POS_UNKNOWN && getCheckedItem() == mSilentPos) {
checkedPosition = mSilentPos;
}
setCheckedItem(checkedPosition);
setupAlert();
}
/**
* Adds a static item to the top of the list. A static item is one that is not from the
* RingtoneManager.
*
* @param listView The ListView to add to.
* @param textResId The resource ID of the text for the item.
* @return The position of the inserted item.
*/
private int addStaticItem(ListView listView, int textResId) {
TextView textView = (TextView) getLayoutInflater().inflate(
com.android.internal.R.layout.select_dialog_singlechoice_material, listView, false);
textView.setText(textResId);
listView.addHeaderView(textView);
mStaticItemCount++;
return listView.getHeaderViewsCount() - 1;
}
private int addDefaultRingtoneItem(ListView listView) {
if (mType == RingtoneManager.TYPE_NOTIFICATION) {
return addStaticItem(listView, R.string.notification_sound_default);
} else if (mType == RingtoneManager.TYPE_ALARM) {
return addStaticItem(listView, R.string.alarm_sound_default);
}
return addStaticItem(listView, R.string.ringtone_default);
}
private int addSilentItem(ListView listView) {
return addStaticItem(listView, com.android.internal.R.string.ringtone_silent);
}
private void addNewSoundItem(ListView listView) {
View view = getLayoutInflater().inflate(R.layout.add_new_sound_item, listView,
false /* attachToRoot */);
TextView text = (TextView)view.findViewById(R.id.add_new_sound_text);
if (mType == RingtoneManager.TYPE_ALARM) {
text.setText(R.string.add_alarm_text);
} else if (mType == RingtoneManager.TYPE_NOTIFICATION) {
text.setText(R.string.add_notification_text);
} else {
text.setText(R.string.add_ringtone_text);
}
listView.addFooterView(view);
}
private void initRingtoneManager() {
// Reinstantiate the RingtoneManager. Cursor.requery() was deprecated and calling it
// causes unexpected behavior.
mRingtoneManager = new RingtoneManager(mTargetContext, /* includeParentRingtones */ true);
if (mType != -1) {
mRingtoneManager.setType(mType);
}
mCursor = new LocalizedCursor(mRingtoneManager.getCursor(), getResources(), COLUMN_LABEL);
}
private Ringtone getRingtone(int ringtoneManagerPosition) {
if (ringtoneManagerPosition < 0) {
return null;
}
return mRingtoneManager.getRingtone(ringtoneManagerPosition);
}
private int getCheckedItem() {
return mAlertParams.mCheckedItem;
}
private void setCheckedItem(int pos) {
mAlertParams.mCheckedItem = pos;
mCheckedItemId = mAdapter.getItemId(getRingtoneManagerPosition(pos));
}
/*
* On click of Ok/Cancel buttons
*/
public void onClick(DialogInterface dialog, int which) {
boolean positiveResult = which == DialogInterface.BUTTON_POSITIVE;
// Stop playing the previous ringtone
mRingtoneManager.stopPreviousRingtone();
if (positiveResult) {
setSuccessResultWithRingtone(getCurrentlySelectedRingtoneUri());
} else {
setResult(RESULT_CANCELED);
}
finish();
}
/*
* On item selected via keys
*/
public void onItemSelected(AdapterView parent, View view, int position, long id) {
// footer view
if (position >= mCursor.getCount() + mStaticItemCount) {
return;
}
playRingtone(position, DELAY_MS_SELECTION_PLAYED);
// In the buttonless (watch-only) version, preemptively set our result since we won't
// have another chance to do so before the activity closes.
if (!mShowOkCancelButtons) {
setSuccessResultWithRingtone(getCurrentlySelectedRingtoneUri());
}
}
public void onNothingSelected(AdapterView parent) {
}
private void playRingtone(int position, int delayMs) {
mHandler.removeCallbacks(this);
mSampleRingtonePos = position;
mHandler.postDelayed(this, delayMs);
}
/* unisoc bug 1147836: added for ensuring sounds respect the rules of audio focus @{*/
private AudioManager.OnAudioFocusChangeListener mAudioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
Log.d(TAG, "focusChange=" + focusChange);
switch(focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
stopAnyPlayingRingtone();
break;
case AudioManager.AUDIOFOCUS_GAIN:
break;
}
}
};
/* @ }*/
public void run() {
stopAnyPlayingRingtone();
if (mSampleRingtonePos == mSilentPos) {
/* unisoc bug 1173643: added for releasing audio focues when ringtone is none @{*/
if (mAudioManager != null) {
mAudioManager.abandonAudioFocus(mAudioFocusListener);
}
/* @ }*/
return;
}
Ringtone ringtone;
if (mSampleRingtonePos == mDefaultRingtonePos) {
if (mDefaultRingtone == null) {
mDefaultRingtone = RingtoneManager.getRingtone(this, mUriForDefaultItem);
}
/*
* Stream type of mDefaultRingtone is not set explicitly here.
* It should be set in accordance with mRingtoneManager of this Activity.
*/
if (mDefaultRingtone != null) {
mDefaultRingtone.setStreamType(mRingtoneManager.inferStreamType());
}
ringtone = mDefaultRingtone;
mCurrentRingtone = null;
} else {
try {
ringtone = mRingtoneManager.getRingtone(getRingtoneManagerPosition(mSampleRingtonePos));
} catch (StaleDataException | IllegalStateException e) {
Log.d(TAG, "A " + e.getClass().getSimpleName() + " was caught");
return;
}
mCurrentRingtone = ringtone;
}
if (ringtone != null) {
if (mAttributesFlags != 0) {
ringtone.setAudioAttributes(
new AudioAttributes.Builder(ringtone.getAudioAttributes())
.setFlags(mAttributesFlags)
.build());
}
/* unisoc bug 1147836: modified for ensuring sounds respect the rules of audio focus @{*/
int result = 0;
if (mAudioManager != null) {
result = mAudioManager.requestAudioFocus(mAudioFocusListener,
mRingtoneManager.inferStreamType(), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
ringtone.play();
}
/* @ }*/
}
}
@Override
protected void onStop() {
super.onStop();
if (!isChangingConfigurations()) {
stopAnyPlayingRingtone();
} else {
saveAnyPlayingRingtone();
}
/* unisoc bug 1147836: added for ensuring sounds respect the rules of audio focus @{*/
if (mAudioManager != null) {
mAudioManager.abandonAudioFocus(mAudioFocusListener);
}
/* @ }*/
}
@Override
protected void onPause() {
super.onPause();
if (!isChangingConfigurations()) {
stopAnyPlayingRingtone();
}
}
private void setSuccessResultWithRingtone(Uri ringtoneUri) {
setResult(RESULT_OK,
new Intent().putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, ringtoneUri));
}
private Uri getCurrentlySelectedRingtoneUri() {
if (getCheckedItem() == mDefaultRingtonePos) {
// Use the default Uri that they originally gave us.
return mUriForDefaultItem;
} else if (getCheckedItem() == mSilentPos) {
// Use a null Uri for the 'Silent' item.
return null;
} else {
return mRingtoneManager.getRingtoneUri(getRingtoneManagerPosition(getCheckedItem()));
}
}
private void saveAnyPlayingRingtone() {
if (mDefaultRingtone != null && mDefaultRingtone.isPlaying()) {
sPlayingRingtone = mDefaultRingtone;
} else if (mCurrentRingtone != null && mCurrentRingtone.isPlaying()) {
sPlayingRingtone = mCurrentRingtone;
}
}
private void stopAnyPlayingRingtone() {
if (sPlayingRingtone != null && sPlayingRingtone.isPlaying()) {
sPlayingRingtone.stop();
}
sPlayingRingtone = null;
if (mDefaultRingtone != null && mDefaultRingtone.isPlaying()) {
mDefaultRingtone.stop();
}
if (mRingtoneManager != null) {
mRingtoneManager.stopPreviousRingtone();
}
}
private int getRingtoneManagerPosition(int listPos) {
return listPos - mStaticItemCount;
}
private int getListPosition(int ringtoneManagerPos) {
// If the manager position is -1 (for not found), return that
if (ringtoneManagerPos < 0) return ringtoneManagerPos;
return ringtoneManagerPos + mStaticItemCount;
}
private static class LocalizedCursor extends CursorWrapper {
final int mTitleIndex;
final Resources mResources;
String mNamePrefix;
final Pattern mSanitizePattern;
LocalizedCursor(Cursor cursor, Resources resources, String columnLabel) {
super(cursor);
mTitleIndex = mCursor.getColumnIndex(columnLabel);
mResources = resources;
mSanitizePattern = Pattern.compile("[^a-zA-Z0-9]");
if (mTitleIndex == -1) {
Log.e(TAG, "No index for column " + columnLabel);
mNamePrefix = null;
} else {
try {
// Build the prefix for the name of the resource to look up
// format is: "ResourcePackageName::ResourceTypeName/"
// (the type name is expected to be "string" but let's not hardcode it).
// Here we use an existing resource "notification_sound_default" which is
// always expected to be found.
mNamePrefix = String.format("%s:%s/%s",
mResources.getResourcePackageName(R.string.notification_sound_default),
mResources.getResourceTypeName(R.string.notification_sound_default),
SOUND_NAME_RES_PREFIX);
} catch (NotFoundException e) {
mNamePrefix = null;
}
}
}
/**
* Process resource name to generate a valid resource name.
* @param input
* @return a non-null String
*/
private String sanitize(String input) {
if (input == null) {
return "";
}
return mSanitizePattern.matcher(input).replaceAll("_").toLowerCase();
}
@Override
public String getString(int columnIndex) {
final String defaultName = mCursor.getString(columnIndex);
if ((columnIndex != mTitleIndex) || (mNamePrefix == null)) {
return defaultName;
}
TypedValue value = new TypedValue();
try {
// the name currently in the database is used to derive a name to match
// against resource names in this package
mResources.getValue(mNamePrefix + sanitize(defaultName), value, false);
} catch (NotFoundException e) {
// no localized string, use the default string
return defaultName;
}
if ((value != null) && (value.type == TypedValue.TYPE_STRING)) {
Log.d(TAG, String.format("Replacing name %s with %s",
defaultName, value.string.toString()));
return value.string.toString();
} else {
Log.e(TAG, "Invalid value when looking up localized name, using " + defaultName);
return defaultName;
}
}
}
private class BadgedRingtoneAdapter extends CursorAdapter {
private final boolean mIsManagedProfile;
public BadgedRingtoneAdapter(Context context, Cursor cursor, boolean isManagedProfile) {
super(context, cursor);
mIsManagedProfile = isManagedProfile;
}
@Override
public long getItemId(int position) {
if (position < 0) {
return position;
}
return super.getItemId(position);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.radio_with_work_badge, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Set text as the title of the ringtone
((TextView) view.findViewById(R.id.checked_text_view))
.setText(cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX));
boolean isWorkRingtone = false;
if (mIsManagedProfile) {
/*
* Display the work icon if the ringtone belongs to a work profile. We can tell that
* a ringtone belongs to a work profile if the picker user is a managed profile, the
* ringtone Uri is in external storage, and either the uri has no user id or has the
* id of the picker user
*/
Uri currentUri = mRingtoneManager.getRingtoneUri(cursor.getPosition());
int uriUserId = ContentProvider.getUserIdFromUri(currentUri, mPickerUserId);
Uri uriWithoutUserId = ContentProvider.getUriWithoutUserId(currentUri);
if (uriUserId == mPickerUserId && uriWithoutUserId.toString()
.startsWith(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString())) {
isWorkRingtone = true;
}
}
ImageView workIcon = (ImageView) view.findViewById(R.id.work_icon);
if(isWorkRingtone) {
workIcon.setImageDrawable(getPackageManager().getUserBadgeForDensityNoBackground(
UserHandle.of(mPickerUserId), -1 /* density */));
workIcon.setVisibility(View.VISIBLE);
} else {
workIcon.setVisibility(View.GONE);
}
}
}
}