RingtoneSelectionController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.content.Context;
  23. import android.database.Cursor;
  24. import android.media.MediaPlayer;
  25. import android.media.RingtoneManager;
  26. import android.net.Uri;
  27. import android.os.Bundle;
  28. import android.os.Handler;
  29. import android.text.TextUtils;
  30. import android.util.Log;
  31. import android.view.LayoutInflater;
  32. import android.view.MenuItem;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import androidx.annotation.NonNull;
  36. import androidx.recyclerview.widget.RecyclerView;
  37. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  38. import autodagger.AutoInjector;
  39. import butterknife.BindView;
  40. import com.bluelinelabs.logansquare.LoganSquare;
  41. import com.nextcloud.talk.R;
  42. import com.nextcloud.talk.adapters.items.NotificationSoundItem;
  43. import com.nextcloud.talk.application.NextcloudTalkApplication;
  44. import com.nextcloud.talk.controllers.base.BaseController;
  45. import com.nextcloud.talk.models.RingtoneSettings;
  46. import com.nextcloud.talk.utils.NotificationUtils;
  47. import com.nextcloud.talk.utils.bundle.BundleKeys;
  48. import com.nextcloud.talk.utils.preferences.AppPreferences;
  49. import eu.davidea.flexibleadapter.FlexibleAdapter;
  50. import eu.davidea.flexibleadapter.SelectableAdapter;
  51. import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
  52. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
  53. import javax.inject.Inject;
  54. import java.io.IOException;
  55. import java.util.ArrayList;
  56. import java.util.List;
  57. @AutoInjector(NextcloudTalkApplication.class)
  58. public class RingtoneSelectionController extends BaseController implements FlexibleAdapter.OnItemClickListener {
  59. private static final String TAG = "RingtoneSelectionController";
  60. @BindView(R.id.recycler_view)
  61. RecyclerView recyclerView;
  62. @BindView(R.id.swipe_refresh_layout)
  63. SwipeRefreshLayout swipeRefreshLayout;
  64. @Inject
  65. AppPreferences appPreferences;
  66. @Inject
  67. Context context;
  68. private FlexibleAdapter adapter;
  69. private RecyclerView.AdapterDataObserver adapterDataObserver;
  70. private List<AbstractFlexibleItem> abstractFlexibleItemList = new ArrayList<>();
  71. private boolean callNotificationSounds;
  72. private MediaPlayer mediaPlayer;
  73. private Handler cancelMediaPlayerHandler;
  74. public RingtoneSelectionController(Bundle args) {
  75. super(args);
  76. setHasOptionsMenu(true);
  77. this.callNotificationSounds = args.getBoolean(BundleKeys.INSTANCE.getKEY_ARE_CALL_SOUNDS(), false);
  78. }
  79. @Override
  80. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  81. return inflater.inflate(R.layout.controller_generic_rv, container, false);
  82. }
  83. @Override
  84. protected void onViewBound(@NonNull View view) {
  85. super.onViewBound(view);
  86. NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
  87. if (adapter == null) {
  88. adapter = new FlexibleAdapter<>(abstractFlexibleItemList, getActivity(), false);
  89. adapter.setNotifyChangeOfUnfilteredItems(true)
  90. .setMode(SelectableAdapter.Mode.SINGLE);
  91. adapter.addListener(this);
  92. cancelMediaPlayerHandler = new Handler();
  93. }
  94. adapter.addListener(this);
  95. prepareViews();
  96. fetchNotificationSounds();
  97. }
  98. @Override
  99. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  100. if (item.getItemId() == android.R.id.home) {
  101. return getRouter().popCurrentController();
  102. }
  103. return super.onOptionsItemSelected(item);
  104. }
  105. private void prepareViews() {
  106. RecyclerView.LayoutManager layoutManager = new SmoothScrollLinearLayoutManager(getActivity());
  107. recyclerView.setLayoutManager(layoutManager);
  108. recyclerView.setHasFixedSize(true);
  109. recyclerView.setAdapter(adapter);
  110. adapterDataObserver = new RecyclerView.AdapterDataObserver() {
  111. @Override
  112. public void onChanged() {
  113. super.onChanged();
  114. findSelectedSound();
  115. }
  116. };
  117. adapter.registerAdapterDataObserver(adapterDataObserver);
  118. swipeRefreshLayout.setEnabled(false);
  119. }
  120. @SuppressLint("LongLogTag")
  121. private void findSelectedSound() {
  122. boolean foundDefault = false;
  123. String preferencesString = null;
  124. if ((callNotificationSounds && TextUtils.isEmpty((preferencesString = appPreferences.getCallRingtoneUri())))
  125. || (!callNotificationSounds && TextUtils.isEmpty((preferencesString = appPreferences
  126. .getMessageRingtoneUri())))) {
  127. adapter.toggleSelection(1);
  128. foundDefault = true;
  129. }
  130. if (!TextUtils.isEmpty(preferencesString) && !foundDefault) {
  131. try {
  132. RingtoneSettings ringtoneSettings = LoganSquare.parse(preferencesString, RingtoneSettings.class);
  133. if (ringtoneSettings.getRingtoneUri() == null) {
  134. adapter.toggleSelection(0);
  135. } else if (ringtoneSettings.getRingtoneUri().toString().equals(getRingtoneString())) {
  136. adapter.toggleSelection(1);
  137. } else {
  138. NotificationSoundItem notificationSoundItem;
  139. for (int i = 2; i < adapter.getItemCount(); i++) {
  140. notificationSoundItem = (NotificationSoundItem) adapter.getItem(i);
  141. if (notificationSoundItem.getNotificationSoundUri().equals(ringtoneSettings.getRingtoneUri().toString())) {
  142. adapter.toggleSelection(i);
  143. break;
  144. }
  145. }
  146. }
  147. } catch (IOException e) {
  148. Log.e(TAG, "Failed to parse ringtone settings");
  149. }
  150. }
  151. adapter.unregisterAdapterDataObserver(adapterDataObserver);
  152. adapterDataObserver = null;
  153. }
  154. private String getRingtoneString() {
  155. if (callNotificationSounds) {
  156. return NotificationUtils.DEFAULT_CALL_RINGTONE_URI;
  157. } else {
  158. return NotificationUtils.DEFAULT_MESSAGE_RINGTONE_URI;
  159. }
  160. }
  161. private void fetchNotificationSounds() {
  162. abstractFlexibleItemList.add(new NotificationSoundItem(getResources().getString(R.string.nc_settings_no_ringtone), null));
  163. abstractFlexibleItemList.add(new NotificationSoundItem(getResources()
  164. .getString(R.string.nc_settings_default_ringtone), getRingtoneString()));
  165. if (getActivity() != null) {
  166. RingtoneManager manager = new RingtoneManager(getActivity());
  167. if (callNotificationSounds) {
  168. manager.setType(RingtoneManager.TYPE_RINGTONE);
  169. } else {
  170. manager.setType(RingtoneManager.TYPE_NOTIFICATION);
  171. }
  172. Cursor cursor = manager.getCursor();
  173. NotificationSoundItem notificationSoundItem;
  174. while (cursor.moveToNext()) {
  175. String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
  176. String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
  177. String completeNotificationUri = notificationUri + "/" + cursor.getString(RingtoneManager
  178. .ID_COLUMN_INDEX);
  179. notificationSoundItem = new NotificationSoundItem(notificationTitle, completeNotificationUri);
  180. abstractFlexibleItemList.add(notificationSoundItem);
  181. }
  182. }
  183. adapter.updateDataSet(abstractFlexibleItemList, false);
  184. }
  185. @Override
  186. protected String getTitle() {
  187. return getResources().getString(R.string.nc_settings_notification_sounds);
  188. }
  189. @SuppressLint("LongLogTag")
  190. @Override
  191. public boolean onItemClick(View view, int position) {
  192. NotificationSoundItem notificationSoundItem = (NotificationSoundItem) adapter.getItem(position);
  193. Uri ringtoneUri = null;
  194. if (!TextUtils.isEmpty(notificationSoundItem.getNotificationSoundUri())) {
  195. ringtoneUri = Uri.parse(notificationSoundItem.getNotificationSoundUri());
  196. endMediaPlayer();
  197. mediaPlayer = MediaPlayer.create(getActivity(), ringtoneUri);
  198. cancelMediaPlayerHandler = new Handler();
  199. cancelMediaPlayerHandler.postDelayed(new Runnable() {
  200. @Override
  201. public void run() {
  202. endMediaPlayer();
  203. }
  204. }, mediaPlayer.getDuration() + 25);
  205. mediaPlayer.start();
  206. }
  207. if (adapter.getSelectedPositions().size() == 0 || adapter.getSelectedPositions().get(0) != position) {
  208. RingtoneSettings ringtoneSettings = new RingtoneSettings();
  209. ringtoneSettings.setRingtoneName(notificationSoundItem.getNotificationSoundName());
  210. ringtoneSettings.setRingtoneUri(ringtoneUri);
  211. if (callNotificationSounds) {
  212. try {
  213. appPreferences.setCallRingtoneUri(LoganSquare.serialize(ringtoneSettings));
  214. adapter.toggleSelection(position);
  215. adapter.notifyDataSetChanged();
  216. } catch (IOException e) {
  217. Log.e(TAG, "Failed to store selected ringtone for calls");
  218. }
  219. } else {
  220. try {
  221. appPreferences.setMessageRingtoneUri(LoganSquare.serialize(ringtoneSettings));
  222. adapter.toggleSelection(position);
  223. adapter.notifyDataSetChanged();
  224. } catch (IOException e) {
  225. Log.e(TAG, "Failed to store selected ringtone for calls");
  226. }
  227. }
  228. }
  229. return true;
  230. }
  231. private void endMediaPlayer() {
  232. if (cancelMediaPlayerHandler != null) {
  233. cancelMediaPlayerHandler.removeCallbacksAndMessages(null);
  234. }
  235. if (mediaPlayer != null) {
  236. if (mediaPlayer.isPlaying()) {
  237. mediaPlayer.stop();
  238. }
  239. mediaPlayer.release();
  240. mediaPlayer = null;
  241. }
  242. }
  243. @Override
  244. public void onDestroy() {
  245. endMediaPlayer();
  246. super.onDestroy();
  247. }
  248. }