RingtoneSelectionController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.controllers;
  21. import android.annotation.SuppressLint;
  22. import android.database.Cursor;
  23. import android.media.MediaPlayer;
  24. import android.media.RingtoneManager;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.os.Handler;
  28. import android.support.annotation.NonNull;
  29. import android.support.v4.widget.SwipeRefreshLayout;
  30. import android.support.v7.widget.RecyclerView;
  31. import android.text.TextUtils;
  32. import android.util.Log;
  33. import android.view.LayoutInflater;
  34. import android.view.MenuItem;
  35. import android.view.View;
  36. import android.view.ViewGroup;
  37. import com.bluelinelabs.logansquare.LoganSquare;
  38. import com.nextcloud.talk.R;
  39. import com.nextcloud.talk.adapters.items.NotificationSoundItem;
  40. import com.nextcloud.talk.application.NextcloudTalkApplication;
  41. import com.nextcloud.talk.controllers.base.BaseController;
  42. import com.nextcloud.talk.models.RingtoneSettings;
  43. import com.nextcloud.talk.utils.bundle.BundleKeys;
  44. import com.nextcloud.talk.utils.preferences.AppPreferences;
  45. import java.io.IOException;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import javax.inject.Inject;
  49. import autodagger.AutoInjector;
  50. import butterknife.BindView;
  51. import eu.davidea.flexibleadapter.FlexibleAdapter;
  52. import eu.davidea.flexibleadapter.SelectableAdapter;
  53. import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
  54. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
  55. @AutoInjector(NextcloudTalkApplication.class)
  56. public class RingtoneSelectionController extends BaseController implements FlexibleAdapter.OnItemClickListener {
  57. private static final String TAG = "RingtoneSelectionController";
  58. @BindView(R.id.recycler_view)
  59. RecyclerView recyclerView;
  60. @BindView(R.id.swipe_refresh_layout)
  61. SwipeRefreshLayout swipeRefreshLayout;
  62. @Inject
  63. AppPreferences appPreferences;
  64. private FlexibleAdapter adapter;
  65. private List<AbstractFlexibleItem> abstractFlexibleItemList = new ArrayList<>();
  66. private boolean callNotificationSounds = false;
  67. private MediaPlayer mediaPlayer;
  68. private Handler cancelMediaPlayerHandler;
  69. private Cursor cursor;
  70. public RingtoneSelectionController(Bundle args) {
  71. super(args);
  72. setHasOptionsMenu(true);
  73. this.callNotificationSounds = args.getBoolean(BundleKeys.KEY_ARE_CALL_SOUNDS, false);
  74. }
  75. @Override
  76. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  77. return inflater.inflate(R.layout.controller_generic_rv, container, false);
  78. }
  79. @Override
  80. protected void onViewBound(@NonNull View view) {
  81. super.onViewBound(view);
  82. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  83. if (adapter == null) {
  84. adapter = new FlexibleAdapter<>(abstractFlexibleItemList, getActivity(), false);
  85. adapter.setNotifyChangeOfUnfilteredItems(true)
  86. .setMode(SelectableAdapter.Mode.SINGLE);
  87. adapter.addListener(this);
  88. fetchNotificationSounds();
  89. cancelMediaPlayerHandler = new Handler();
  90. }
  91. adapter.addListener(this);
  92. prepareViews();
  93. }
  94. @Override
  95. protected void onAttach(@NonNull View view) {
  96. super.onAttach(view);
  97. if (getActionBar() != null) {
  98. getActionBar().setDisplayHomeAsUpEnabled(true);
  99. }
  100. }
  101. @Override
  102. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  103. switch (item.getItemId()) {
  104. case android.R.id.home:
  105. getRouter().popCurrentController();
  106. return true;
  107. default:
  108. return super.onOptionsItemSelected(item);
  109. }
  110. }
  111. private void prepareViews() {
  112. RecyclerView.LayoutManager layoutManager = new SmoothScrollLinearLayoutManager(getActivity());
  113. recyclerView.setLayoutManager(layoutManager);
  114. recyclerView.setHasFixedSize(true);
  115. recyclerView.setAdapter(adapter);
  116. swipeRefreshLayout.setEnabled(false);
  117. }
  118. @SuppressLint("LongLogTag")
  119. private void fetchNotificationSounds() {
  120. abstractFlexibleItemList = new ArrayList<>();
  121. abstractFlexibleItemList.add(new NotificationSoundItem(getResources().getString(R.string.nc_settings_no_ringtone),
  122. null));
  123. String ringtoneString;
  124. if (callNotificationSounds) {
  125. ringtoneString = "android.resource://" + getApplicationContext().getPackageName() +
  126. "/raw/librem_by_feandesign_call";
  127. } else {
  128. ringtoneString = "android.resource://" + getApplicationContext().getPackageName() +
  129. "/raw/librem_by_feandesign_message";
  130. }
  131. abstractFlexibleItemList.add(new NotificationSoundItem(getResources()
  132. .getString(R.string.nc_settings_default_ringtone), ringtoneString));
  133. boolean foundDefault = false;
  134. String preferencesString = null;
  135. if ((callNotificationSounds && TextUtils.isEmpty((preferencesString = appPreferences.getCallRingtoneUri())))
  136. || (!callNotificationSounds && TextUtils.isEmpty((preferencesString = appPreferences
  137. .getMessageRingtoneUri())))) {
  138. ((NotificationSoundItem) abstractFlexibleItemList.get(1)).setSelected(true);
  139. foundDefault = true;
  140. }
  141. if (getActivity() != null) {
  142. RingtoneManager manager = new RingtoneManager(getActivity());
  143. if (callNotificationSounds) {
  144. manager.setType(RingtoneManager.TYPE_RINGTONE);
  145. } else {
  146. manager.setType(RingtoneManager.TYPE_NOTIFICATION);
  147. }
  148. cursor = manager.getCursor();
  149. NotificationSoundItem notificationSoundItem;
  150. while (cursor.moveToNext()) {
  151. String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
  152. String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
  153. String completeNotificationUri = notificationUri + "/" + cursor.getString(RingtoneManager
  154. .ID_COLUMN_INDEX);
  155. notificationSoundItem = new NotificationSoundItem(notificationTitle, completeNotificationUri);
  156. abstractFlexibleItemList.add(notificationSoundItem);
  157. if (!TextUtils.isEmpty(preferencesString) && !foundDefault) {
  158. try {
  159. RingtoneSettings ringtoneSettings = LoganSquare.parse(preferencesString, RingtoneSettings.class);
  160. if (ringtoneSettings.getRingtoneUri() == null) {
  161. ((NotificationSoundItem) abstractFlexibleItemList.get(0)).setSelected(true);
  162. foundDefault = true;
  163. } else if (completeNotificationUri.equals(ringtoneSettings.getRingtoneUri().toString())) {
  164. notificationSoundItem.setSelected(true);
  165. foundDefault = true;
  166. } else if (ringtoneSettings.getRingtoneUri().toString().equals(ringtoneString)) {
  167. ((NotificationSoundItem) abstractFlexibleItemList.get(1)).setSelected(true);
  168. foundDefault = true;
  169. }
  170. } catch (IOException e) {
  171. Log.e(TAG, "Failed to parse ringtone settings");
  172. }
  173. }
  174. }
  175. }
  176. adapter.updateDataSet(abstractFlexibleItemList, true);
  177. }
  178. @Override
  179. protected String getTitle() {
  180. return getResources().getString(R.string.nc_settings_notification_sounds);
  181. }
  182. @SuppressLint("LongLogTag")
  183. @Override
  184. public boolean onItemClick(View view, int position) {
  185. NotificationSoundItem notificationSoundItem = (NotificationSoundItem) adapter.getItem(position);
  186. Uri ringtoneUri = null;
  187. Runnable runnable = () -> endMediaPlayer();
  188. if (!TextUtils.isEmpty(notificationSoundItem.getNotificationSoundUri())) {
  189. ringtoneUri = Uri.parse(notificationSoundItem.getNotificationSoundUri());
  190. endMediaPlayer();
  191. mediaPlayer = MediaPlayer.create(getActivity(), ringtoneUri);
  192. cancelMediaPlayerHandler = new Handler();
  193. cancelMediaPlayerHandler.postDelayed(runnable, mediaPlayer.getDuration() + 25);
  194. mediaPlayer.start();
  195. }
  196. if (adapter.getSelectedPositions().size() == 0 || adapter.getSelectedPositions().get(0) != position) {
  197. RingtoneSettings ringtoneSettings = new RingtoneSettings();
  198. ringtoneSettings.setRingtoneName(notificationSoundItem.getNotificationSoundName());
  199. ringtoneSettings.setRingtoneUri(ringtoneUri);
  200. if (callNotificationSounds) {
  201. try {
  202. appPreferences.setCallRingtoneUri(LoganSquare.serialize(ringtoneSettings));
  203. toggleSelection(position);
  204. } catch (IOException e) {
  205. Log.e(TAG, "Failed to store selected ringtone for calls");
  206. }
  207. } else {
  208. try {
  209. appPreferences.setMessageRingtoneUri(LoganSquare.serialize(ringtoneSettings));
  210. toggleSelection(position);
  211. } catch (IOException e) {
  212. Log.e(TAG, "Failed to store selected ringtone for calls");
  213. }
  214. }
  215. }
  216. return true;
  217. }
  218. private void toggleSelection(int position) {
  219. adapter.toggleSelection(position);
  220. ((NotificationSoundItem) adapter.getItem(position)).flipItemSelection();
  221. NotificationSoundItem notificationSoundItem;
  222. for (int i = 0; i < adapter.getItemCount(); i++) {
  223. if (i != position) {
  224. notificationSoundItem = (NotificationSoundItem) adapter.getItem(i);
  225. notificationSoundItem.flipToFront();
  226. }
  227. }
  228. }
  229. private void endMediaPlayer() {
  230. if (cancelMediaPlayerHandler != null) {
  231. cancelMediaPlayerHandler.removeCallbacksAndMessages(null);
  232. }
  233. if (mediaPlayer != null) {
  234. if (mediaPlayer.isPlaying()) {
  235. mediaPlayer.stop();
  236. }
  237. mediaPlayer.release();
  238. mediaPlayer = null;
  239. }
  240. }
  241. @Override
  242. public void onDestroy() {
  243. endMediaPlayer();
  244. if (cursor != null) {
  245. cursor.close();
  246. }
  247. super.onDestroy();
  248. }
  249. }