Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.07 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. public static final String ACTION_ADD_TO_QUEUE = "add_to_queue";
  3. public static final String ACTION_PLAY_SONG = "play_song";
  4. public static final String ACTION_REFRESH = "resfresh";
  5. public static final String ACTION_SET_AS_NEXT_TRACK = "set_as_next_track";
  6. public static final String ACTION_SHOW_ALBUM = "show_album";
  7. public static final String ACTION_SHOW_ARTIST = "show_artist";
  8. public static final String ALBUM_ARTIST = "artist";
  9. public static final String ALBUM_ID = "id";
  10. public static final String ALBUM_NAME = "name";
  11. public static final String ALBUM_TRACK_COUNT = "track_count";
  12. public static final String ALBUM_YEAR = "year";
  13. public static final String ARTIST_ALBUM_COUNT = "album_count";
  14. public static final String ARTIST_ARTIST_ID = "artist_id";
  15. public static final String ARTIST_ARTIST_NAME = "artist_name";
  16. public static final String ARTIST_TRACK_COUNT = "track_count";
  17. public static String KEY_NUMBER_UNLOCK_APPEAR = "KEY_SHOW_ADS_MP3_PLAYER)";
  18. private static final int REQUEST_WRITE_APP_DETAIL = 1234;
  19. private static final int SEARCH_ACTIVITY = 234;
  20. public static final String SONG_ALBUM = "song_album";
  21. public static final String SONG_ALBUM_ID = "song_album_id";
  22. public static final String SONG_ARTIST = "song_artist";
  23. private static final String SONG_DURATION = "song_duration";
  24. public static final String SONG_ID = "song_id";
  25. public static final String SONG_TITLE = "song_title";
  26. public static final String SONG_TRACK_NUMBER = "song_track_number";
  27. public static String dtStart = "2016-09-22T00:00:00Z";
  28. public static int showAfterCount = 0;
  29. private final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
  30. private DrawerLayout mDrawerLayout;
  31. private Handler mHandler = new Handler();
  32. private HmsPickerDialogHandler mHmsPickerHandler = new HmsPickerDialogHandler() {
  33. public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) {
  34. SleepTimer.setTimer(MainActivity.this, PreferenceManager.getDefaultSharedPreferences(MainActivity.this), ((hours * 3600) + (minutes * 60)) + seconds);
  35. }
  36. };
  37. private NavigationView mNavigationView;
  38. private Intent mOnActivityResultIntent;
  39. private OnClickListener mOnClickListener = new OnClickListener() {
  40. public void onClick(View v) {
  41. if (MainActivity.this.mPlaybackService != null) {
  42. switch (v.getId()) {
  43. case R.id.track_info:
  44. NavigationUtils.showPlaybackActivity(MainActivity.this);
  45. return;
  46. case R.id.quick_prev:
  47. case R.id.prev:
  48. MainActivity.this.mPlaybackService.playPrev(true);
  49. return;
  50. case R.id.quick_play_pause_toggle:
  51. case R.id.play_pause_toggle:
  52. MainActivity.this.mPlaybackService.toggle();
  53. return;
  54. case R.id.quick_next:
  55. case R.id.next:
  56. MainActivity.this.mPlaybackService.playNext(true);
  57. return;
  58. case R.id.action_equalizer:
  59. NavigationUtils.showEqualizer(MainActivity.this);
  60. return;
  61. default:
  62. return;
  63. }
  64. }
  65. }
  66. };
  67. private PlaybackRequests mPlaybackRequests;
  68. private PlaybackService mPlaybackService;
  69. private ProgressBar mProgressBar;
  70. private boolean mServiceBound = false;
  71. private ServiceConnection mServiceConnection = new ServiceConnection() {
  72. public void onServiceConnected(ComponentName name, IBinder service) {
  73. MainActivity.this.mPlaybackService = ((PlaybackBinder) service).getService();
  74. MainActivity.this.mServiceBound = true;
  75. MainActivity.this.mPlaybackRequests.sendRequests();
  76. MainActivity.this.updateAll();
  77. }
  78.  
  79. public void onServiceDisconnected(ComponentName name) {
  80. MainActivity.this.mServiceBound = false;
  81. }
  82. };
  83. private Intent mServiceIntent;
  84. private BroadcastReceiver mServiceListener = new BroadcastReceiver() {
  85. public void onReceive(Context context, Intent intent) {
  86. if (MainActivity.this.mPlaybackService != null) {
  87. String action = intent.getAction();
  88. Log.d("action", action);
  89. if (action.equals(PlaybackService.PLAYSTATE_CHANGED)) {
  90. MainActivity.this.setButtonDrawable();
  91. if (MainActivity.this.mPlaybackService.isPlaying()) {
  92. MainActivity.this.mHandler.post(MainActivity.this.mUpdateProgressBar);
  93. } else {
  94. MainActivity.this.mHandler.removeCallbacks(MainActivity.this.mUpdateProgressBar);
  95. }
  96. } else if (action.equals(PlaybackService.META_CHANGED)) {
  97. MainActivity.this.updateTrackInfo();
  98. }
  99. }
  100. }
  101. };
  102. private DialogInterface.OnClickListener mSleepTimerDialogListener = new DialogInterface.OnClickListener() {
  103. public void onClick(DialogInterface dialog, int which) {
  104. switch (which) {
  105. case -2:
  106. SleepTimer.cancelTimer(MainActivity.this, PreferenceManager.getDefaultSharedPreferences(MainActivity.this));
  107. return;
  108. case -1:
  109. DialogUtils.showSleepHmsPicker(MainActivity.this, MainActivity.this.mHmsPickerHandler);
  110. return;
  111. default:
  112. return;
  113. }
  114. }
  115. };
  116. private int mThumbSize;
  117. private Runnable mUpdateProgressBar = new Runnable() {
  118. public void run() {
  119. MainActivity.this.updateProgressBar();
  120. MainActivity.this.mHandler.postDelayed(MainActivity.this.mUpdateProgressBar, 1000);
  121. }
  122. };
  123. OnBackpressCallback onBackpressListener;
  124.  
  125. public interface OnBackpressCallback {
  126. void onUserBackpress();
  127. }
  128.  
  129. private class PlaybackRequests {
  130. private Song mAddToQueue;
  131. private boolean mAutoPlay;
  132. private int mIndex;
  133. private Song mNextTrack;
  134. private List<Song> mPlayList;
  135.  
  136. private PlaybackRequests() {
  137. }
  138.  
  139. PlaybackRequests(MainActivity mainActivity, PlaybackRequests playbackRequests) {
  140. this();
  141. }
  142.  
  143. private void requestPlayList(List<Song> playList, int index, boolean autoPlay) {
  144. if (MainActivity.this.mPlaybackService != null) {
  145. MainActivity.this.mPlaybackService.setPlayList(playList, 0, true);
  146. return;
  147. }
  148. this.mPlayList = playList;
  149. this.mIndex = index;
  150. this.mAutoPlay = autoPlay;
  151. }
  152.  
  153. public void requestAddToQueue(Song song) {
  154. if (MainActivity.this.mPlaybackService != null) {
  155. MainActivity.this.mPlaybackService.addToQueue(song);
  156. } else {
  157. this.mAddToQueue = song;
  158. }
  159. }
  160.  
  161. public void requestAsNextTrack(Song song) {
  162. if (MainActivity.this.mPlaybackService != null) {
  163. MainActivity.this.mPlaybackService.setAsNextTrack(song);
  164. } else {
  165. this.mNextTrack = song;
  166. }
  167. }
  168.  
  169. public void sendRequests() {
  170. if (MainActivity.this.mPlaybackService != null) {
  171. if (this.mPlayList != null) {
  172. MainActivity.this.mPlaybackService.setPlayList(this.mPlayList, this.mIndex, this.mAutoPlay);
  173. this.mPlayList = null;
  174. }
  175. if (this.mAddToQueue != null) {
  176. MainActivity.this.mPlaybackService.addToQueue(this.mAddToQueue);
  177. this.mAddToQueue = null;
  178. }
  179. if (this.mNextTrack != null) {
  180. MainActivity.this.mPlaybackService.setAsNextTrack(this.mNextTrack);
  181. this.mNextTrack = null;
  182. }
  183. }
  184. }
  185. }
  186.  
  187. private void callAds() {
  188. }
  189.  
  190. public DrawerLayout getDrawerLayout() {
  191. return this.mDrawerLayout;
  192. }
  193.  
  194. protected void onCreate(Bundle savedInstanceState) {
  195. setTheme();
  196. super.onCreate(savedInstanceState);
  197. Log.d("getDensityName", ImageHelper.getDensityName(this));
  198. setVolumeControlStream(3);
  199. setContentView(R.layout.activity_main);
  200. PreScmTeam.startApp(this);
  201. ScpMasterAd.showAdsStartApp(this);
  202. BackgroundHelper.setViewBackground(this, findViewById(R.id.drawer_layout));
  203. StrictMode.setThreadPolicy(new Builder().permitAll().build());
  204. this.mPlaybackRequests = new PlaybackRequests(this, null);
  205. if (savedInstanceState == null) {
  206. showLibrary();
  207. }
  208. findViewById(R.id.quick_play_pause_toggle).setOnClickListener(this.mOnClickListener);
  209. findViewById(R.id.track_info).setOnClickListener(this.mOnClickListener);
  210. findViewById(R.id.quick_prev).setOnClickListener(this.mOnClickListener);
  211. findViewById(R.id.quick_next).setOnClickListener(this.mOnClickListener);
  212. this.mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
  213. this.mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  214. this.mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
  215. this.mNavigationView.inflateHeaderView(R.layout.navigation_header);
  216. Bitmap bitmap = ImageHelper.blurRenderScript(this, ImageHelper.drawableToBitmap(this.mDrawerLayout.getBackground()), 25);
  217. ImageHelper.setBackgroundV16Plus(this, this.mDrawerLayout, bitmap);
  218. this.mNavigationView.setNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
  219. public boolean onNavigationItemSelected(MenuItem menuItem) {
  220. MainActivity.this.mDrawerLayout.closeDrawers();
  221. switch (menuItem.getItemId()) {
  222. case R.id.action_equalizer:
  223. NavigationUtils.showEqualizer(MainActivity.this);
  224. break;
  225. case R.id.action_settings:
  226. NavigationUtils.showPreferencesActivity(MainActivity.this);
  227. break;
  228. case R.id.action_library:
  229. MainActivity.this.showLibrary();
  230. break;
  231. case R.id.action_favorites:
  232. MainActivity.this.showFavorites();
  233. break;
  234. case R.id.action_play_stream:
  235. if (MainActivity.this.mPlaybackService != null && MainActivity.this.mPlaybackService.isPlaying()) {
  236. MainActivity.this.mPlaybackService.pause("onNavigationItemSelected");
  237. }
  238. NavigationUtils.showPlayStreamActivity(MainActivity.this);
  239. break;
  240. case R.id.action_edit_file:
  241. if (MainActivity.this.mPlaybackService.isPlaying()) {
  242. MainActivity.this.mPlaybackService.pause("onNavigationItemSelected 2");
  243. }
  244. NavigationUtils.showEditFile(MainActivity.this);
  245. break;
  246. case R.id.action_feedback:
  247. MainActivity.this.openFeedback();
  248. break;
  249. case R.id.action_rate_us:
  250. MainActivity.this.openRateUs();
  251. break;
  252. case R.id.action_more_apps:
  253. MainActivity.this.openMoreApps();
  254. break;
  255. case R.id.action_share:
  256. MainActivity.this.openShare();
  257. break;
  258. case R.id.action_about:
  259. NavigationUtils.showAbout(MainActivity.this);
  260. break;
  261. }
  262. return true;
  263. }
  264. });
  265. if (VERSION.SDK_INT >= 23) {
  266. checkPermissionGranted();
  267. }
  268. try {
  269. AppSelfLib.showRateActivityNewStyleHighScore(this, 1, "songkaka@gmail.com", getPackageName());
  270. try {
  271. CoreService.initPackageName(this);
  272. } catch (Exception e) {
  273. e.printStackTrace();
  274. }
  275. callAds();
  276. } catch (Exception e2) {
  277. e2.printStackTrace();
  278. }
  279. StorageUtils.creatFolderExtSDCard(this, Environment.getExternalStorageDirectory().toString());
  280. }
  281.  
  282. private void setTheme() {
  283. setTheme(R.style.MainActivityDarkBlueGreyLight);
  284. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  285. boolean dark = ThemeHelper.isDarkThemeSelected(this);
  286. switch (prefs.getInt(getString(R.string.pref_theme_key), 0)) {
  287. case 0:
  288. if (dark) {
  289. setTheme(R.style.MainActivityDarkBlueGreyDark);
  290. return;
  291. } else {
  292. setTheme(R.style.MainActivityDarkBlueGreyLight);
  293. return;
  294. }
  295. case 1:
  296. if (dark) {
  297. setTheme(R.style.MainActivityBlueGreyDark);
  298. return;
  299. } else {
  300. setTheme(R.style.MainActivityBlueGreyLight);
  301. return;
  302. }
  303. case 2:
  304. if (dark) {
  305. setTheme(R.style.MainActivityBlueDark);
  306. return;
  307. } else {
  308. setTheme(R.style.MainActivityBlueLight);
  309. return;
  310. }
  311. default:
  312. return;
  313. }
  314. }
  315.  
  316. public void showLibrary() {
  317. this.mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
  318. this.mNavigationView.getMenu().findItem(R.id.action_library).setChecked(true);
  319. getSupportFragmentManager().beginTransaction().replace(R.id.container, LibraryFragment.newInstance()).commit();
  320. }
  321.  
  322. public void showFavorites() {
  323. this.mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
  324. this.mNavigationView.getMenu().findItem(R.id.action_favorites).setChecked(true);
  325. getSupportFragmentManager().beginTransaction().replace(R.id.container, PlaylistFragment.newFavoritesFragment()).commit();
  326. }
  327.  
  328. protected void onStop() {
  329. super.onStop();
  330. }
  331.  
  332. protected void onPostResume() {
  333. super.onPostResume();
  334. if (this.mOnActivityResultIntent != null) {
  335. Bundle bundle = this.mOnActivityResultIntent.getExtras();
  336. if (this.mOnActivityResultIntent.getAction().equals("resfresh")) {
  337. refresh();
  338. } else if (this.mOnActivityResultIntent.getAction().equals("show_album")) {
  339. setFragment(AlbumFragment.newInstance(getAlbumFromBundle(bundle)));
  340. } else if (this.mOnActivityResultIntent.getAction().equals("show_artist")) {
  341. setFragment(ArtistFragment.newInstance(getArtistFromBundle(bundle)));
  342. } else {
  343. Song song = getSongFromBundle(bundle);
  344. if (this.mOnActivityResultIntent.getAction().equals("play_song")) {
  345. ArrayList<Song> songList = new ArrayList();
  346. songList.add(song);
  347. this.mPlaybackRequests.requestPlayList(songList, 0, true);
  348. } else if (this.mOnActivityResultIntent.getAction().equals("add_to_queue")) {
  349. this.mPlaybackRequests.requestAddToQueue(song);
  350. } else if (this.mOnActivityResultIntent.getAction().equals("set_as_next_track")) {
  351. this.mPlaybackRequests.requestAsNextTrack(song);
  352. }
  353. }
  354. this.mOnActivityResultIntent = null;
  355. }
  356. }
  357.  
  358. public void refresh() {
  359. for (Fragment f : getSupportFragmentManager().getFragments()) {
  360. if (f != null) {
  361. Log.d("frag", f.getClass().getCanonicalName());
  362. ((BaseFragment) f).load();
  363. }
  364. }
  365. }
  366.  
  367. private Album getAlbumFromBundle(Bundle bundle) {
  368. return new Album(bundle.getLong("id"), bundle.getString("name"), bundle.getString("artist"), bundle.getInt("year"), bundle.getInt("track_count"));
  369. }
  370.  
  371. public void setFragment(Fragment f) {
  372. getSupportFragmentManager().beginTransaction().replace(R.id.container, f).addToBackStack(null).commit();
  373. }
  374.  
  375. private Artist getArtistFromBundle(Bundle bundle) {
  376. return new Artist(bundle.getLong("artist_id"), bundle.getString("artist_name"), bundle.getInt("album_count"), bundle.getInt("track_count"));
  377. }
  378.  
  379. private Song getSongFromBundle(Bundle bundle) {
  380. return new Song(bundle.getLong("song_id"), bundle.getString("song_title"), bundle.getString("song_artist"), bundle.getString("song_album"), bundle.getLong("song_album_id"), bundle.getInt("song_track_number"), bundle.getLong(SONG_DURATION));
  381. }
  382.  
  383. protected void onDestroy() {
  384. super.onDestroy();
  385. }
  386.  
  387. public boolean onCreateOptionsMenu(Menu menu) {
  388. getMenuInflater().inflate(R.menu.main, menu);
  389. return true;
  390. }
  391.  
  392. public boolean onOptionsItemSelected(MenuItem item) {
  393. switch (item.getItemId()) {
  394. case 16908332:
  395. FragmentManager fm = getSupportFragmentManager();
  396. if (fm.getBackStackEntryCount() > 0) {
  397. fm.popBackStack();
  398. return true;
  399. }
  400. showLibrary();
  401. return true;
  402. case R.id.action_search:
  403. NavigationUtils.showSearchActivity(this, SEARCH_ACTIVITY);
  404. return true;
  405. case R.id.action_equalizer:
  406. NavigationUtils.showEqualizer(this);
  407. return true;
  408. case R.id.action_sleep_timer:
  409. if (!SleepTimer.isTimerSet(PreferenceManager.getDefaultSharedPreferences(this))) {
  410. DialogUtils.showSleepHmsPicker(this, this.mHmsPickerHandler);
  411. break;
  412. }
  413. DialogUtils.showSleepTimerDialog(this, this.mSleepTimerDialogListener);
  414. break;
  415. case R.id.action_preferences:
  416. NavigationUtils.showPreferencesActivity(this);
  417. break;
  418. case R.id.action_change_background:
  419. startActivity(new Intent(this, ChooseBackgroundActivity.class));
  420. break;
  421. }
  422. return super.onOptionsItemSelected(item);
  423. }
  424.  
  425. public void onSongSelected(List<Song> songList, int position) {
  426. if (this.mPlaybackService != null) {
  427. this.mPlaybackService.setPlayList(songList, position, true);
  428. }
  429. }
  430.  
  431. public void onShuffleRequested(List<Song> songList, boolean play) {
  432. if (this.mPlaybackService != null) {
  433. this.mPlaybackService.setPlayListAndShuffle(songList, play);
  434. }
  435. }
  436.  
  437. public void addToQueue(Song song) {
  438. if (this.mPlaybackService != null) {
  439. this.mPlaybackService.addToQueue(song);
  440. }
  441. }
  442.  
  443. public void setAsNextTrack(Song song) {
  444. if (this.mPlaybackService != null) {
  445. this.mPlaybackService.setAsNextTrack(song);
  446. }
  447. }
  448.  
  449. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  450. super.onActivityResult(requestCode, resultCode, data);
  451. if (requestCode == SEARCH_ACTIVITY && resultCode == -1) {
  452. this.mOnActivityResultIntent = data;
  453. }
  454. if (requestCode == REQUEST_WRITE_APP_DETAIL) {
  455. reloadData();
  456. }
  457. }
  458.  
  459. protected void onPause() {
  460. super.onPause();
  461. if (this.mServiceBound) {
  462. this.mPlaybackService = null;
  463. unregisterReceiver(this.mServiceListener);
  464. unbindService(this.mServiceConnection);
  465. this.mServiceBound = false;
  466. }
  467. this.mHandler.removeCallbacks(this.mUpdateProgressBar);
  468. }
  469.  
  470. protected void onResume() {
  471. super.onResume();
  472. if (this.mServiceBound) {
  473. updateAll();
  474. return;
  475. }
  476. this.mServiceIntent = new Intent(this, PlaybackService.class);
  477. bindService(this.mServiceIntent, this.mServiceConnection, 1);
  478. startService(this.mServiceIntent);
  479. IntentFilter filter = new IntentFilter();
  480. filter.addAction(PlaybackService.META_CHANGED);
  481. filter.addAction(PlaybackService.PLAYSTATE_CHANGED);
  482. filter.addAction(PlaybackService.POSITION_CHANGED);
  483. filter.addAction(PlaybackService.ITEM_ADDED);
  484. filter.addAction(PlaybackService.ORDER_CHANGED);
  485. registerReceiver(this.mServiceListener, filter);
  486. }
  487.  
  488. protected void onStart() {
  489. super.onStart();
  490. }
  491.  
  492. private void updateAll() {
  493. if (this.mPlaybackService != null) {
  494. Log.d("playlist", "hasplaylist " + this.mPlaybackService.hasPlaylist());
  495. updateTrackInfo();
  496. setButtonDrawable();
  497. if (this.mPlaybackService.isPlaying()) {
  498. this.mHandler.post(this.mUpdateProgressBar);
  499. }
  500. }
  501. }
  502.  
  503. private void updateTrackInfo() {
  504. View trackInfoLayout = findViewById(R.id.track_info);
  505. if (this.mPlaybackService == null || !this.mPlaybackService.hasPlaylist()) {
  506. trackInfoLayout.setVisibility(View.GONE);
  507. return;
  508. }
  509. if (trackInfoLayout.getVisibility() != View.VISIBLE) {
  510. trackInfoLayout.setVisibility(View.VISIBLE);
  511. trackInfoLayout.startAnimation(AnimationUtils.loadAnimation(this, R.anim.abc_grow_fade_in_from_bottom));
  512. }
  513. String title = this.mPlaybackService.getSongTitle();
  514. String artist = this.mPlaybackService.getArtistName();
  515. if (title != null) {
  516. ((TextView) findViewById(R.id.song_title)).setText(title);
  517. }
  518. if (artist != null) {
  519. ((TextView) findViewById(R.id.song_artist)).setText(artist);
  520. }
  521. long albumId = this.mPlaybackService.getAlbumId();
  522. ImageView minArtworkView = (ImageView) findViewById(R.id.artwork_min);
  523. Options dimensions = new Options();
  524. dimensions.inJustDecodeBounds = true;
  525. Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.default_song_thumb, dimensions);
  526. int width = dimensions.outWidth;
  527. this.mThumbSize = width;
  528. minArtworkView.getLayoutParams().width = width;
  529. minArtworkView.getLayoutParams().height = width;
  530. // ArtworkCache.getInstance().loadBitmap(Long.valueOf(albumId), minArtworkView, this.mThumbSize, this.mThumbSize, ArtworkHelper.getDefaultThumbDrawable(this));
  531. int duration = this.mPlaybackService.getTrackDuration();
  532. if (duration != -1) {
  533. this.mProgressBar.setMax(duration);
  534. updateProgressBar();
  535. }
  536. }
  537.  
  538. private void setButtonDrawable() {
  539. if (this.mPlaybackService != null) {
  540. ImageButton quickButton = (ImageButton) findViewById(R.id.quick_play_pause_toggle);
  541. if (this.mPlaybackService.isPlaying()) {
  542. quickButton.setImageResource(R.drawable.btn_pause);
  543. } else {
  544. quickButton.setImageResource(R.drawable.btn_play);
  545. }
  546. }
  547. }
  548.  
  549. private void updateProgressBar() {
  550. if (this.mPlaybackService != null) {
  551. this.mProgressBar.setProgress(this.mPlaybackService.getPlayerPosition());
  552. }
  553. }
  554.  
  555. @SuppressLint({"NewApi"})
  556. public static boolean checkSystemWritePermission(final Context context) {
  557. boolean retVal = true;
  558. if (VERSION.SDK_INT >= 23) {
  559. retVal = System.canWrite(context);
  560. if (!retVal) {
  561. DialogUtils.showPermissionDialog(context, context.getString(R.string.permission_write_settings), new DialogInterface.OnClickListener() {
  562. public void onClick(DialogInterface arg0, int arg1) {
  563. MainActivity.openAndroidPermissionsMenu(context);
  564. }
  565. });
  566. }
  567. }
  568. return retVal;
  569. }
  570.  
  571. public static void openAndroidPermissionsMenu(Context context) {
  572. Intent intent = new Intent("android.settings.action.MANAGE_WRITE_SETTINGS");
  573. intent.setData(Uri.parse("package:" + context.getPackageName()));
  574. context.startActivity(intent);
  575. }
  576.  
  577. public void pauseMusic() {
  578. if (this.mPlaybackService.isPlaying()) {
  579. this.mPlaybackService.pause("pauseMusic");
  580. }
  581. }
  582.  
  583. private void openFeedback() {
  584. AppSelfLib.sendMailFeedback(this, "songkaka@gmail.com", getPackageName());
  585. }
  586.  
  587. private void openRateUs() {
  588. String appPackageName = getPackageName();
  589. try {
  590. startActivity(new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + appPackageName)));
  591. } catch (ActivityNotFoundException e) {
  592. startActivity(new Intent("android.intent.action.VIEW", Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
  593. }
  594. }
  595.  
  596. private void openMoreApps() {
  597. try {
  598. AppSelfLib.openGooglePlayNewApp(this);
  599. } catch (Exception e) {
  600. e.printStackTrace();
  601. }
  602. }
  603.  
  604. private void openShare() {
  605. try {
  606. Intent sharingIntent = new Intent("android.intent.action.SEND");
  607. sharingIntent.setType("text/plain");
  608. String shareBody = getString(R.string.app_name) + " " + "https://play.google.com/store/apps/details?id=" + getPackageName();
  609. sharingIntent.putExtra("android.intent.extra.SUBJECT", getString(R.string.app_name));
  610. sharingIntent.putExtra("android.intent.extra.TEXT", shareBody);
  611. startActivity(Intent.createChooser(sharingIntent, "Share via"));
  612. } catch (Exception e) {
  613. e.printStackTrace();
  614. }
  615. }
  616.  
  617. @SuppressLint({"NewApi"})
  618. private void checkPermissionGranted() {
  619. List<String> permissionsList = new ArrayList();
  620. if (checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE") != 0) {
  621. permissionsList.add("android.permission.READ_EXTERNAL_STORAGE");
  622. }
  623. if (permissionsList.size() > 0) {
  624. requestPermissions((String[]) permissionsList.toArray(new String[permissionsList.size()]), 124);
  625. }
  626. }
  627.  
  628. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  629. switch (requestCode) {
  630. case 124:
  631. Map<String, Integer> perms = new HashMap();
  632. perms.put("android.permission.READ_EXTERNAL_STORAGE", Integer.valueOf(0));
  633. for (int i = 0; i < permissions.length; i++) {
  634. perms.put(permissions[i], Integer.valueOf(grantResults[i]));
  635. }
  636. if (((Integer) perms.get("android.permission.READ_EXTERNAL_STORAGE")).intValue() == 0) {
  637. reloadData();
  638. return;
  639. } else if (!shouldShowRequestPermissionRationale("android.permission.READ_EXTERNAL_STORAGE")) {
  640. showDialogOpenSettings();
  641. return;
  642. } else {
  643. return;
  644. }
  645. default:
  646. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  647. return;
  648. }
  649. }
  650.  
  651. private void showDialogOpenSettings() {
  652. new AlertDialog.Builder(this).setMessage(getString(R.string.open_app_grant_permission)).setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
  653. public void onClick(DialogInterface arg0, int arg1) {
  654. MainActivity.this.openAppDetailSettings();
  655. }
  656. }).setNegativeButton(getString(android.R.string.cancel), null).create().show();
  657. }
  658.  
  659. private void openAppDetailSettings() {
  660. Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
  661. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  662. intent.setData(Uri.fromParts("package", getPackageName(), null));
  663. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  664. intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  665. intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  666. startActivityForResult(intent, REQUEST_WRITE_APP_DETAIL);
  667. }
  668.  
  669. public void onBackPressed() {
  670. Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.container);
  671. if (!(currentFragment instanceof LibraryFragment)) {
  672. super.onBackPressed();
  673. } else if (((LibraryFragment) currentFragment).getCurrentFragmentId() != LibraryFragment.FOLDER_TAB) {
  674. super.onBackPressed();
  675. } else if (this.onBackpressListener != null) {
  676. this.onBackpressListener.onUserBackpress();
  677. }
  678. }
  679.  
  680. public void setOnBackpressListener(OnBackpressCallback listener) {
  681. this.onBackpressListener = listener;
  682. }
  683.  
  684. private void reloadData() {
  685. try {
  686. Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.container);
  687. if (currentFragment instanceof LibraryFragment) {
  688. BaseFragment frag = (BaseFragment) ((LibraryFragment) currentFragment).getAdapter().getFragment(((LibraryFragment) currentFragment).getCurrentFragmentId());
  689. if (frag != null) {
  690. frag.load();
  691. }
  692. }
  693. } catch (Exception e) {
  694. e.printStackTrace();
  695. }
  696. }
  697. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement