From 43eb6995e8b9896fc208fcb14d5ebf925a66540f Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 11 Aug 2015 21:18:08 -0700 Subject: #535 Move online/offline to pull out drawer --- .../dsub/activity/SubsonicActivity.java | 154 +++++++++++++++++++- .../dsub/activity/SubsonicFragmentActivity.java | 25 +--- .../daneren2005/dsub/fragments/MainFragment.java | 156 +++------------------ 3 files changed, 174 insertions(+), 161 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java index bb20843d..f336d178 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -20,6 +20,7 @@ package github.daneren2005.dsub.activity; import android.app.UiModeManager; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; @@ -36,6 +37,7 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; @@ -53,6 +55,7 @@ import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; +import android.widget.CheckBox; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; @@ -67,6 +70,8 @@ import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.fragments.SubsonicFragment; import github.daneren2005.dsub.service.DownloadService; import github.daneren2005.dsub.service.HeadphoneListenerService; +import github.daneren2005.dsub.service.MusicService; +import github.daneren2005.dsub.service.MusicServiceFactory; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.DrawableTint; import github.daneren2005.dsub.util.ImageLoader; @@ -247,9 +252,9 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte drawerList.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(final MenuItem menuItem) { - if (showingTabs) { + if(showingTabs) { // Settings are on a different selectable track - if (menuItem.getItemId() != R.id.drawer_settings && menuItem.getItemId() != R.id.drawer_admin) { + if (menuItem.getItemId() != R.id.drawer_settings && menuItem.getItemId() != R.id.drawer_admin && menuItem.getItemId() != R.id.drawer_offline) { menuItem.setChecked(true); lastSelectedPosition = menuItem.getItemId(); } @@ -295,6 +300,9 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte case R.id.drawer_downloading: drawerItemSelected("Download"); return true; + case R.id.drawer_offline: + toggleOffline(); + return true; case R.id.drawer_settings: startActivity(new Intent(SubsonicActivity.this, SettingsActivity.class)); drawer.closeDrawers(); @@ -328,7 +336,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte drawerUserName = (TextView) drawerHeader.findViewById(R.id.header_user_name); drawerUserAvatar = (ImageView) drawerHeader.findViewById(R.id.header_user_avatar); - populateTabs(); + updateDrawerHeader(); if(!isTv()) { @@ -555,6 +563,25 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte boolean chatEnabled = prefs.getBoolean(Constants.PREFERENCES_KEY_CHAT_ENABLED, true) && !Util.isOffline(this); boolean adminEnabled = prefs.getBoolean(Constants.PREFERENCES_KEY_ADMIN_ENABLED, true) && !Util.isOffline(this); + MenuItem offlineMenuItem = drawerList.getMenu().findItem(R.id.drawer_offline); + if(Util.isOffline(this)) { + setDrawerItemVisible(R.id.drawer_home, false); + + if(lastSelectedPosition == 0 || lastSelectedPosition == R.id.drawer_home) { + String newFragment = Util.openToTab(this); + if(newFragment == null || "Home".equals(newFragment)) { + newFragment = "Artist"; + } + + lastSelectedPosition = getDrawerItemId(newFragment); + drawerItemSelected(newFragment); + } + + offlineMenuItem.setTitle(R.string.main_online); + } else { + offlineMenuItem.setTitle(R.string.main_offline); + } + if(!podcastsEnabled) { setDrawerItemVisible(R.id.drawer_podcasts, false); } @@ -960,6 +987,127 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte } } + public void toggleOffline() { + boolean isOffline = Util.isOffline(this); + Util.setOffline(this, !isOffline); + invalidate(); + DownloadService service = getDownloadService(); + if (service != null) { + service.setOnline(isOffline); + } + + // Coming back online + if(isOffline) { + int scrobblesCount = Util.offlineScrobblesCount(this); + int starsCount = Util.offlineStarsCount(this); + if(scrobblesCount > 0 || starsCount > 0){ + showOfflineSyncDialog(scrobblesCount, starsCount); + } + } + + UserUtil.seedCurrentUser(this); + this.updateDrawerHeader(); + } + + private void showOfflineSyncDialog(final int scrobbleCount, final int starsCount) { + String syncDefault = Util.getSyncDefault(this); + if(syncDefault != null) { + if("sync".equals(syncDefault)) { + syncOffline(scrobbleCount, starsCount); + return; + } else if("delete".equals(syncDefault)) { + deleteOffline(); + return; + } + } + + View checkBoxView = this.getLayoutInflater().inflate(R.layout.sync_dialog, null); + final CheckBox checkBox = (CheckBox)checkBoxView.findViewById(R.id.sync_default); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setIcon(android.R.drawable.ic_dialog_info) + .setTitle(R.string.offline_sync_dialog_title) + .setMessage(this.getResources().getString(R.string.offline_sync_dialog_message, scrobbleCount, starsCount)) + .setView(checkBoxView) + .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + if(checkBox.isChecked()) { + Util.setSyncDefault(SubsonicActivity.this, "sync"); + } + syncOffline(scrobbleCount, starsCount); + } + }).setNeutralButton(R.string.common_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + dialogInterface.dismiss(); + } + }).setNegativeButton(R.string.common_delete, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + if (checkBox.isChecked()) { + Util.setSyncDefault(SubsonicActivity.this, "delete"); + } + deleteOffline(); + } + }); + + builder.create().show(); + } + + private void syncOffline(final int scrobbleCount, final int starsCount) { + new SilentBackgroundTask(this) { + @Override + protected Integer doInBackground() throws Throwable { + MusicService musicService = MusicServiceFactory.getMusicService(SubsonicActivity.this); + return musicService.processOfflineSyncs(SubsonicActivity.this, null); + } + + @Override + protected void done(Integer result) { + if(result == scrobbleCount) { + Util.toast(SubsonicActivity.this, getResources().getString(R.string.offline_sync_success, result)); + } else { + Util.toast(SubsonicActivity.this, getResources().getString(R.string.offline_sync_partial, result, scrobbleCount + starsCount)); + } + } + + @Override + protected void error(Throwable error) { + Log.w(TAG, "Failed to sync offline stats", error); + String msg = getResources().getString(R.string.offline_sync_error) + " " + getErrorMessage(error); + Util.toast(SubsonicActivity.this, msg); + } + }.execute(); + } + private void deleteOffline() { + SharedPreferences.Editor offline = Util.getOfflineSync(this).edit(); + offline.putInt(Constants.OFFLINE_SCROBBLE_COUNT, 0); + offline.putInt(Constants.OFFLINE_STAR_COUNT, 0); + offline.commit(); + } + + public int getDrawerItemId(String fragmentType) { + switch(fragmentType) { + case "Home": + return R.id.drawer_home; + case "Artist": + return R.id.drawer_library; + case "Playlist": + return R.id.drawer_playlists; + case "Podcast": + return R.id.drawer_podcasts; + case "Bookmark": + return R.id.drawer_bookmarks; + case "Share": + return R.id.drawer_shares; + case "Chat": + return R.id.drawer_chat; + default: + return R.id.drawer_home; + } + } + private void setUncaughtExceptionHandler() { Thread.UncaughtExceptionHandler handler = Thread.getDefaultUncaughtExceptionHandler(); if (!(handler instanceof SubsonicActivity.SubsonicUncaughtExceptionHandler)) { diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java index 778c4982..a007226d 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java @@ -143,30 +143,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo fragmentType = Util.openToTab(this); if(fragmentType != null) { getIntent().putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, fragmentType); - - switch(fragmentType) { - case "Home": - lastSelectedPosition = R.id.drawer_home; - break; - case "Artist": - lastSelectedPosition = R.id.drawer_library; - break; - case "Playlist": - lastSelectedPosition = R.id.drawer_playlists; - break; - case "Podcast": - lastSelectedPosition = R.id.drawer_podcasts; - break; - case "Bookmark": - lastSelectedPosition = R.id.drawer_bookmarks; - break; - case "Share": - lastSelectedPosition = R.id.drawer_shares; - break; - case "Chat": - lastSelectedPosition = R.id.drawer_chat; - break; - } + lastSelectedPosition = getDrawerItemId(fragmentType); } else { lastSelectedPosition = R.id.drawer_home; } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java index d25315ac..0c072864 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java @@ -100,38 +100,28 @@ public class MainFragment extends SelectRecyclerFragment { List> sections = new ArrayList<>(); List headers = new ArrayList<>(); - if (!Util.isOffline(context)) { - List offline = Arrays.asList(R.string.main_offline); - sections.add(offline); - headers.add(null); - - List albums = new ArrayList<>(); - albums.add(R.string.main_albums_newest); - albums.add(R.string.main_albums_random); - if(ServerInfo.checkServerVersion(context, "1.8")) { - albums.add(R.string.main_albums_alphabetical); - } - if(!Util.isTagBrowsing(context)) { - albums.add(R.string.main_albums_highest); - } - albums.add(R.string.main_albums_starred); - albums.add(R.string.main_albums_genres); - albums.add(R.string.main_albums_year); - albums.add(R.string.main_albums_recent); + List albums = new ArrayList<>(); + albums.add(R.string.main_albums_newest); + albums.add(R.string.main_albums_random); + if(ServerInfo.checkServerVersion(context, "1.8")) { + albums.add(R.string.main_albums_alphabetical); + } + if(!Util.isTagBrowsing(context)) { albums.add(R.string.main_albums_highest); - - sections.add(albums); - headers.add("albums"); - - if(ServerInfo.checkServerVersion(context, "1.8") && !Util.isTagBrowsing(context)) { - List videos = Arrays.asList(R.string.main_videos); - sections.add(videos); - headers.add("videos"); - } - } else { - List online = Arrays.asList(R.string.main_online); - sections.add(online); - headers.add(null); + } + albums.add(R.string.main_albums_starred); + albums.add(R.string.main_albums_genres); + albums.add(R.string.main_albums_year); + albums.add(R.string.main_albums_recent); + albums.add(R.string.main_albums_highest); + + sections.add(albums); + headers.add("albums"); + + if(ServerInfo.checkServerVersion(context, "1.8") && !Util.isTagBrowsing(context)) { + List videos = Arrays.asList(R.string.main_videos); + sections.add(videos); + headers.add("videos"); } return new MainAdapter(context, headers, sections, this); @@ -147,28 +137,6 @@ public class MainFragment extends SelectRecyclerFragment { return R.string.common_appname; } - private void toggleOffline() { - boolean isOffline = Util.isOffline(context); - Util.setOffline(context, !isOffline); - context.invalidate(); - DownloadService service = getDownloadService(); - if (service != null) { - service.setOnline(isOffline); - } - - // Coming back online - if(isOffline) { - int scrobblesCount = Util.offlineScrobblesCount(context); - int starsCount = Util.offlineStarsCount(context); - if(scrobblesCount > 0 || starsCount > 0){ - showOfflineSyncDialog(scrobblesCount, starsCount); - } - } - - UserUtil.seedCurrentUser(context); - context.updateDrawerHeader(); - } - private void showAlbumList(String type) { if("genres".equals(type)) { SubsonicFragment fragment = new SelectGenreFragment(); @@ -198,84 +166,6 @@ public class MainFragment extends SelectRecyclerFragment { SubsonicFragment fragment = new SelectVideoFragment(); replaceFragment(fragment); } - - private void showOfflineSyncDialog(final int scrobbleCount, final int starsCount) { - String syncDefault = Util.getSyncDefault(context); - if(syncDefault != null) { - if("sync".equals(syncDefault)) { - syncOffline(scrobbleCount, starsCount); - return; - } else if("delete".equals(syncDefault)) { - deleteOffline(); - return; - } - } - - View checkBoxView = context.getLayoutInflater().inflate(R.layout.sync_dialog, null); - final CheckBox checkBox = (CheckBox)checkBoxView.findViewById(R.id.sync_default); - - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setIcon(android.R.drawable.ic_dialog_info) - .setTitle(R.string.offline_sync_dialog_title) - .setMessage(context.getResources().getString(R.string.offline_sync_dialog_message, scrobbleCount, starsCount)) - .setView(checkBoxView) - .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - if(checkBox.isChecked()) { - Util.setSyncDefault(context, "sync"); - } - syncOffline(scrobbleCount, starsCount); - } - }).setNeutralButton(R.string.common_cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - dialogInterface.dismiss(); - } - }).setNegativeButton(R.string.common_delete, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - if(checkBox.isChecked()) { - Util.setSyncDefault(context, "delete"); - } - deleteOffline(); - } - }); - - builder.create().show(); - } - - private void syncOffline(final int scrobbleCount, final int starsCount) { - new SilentBackgroundTask(context) { - @Override - protected Integer doInBackground() throws Throwable { - MusicService musicService = MusicServiceFactory.getMusicService(context); - return musicService.processOfflineSyncs(context, null); - } - - @Override - protected void done(Integer result) { - if(result == scrobbleCount) { - Util.toast(context, context.getResources().getString(R.string.offline_sync_success, result)); - } else { - Util.toast(context, context.getResources().getString(R.string.offline_sync_partial, result, scrobbleCount + starsCount)); - } - } - - @Override - protected void error(Throwable error) { - Log.w(TAG, "Failed to sync offline stats", error); - String msg = context.getResources().getString(R.string.offline_sync_error) + " " + getErrorMessage(error); - Util.toast(context, msg); - } - }.execute(); - } - private void deleteOffline() { - SharedPreferences.Editor offline = Util.getOfflineSync(context).edit(); - offline.putInt(Constants.OFFLINE_SCROBBLE_COUNT, 0); - offline.putInt(Constants.OFFLINE_STAR_COUNT, 0); - offline.commit(); - } private void showAboutDialog() { new LoadingTask(context) { @@ -405,9 +295,7 @@ public class MainFragment extends SelectRecyclerFragment { @Override public void onItemClicked(Integer item) { - if(item == R.string.main_offline || item == R.string.main_online) { - toggleOffline(); - } else if (item == R.string.main_albums_newest) { + if (item == R.string.main_albums_newest) { showAlbumList("newest"); } else if (item == R.string.main_albums_random) { showAlbumList("random"); -- cgit v1.2.3