• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.android.deskclock.ringtone;
18 
19 import android.content.AsyncTaskLoader;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.database.MatrixCursor;
23 import android.media.RingtoneManager;
24 import android.net.Uri;
25 
26 import com.android.deskclock.ItemAdapter;
27 import com.android.deskclock.LogUtils;
28 import com.android.deskclock.R;
29 import com.android.deskclock.data.CustomRingtone;
30 import com.android.deskclock.data.DataModel;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 import static android.media.AudioManager.STREAM_ALARM;
36 import static com.android.deskclock.Utils.RINGTONE_SILENT;
37 
38 /**
39  * Assembles the list of ItemHolders that back the RecyclerView used to choose a ringtone.
40  */
41 class RingtoneLoader extends AsyncTaskLoader<List<ItemAdapter.ItemHolder<Uri>>> {
42 
43     private final Uri mDefaultRingtoneUri;
44     private final String mDefaultRingtoneTitle;
45     private List<CustomRingtone> mCustomRingtones;
46 
RingtoneLoader(Context context, Uri defaultRingtoneUri, String defaultRingtoneTitle)47     RingtoneLoader(Context context, Uri defaultRingtoneUri, String defaultRingtoneTitle) {
48         super(context);
49         mDefaultRingtoneUri = defaultRingtoneUri;
50         mDefaultRingtoneTitle = defaultRingtoneTitle;
51     }
52 
53     @Override
onStartLoading()54     protected void onStartLoading() {
55         super.onStartLoading();
56 
57         mCustomRingtones = DataModel.getDataModel().getCustomRingtones();
58         forceLoad();
59     }
60 
61     @Override
loadInBackground()62     public List<ItemAdapter.ItemHolder<Uri>> loadInBackground() {
63         // Prime the ringtone title cache for later access.
64         DataModel.getDataModel().loadRingtoneTitles();
65         DataModel.getDataModel().loadRingtonePermissions();
66 
67         // Fetch the standard system ringtones.
68         final RingtoneManager ringtoneManager = new RingtoneManager(getContext());
69         ringtoneManager.setType(STREAM_ALARM);
70 
71         Cursor systemRingtoneCursor;
72         try {
73             systemRingtoneCursor = ringtoneManager.getCursor();
74         } catch (Exception e) {
75             LogUtils.e("Could not get system ringtone cursor");
76             systemRingtoneCursor = new MatrixCursor(new String[] {});
77         }
78         final int systemRingtoneCount = systemRingtoneCursor.getCount();
79         // item count = # system ringtones + # custom ringtones + 2 headers + Add new music item
80         final int itemCount = systemRingtoneCount + mCustomRingtones.size() + 3;
81 
82         final List<ItemAdapter.ItemHolder<Uri>> itemHolders = new ArrayList<>(itemCount);
83 
84         // Add the item holder for the Music heading.
85         itemHolders.add(new HeaderHolder(R.string.your_sounds));
86 
87         // Add an item holder for each custom ringtone and also cache a pretty name.
88         for (CustomRingtone ringtone : mCustomRingtones) {
89             itemHolders.add(new CustomRingtoneHolder(ringtone));
90         }
91 
92         // Add an item holder for the "Add new" music ringtone.
93         itemHolders.add(new AddCustomRingtoneHolder());
94 
95         // Add an item holder for the Ringtones heading.
96         itemHolders.add(new HeaderHolder(R.string.device_sounds));
97 
98         // Add an item holder for the silent ringtone.
99         itemHolders.add(new SystemRingtoneHolder(RINGTONE_SILENT, null));
100 
101         // Add an item holder for the system default alarm sound.
102         itemHolders.add(new SystemRingtoneHolder(mDefaultRingtoneUri, mDefaultRingtoneTitle));
103 
104         // Add an item holder for each system ringtone.
105         for (int i = 0; i < systemRingtoneCount; i++) {
106             final Uri ringtoneUri = ringtoneManager.getRingtoneUri(i);
107             itemHolders.add(new SystemRingtoneHolder(ringtoneUri, null));
108         }
109 
110         return itemHolders;
111     }
112 
113     @Override
onReset()114     protected void onReset() {
115         super.onReset();
116         mCustomRingtones = null;
117     }
118 }