This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
/
MainActivity.java
377 lines (331 loc) · 14.4 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.kabouzeid.gramophone.ui.activities;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.navigation.NavigationView;
import androidx.fragment.app.Fragment;
import androidx.drawerlayout.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.kabouzeid.appthemehelper.ThemeStore;
import com.kabouzeid.appthemehelper.util.ATHUtil;
import com.kabouzeid.appthemehelper.util.NavigationViewUtil;
import com.kabouzeid.gramophone.App;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.dialogs.ChangelogDialog;
import com.kabouzeid.gramophone.dialogs.ScanMediaFolderChooserDialog;
import com.kabouzeid.gramophone.glide.SongGlideRequest;
import com.kabouzeid.gramophone.helper.MusicPlayerRemote;
import com.kabouzeid.gramophone.helper.SearchQueryHelper;
import com.kabouzeid.gramophone.loader.AlbumLoader;
import com.kabouzeid.gramophone.loader.ArtistLoader;
import com.kabouzeid.gramophone.loader.PlaylistSongLoader;
import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.service.MusicService;
import com.kabouzeid.gramophone.ui.activities.base.AbsSlidingMusicPanelActivity;
import com.kabouzeid.gramophone.ui.activities.intro.AppIntroActivity;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.folders.FoldersFragment;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.LibraryFragment;
import com.kabouzeid.gramophone.util.MusicUtil;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AbsSlidingMusicPanelActivity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final int APP_INTRO_REQUEST = 100;
private static final int LIBRARY = 0;
private static final int FOLDERS = 1;
@BindView(R.id.navigation_view)
NavigationView navigationView;
@BindView(R.id.drawer_layout)
DrawerLayout drawerLayout;
@Nullable
MainActivityFragmentCallbacks currentFragment;
@Nullable
private View navigationDrawerHeader;
private boolean blockRequestPermissions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDrawUnderStatusbar();
ButterKnife.bind(this);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
navigationView.setFitsSystemWindows(false); // for header to go below statusbar
}
setUpDrawerLayout();
if (savedInstanceState == null) {
setMusicChooser(PreferenceUtil.getInstance(this).getLastMusicChooser());
} else {
restoreCurrentFragment();
}
if (!checkShowIntro()) {
showChangelog();
}
App.setOnProVersionChangedListener(() -> {
// called if the cached value was outdated (should be a rare event)
checkSetUpPro();
if (!App.isProVersion() && PreferenceUtil.getInstance(MainActivity.this).getLastMusicChooser() == FOLDERS) {
setMusicChooser(FOLDERS); // shows the purchase activity and switches to LIBRARY
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
App.setOnProVersionChangedListener(null);
}
private void setMusicChooser(int key) {
if (!App.isProVersion() && key == FOLDERS) {
Toast.makeText(this, R.string.folder_view_is_a_pro_feature, Toast.LENGTH_LONG).show();
startActivity(new Intent(this, PurchaseActivity.class));
key = LIBRARY;
}
PreferenceUtil.getInstance(this).setLastMusicChooser(key);
switch (key) {
case LIBRARY:
navigationView.setCheckedItem(R.id.nav_library);
setCurrentFragment(LibraryFragment.newInstance());
break;
case FOLDERS:
navigationView.setCheckedItem(R.id.nav_folders);
setCurrentFragment(FoldersFragment.newInstance(this));
break;
}
}
private void setCurrentFragment(@SuppressWarnings("NullableProblems") Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment, null).commit();
currentFragment = (MainActivityFragmentCallbacks) fragment;
}
private void restoreCurrentFragment() {
currentFragment = (MainActivityFragmentCallbacks) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == APP_INTRO_REQUEST) {
blockRequestPermissions = false;
if (!hasPermissions()) {
requestPermissions();
}
}
}
@Override
protected void requestPermissions() {
if (!blockRequestPermissions) super.requestPermissions();
}
@Override
protected View createContentView() {
@SuppressLint("InflateParams")
View contentView = getLayoutInflater().inflate(R.layout.activity_main_drawer_layout, null);
ViewGroup drawerContent = contentView.findViewById(R.id.drawer_content_container);
drawerContent.addView(wrapSlidingMusicPanel(R.layout.activity_main_content));
return contentView;
}
private void setUpNavigationView() {
int accentColor = ThemeStore.accentColor(this);
NavigationViewUtil.setItemIconColors(navigationView, ATHUtil.resolveColor(this, R.attr.iconColor, ThemeStore.textColorSecondary(this)), accentColor);
NavigationViewUtil.setItemTextColors(navigationView, ThemeStore.textColorPrimary(this), accentColor);
checkSetUpPro();
navigationView.setNavigationItemSelectedListener(menuItem -> {
drawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.nav_library:
new Handler().postDelayed(() -> setMusicChooser(LIBRARY), 200);
break;
case R.id.nav_folders:
new Handler().postDelayed(() -> setMusicChooser(FOLDERS), 200);
break;
case R.id.buy_pro:
new Handler().postDelayed(() -> startActivity(new Intent(MainActivity.this, PurchaseActivity.class)), 200);
break;
case R.id.action_scan:
new Handler().postDelayed(() -> {
ScanMediaFolderChooserDialog dialog = ScanMediaFolderChooserDialog.create();
dialog.show(getSupportFragmentManager(), "SCAN_MEDIA_FOLDER_CHOOSER");
}, 200);
break;
case R.id.nav_settings:
new Handler().postDelayed(() -> startActivity(new Intent(MainActivity.this, SettingsActivity.class)), 200);
break;
case R.id.nav_about:
new Handler().postDelayed(() -> startActivity(new Intent(MainActivity.this, AboutActivity.class)), 200);
break;
}
return true;
});
}
private void checkSetUpPro() {
navigationView.getMenu().setGroupVisible(R.id.navigation_drawer_menu_category_buy_pro, !App.isProVersion());
}
private void setUpDrawerLayout() {
setUpNavigationView();
}
private void updateNavigationDrawerHeader() {
if (!MusicPlayerRemote.getPlayingQueue().isEmpty()) {
Song song = MusicPlayerRemote.getCurrentSong();
if (navigationDrawerHeader == null) {
navigationDrawerHeader = navigationView.inflateHeaderView(R.layout.navigation_drawer_header);
//noinspection ConstantConditions
navigationDrawerHeader.setOnClickListener(v -> {
drawerLayout.closeDrawers();
if (getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) {
expandPanel();
}
});
}
((TextView) navigationDrawerHeader.findViewById(R.id.title)).setText(song.title);
((TextView) navigationDrawerHeader.findViewById(R.id.text)).setText(MusicUtil.getSongInfoString(song));
SongGlideRequest.Builder.from(Glide.with(this), song)
.checkIgnoreMediaStore(this).build()
.into(((ImageView) navigationDrawerHeader.findViewById(R.id.image)));
} else {
if (navigationDrawerHeader != null) {
navigationView.removeHeaderView(navigationDrawerHeader);
navigationDrawerHeader = null;
}
}
}
@Override
public void onPlayingMetaChanged() {
super.onPlayingMetaChanged();
updateNavigationDrawerHeader();
}
@Override
public void onServiceConnected() {
super.onServiceConnected();
updateNavigationDrawerHeader();
handlePlaybackIntent(getIntent());
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (drawerLayout.isDrawerOpen(navigationView)) {
drawerLayout.closeDrawer(navigationView);
} else {
drawerLayout.openDrawer(navigationView);
}
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean handleBackPress() {
if (drawerLayout.isDrawerOpen(navigationView)) {
drawerLayout.closeDrawers();
return true;
}
return super.handleBackPress() || (currentFragment != null && currentFragment.handleBackPress());
}
private void handlePlaybackIntent(@Nullable Intent intent) {
if (intent == null) {
return;
}
Uri uri = intent.getData();
String mimeType = intent.getType();
boolean handled = false;
if (intent.getAction() != null && intent.getAction().equals(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)) {
final List<Song> songs = SearchQueryHelper.getSongs(this, intent.getExtras());
if (MusicPlayerRemote.getShuffleMode() == MusicService.SHUFFLE_MODE_SHUFFLE) {
MusicPlayerRemote.openAndShuffleQueue(songs, true);
} else {
MusicPlayerRemote.openQueue(songs, 0, true);
}
handled = true;
}
if (uri != null && uri.toString().length() > 0) {
MusicPlayerRemote.playFromUri(uri);
handled = true;
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
final long id = parseIdFromIntent(intent, "playlistId", "playlist");
if (id >= 0) {
int position = intent.getIntExtra("position", 0);
List<Song> songs = new ArrayList<>(PlaylistSongLoader.getPlaylistSongList(this, id));
MusicPlayerRemote.openQueue(songs, position, true);
handled = true;
}
} else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) {
final long id = parseIdFromIntent(intent, "albumId", "album");
if (id >= 0) {
int position = intent.getIntExtra("position", 0);
MusicPlayerRemote.openQueue(AlbumLoader.getAlbum(this, id).songs, position, true);
handled = true;
}
} else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) {
final long id = parseIdFromIntent(intent, "artistId", "artist");
if (id >= 0) {
int position = intent.getIntExtra("position", 0);
MusicPlayerRemote.openQueue(ArtistLoader.getArtist(this, id).getSongs(), position, true);
handled = true;
}
}
if (handled) {
setIntent(new Intent());
}
}
private long parseIdFromIntent(@NonNull Intent intent, String longKey,
String stringKey) {
long id = intent.getLongExtra(longKey, -1);
if (id < 0) {
String idString = intent.getStringExtra(stringKey);
if (idString != null) {
try {
id = Long.parseLong(idString);
} catch (NumberFormatException e) {
Log.e(TAG, e.getMessage());
}
}
}
return id;
}
@Override
public void onPanelExpanded(View view) {
super.onPanelExpanded(view);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
@Override
public void onPanelCollapsed(View view) {
super.onPanelCollapsed(view);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
private boolean checkShowIntro() {
if (!PreferenceUtil.getInstance(this).introShown()) {
PreferenceUtil.getInstance(this).setIntroShown();
ChangelogDialog.setChangelogRead(this);
blockRequestPermissions = true;
new Handler().postDelayed(() -> startActivityForResult(new Intent(MainActivity.this, AppIntroActivity.class), APP_INTRO_REQUEST), 50);
return true;
}
return false;
}
private void showChangelog() {
try {
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int currentVersion = pInfo.versionCode;
if (currentVersion != PreferenceUtil.getInstance(this).getLastChangelogVersion()) {
ChangelogDialog.create().show(getSupportFragmentManager(), "CHANGE_LOG_DIALOG");
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
public interface MainActivityFragmentCallbacks {
boolean handleBackPress();
}
}