RingtoneSelectionController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. public RingtoneSelectionController(Bundle args) {
  70. super(args);
  71. setHasOptionsMenu(true);
  72. this.callNotificationSounds = args.getBoolean(BundleKeys.KEY_ARE_CALL_SOUNDS, false);
  73. }
  74. @Override
  75. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  76. return inflater.inflate(R.layout.controller_generic_rv, container, false);
  77. }
  78. @Override
  79. protected void onViewBound(@NonNull View view) {
  80. super.onViewBound(view);
  81. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  82. if (adapter == null) {
  83. adapter = new FlexibleAdapter<>(abstractFlexibleItemList, getActivity(), false);
  84. adapter.setNotifyChangeOfUnfilteredItems(true)
  85. .setMode(SelectableAdapter.Mode.SINGLE);
  86. adapter.addListener(this);
  87. fetchNotificationSounds();
  88. cancelMediaPlayerHandler = new Handler();
  89. }
  90. adapter.addListener(this);
  91. prepareViews();
  92. }
  93. @Override
  94. protected void onAttach(@NonNull View view) {
  95. super.onAttach(view);
  96. if (getActionBar() != null) {
  97. getActionBar().setDisplayHomeAsUpEnabled(true);
  98. }
  99. }
  100. @Override
  101. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  102. switch (item.getItemId()) {
  103. case android.R.id.home:
  104. getRouter().popCurrentController();
  105. return true;
  106. default:
  107. return super.onOptionsItemSelected(item);
  108. }
  109. }
  110. private void prepareViews() {
  111. RecyclerView.LayoutManager layoutManager = new SmoothScrollLinearLayoutManager(getActivity());
  112. recyclerView.setLayoutManager(layoutManager);
  113. recyclerView.setHasFixedSize(true);
  114. recyclerView.setAdapter(adapter);
  115. swipeRefreshLayout.setEnabled(false);
  116. }
  117. @SuppressLint("LongLogTag")
  118. private void fetchNotificationSounds() {
  119. abstractFlexibleItemList = new ArrayList<>();
  120. abstractFlexibleItemList.add(new NotificationSoundItem("None", null));
  121. String ringtoneString;
  122. if (callNotificationSounds) {
  123. ringtoneString = "android.resource://" + getApplicationContext().getPackageName() +
  124. "/raw/librem_by_feandesign_call";
  125. } else {
  126. ringtoneString = "android.resource://" + getApplicationContext().getPackageName() +
  127. "/raw/librem_by_feandesign_message";
  128. }
  129. abstractFlexibleItemList.add(new NotificationSoundItem(getResources()
  130. .getString(R.string.nc_settings_default_ringtone), ringtoneString));
  131. boolean foundDefault = false;
  132. String preferencesString = null;
  133. if ((callNotificationSounds && TextUtils.isEmpty((preferencesString = appPreferences.getCallRingtoneUri())))
  134. || (!callNotificationSounds && TextUtils.isEmpty((preferencesString = appPreferences
  135. .getMessageRingtoneUri())))) {
  136. ((NotificationSoundItem) abstractFlexibleItemList.get(1)).setSelected(true);
  137. foundDefault = true;
  138. }
  139. if (getActivity() != null) {
  140. RingtoneManager manager = new RingtoneManager(getActivity());
  141. if (callNotificationSounds) {
  142. manager.setType(RingtoneManager.TYPE_RINGTONE);
  143. } else {
  144. manager.setType(RingtoneManager.TYPE_NOTIFICATION);
  145. }
  146. Cursor cursor = manager.getCursor();
  147. NotificationSoundItem notificationSoundItem;
  148. while (cursor.moveToNext()) {
  149. String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
  150. String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
  151. String completeNotificationUri = notificationUri + "/" + cursor.getString(RingtoneManager
  152. .ID_COLUMN_INDEX);
  153. notificationSoundItem = new NotificationSoundItem(notificationTitle, completeNotificationUri);
  154. abstractFlexibleItemList.add(notificationSoundItem);
  155. if (!TextUtils.isEmpty(preferencesString) && !foundDefault) {
  156. try {
  157. RingtoneSettings ringtoneSettings = LoganSquare.parse(preferencesString, RingtoneSettings.class);
  158. if (ringtoneSettings.getRingtoneUri() == null) {
  159. ((NotificationSoundItem) abstractFlexibleItemList.get(0)).setSelected(true);
  160. foundDefault = true;
  161. } else if (completeNotificationUri.equals(ringtoneSettings.getRingtoneUri().toString())) {
  162. notificationSoundItem.setSelected(true);
  163. foundDefault = true;
  164. } else if (ringtoneSettings.getRingtoneUri().toString().equals(ringtoneString)) {
  165. ((NotificationSoundItem) abstractFlexibleItemList.get(1)).setSelected(true);
  166. foundDefault = true;
  167. }
  168. } catch (IOException e) {
  169. Log.e(TAG, "Failed to parse ringtone settings");
  170. }
  171. }
  172. }
  173. cursor.close();
  174. }
  175. adapter.updateDataSet(abstractFlexibleItemList, true);
  176. }
  177. @Override
  178. protected String getTitle() {
  179. return getResources().getString(R.string.nc_settings_notification_sounds);
  180. }
  181. @SuppressLint("LongLogTag")
  182. @Override
  183. public boolean onItemClick(View view, int position) {
  184. NotificationSoundItem notificationSoundItem = (NotificationSoundItem) adapter.getItem(position);
  185. Uri ringtoneUri = null;
  186. Runnable runnable = () -> endMediaPlayer();
  187. if (!TextUtils.isEmpty(notificationSoundItem.getNotificationSoundUri())) {
  188. ringtoneUri = Uri.parse(notificationSoundItem.getNotificationSoundUri());
  189. endMediaPlayer();
  190. mediaPlayer = MediaPlayer.create(getActivity(), ringtoneUri);
  191. cancelMediaPlayerHandler = new Handler();
  192. cancelMediaPlayerHandler.postDelayed(runnable, mediaPlayer.getDuration() + 25);
  193. mediaPlayer.start();
  194. }
  195. if (adapter.getSelectedPositions().get(0) != position) {
  196. RingtoneSettings ringtoneSettings = new RingtoneSettings();
  197. ringtoneSettings.setRingtoneName(notificationSoundItem.getNotificationSoundName());
  198. ringtoneSettings.setRingtoneUri(ringtoneUri);
  199. if (callNotificationSounds) {
  200. try {
  201. appPreferences.setCallRingtoneUri(LoganSquare.serialize(ringtoneSettings));
  202. toggleSelection(position);
  203. } catch (IOException e) {
  204. Log.e(TAG, "Failed to store selected ringtone for calls");
  205. }
  206. } else {
  207. try {
  208. appPreferences.setMessageRingtoneUri(LoganSquare.serialize(ringtoneSettings));
  209. toggleSelection(position);
  210. } catch (IOException e) {
  211. Log.e(TAG, "Failed to store selected ringtone for calls");
  212. }
  213. }
  214. }
  215. return true;
  216. }
  217. private void toggleSelection(int position) {
  218. adapter.toggleSelection(position);
  219. ((NotificationSoundItem) adapter.getItem(position)).flipItemSelection();
  220. NotificationSoundItem notificationSoundItem;
  221. for (int i = 0; i < adapter.getItemCount(); i++) {
  222. if (i != position) {
  223. notificationSoundItem = (NotificationSoundItem) adapter.getItem(i);
  224. notificationSoundItem.flipToFront();
  225. }
  226. }
  227. }
  228. private void endMediaPlayer() {
  229. if (cancelMediaPlayerHandler != null) {
  230. cancelMediaPlayerHandler.removeCallbacksAndMessages(null);
  231. }
  232. if (mediaPlayer != null) {
  233. if (mediaPlayer.isPlaying()) {
  234. mediaPlayer.stop();
  235. }
  236. mediaPlayer.release();
  237. mediaPlayer = null;
  238. }
  239. }
  240. @Override
  241. public void onDestroy() {
  242. endMediaPlayer();
  243. super.onDestroy();
  244. }
  245. }