From 5ac0c33e682709c0c079f47da271f837b4edfe9b Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 29 Mar 2013 20:27:22 -0700 Subject: Added menu to MainFragment, start of abstracted list of common functions --- .../daneren2005/dsub/activity/MainActivity.java | 44 ++++---- .../dsub/fragments/LibraryFunctionsFragment.java | 62 +++++++++++ .../daneren2005/dsub/fragments/MainFragment.java | 119 ++++++++++++++++++++- .../dsub/fragments/SubsonicTabFragment.java | 14 ++- 4 files changed, 212 insertions(+), 27 deletions(-) create mode 100644 subsonic-android/src/github/daneren2005/dsub/fragments/LibraryFunctionsFragment.java diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java index a53be72a..50fea96e 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java @@ -30,12 +30,12 @@ public class MainActivity extends SubsonicActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); - + viewPager = (ViewPager) findViewById(R.id.pager); pagerAdapter = new MainActivityPagerAdapter(this, viewPager); viewPager.setAdapter(pagerAdapter); viewPager.setOnPageChangeListener(pagerAdapter); - + addTab("Home", MainFragment.class, null); addTab("Library", SelectArtistFragment.class, null); addTab("Playlists", SelectPlaylistFragment.class, null); @@ -96,34 +96,34 @@ public class MainActivity extends SubsonicActivity { @Override public Fragment getItem(int i) { final TabInfo tabInfo = (TabInfo)tabs.get(i); - return (Fragment) Fragment.instantiate(activity, tabInfo.fragmentClass.getName(), tabInfo.args ); + return (Fragment) Fragment.instantiate(activity, tabInfo.fragmentClass.getName(), tabInfo.args ); } @Override public int getCount() { return tabs.size(); } - + public void onTabSelected(Tab tab, FragmentTransaction ft) { - TabInfo tabInfo = (TabInfo) tab.getTag(); - for (int i = 0; i < tabs.size(); i++) { - if ( tabs.get(i) == tabInfo ) { - pager.setCurrentItem(i); - } - } - } - - public void onTabUnselected(Tab tab, FragmentTransaction ft) {} - - public void onTabReselected(Tab tab, FragmentTransaction ft) {} - + TabInfo tabInfo = (TabInfo) tab.getTag(); + for (int i = 0; i < tabs.size(); i++) { + if ( tabs.get(i) == tabInfo ) { + pager.setCurrentItem(i); + } + } + } + + public void onTabUnselected(Tab tab, FragmentTransaction ft) {} + + public void onTabReselected(Tab tab, FragmentTransaction ft) {} + public void onPageScrollStateChanged(int arg0) {} - - public void onPageScrolled(int arg0, float arg1, int arg2) {} - - public void onPageSelected(int position) { - actionBar.setSelectedNavigationItem(position); - } + + public void onPageScrolled(int arg0, float arg1, int arg2) {} + + public void onPageSelected(int position) { + actionBar.setSelectedNavigationItem(position); + } public void addTab(CharSequence title, Class fragmentClass, Bundle args) { final TabInfo tabInfo = new TabInfo(fragmentClass, args); diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/LibraryFunctionsFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/LibraryFunctionsFragment.java new file mode 100644 index 00000000..e21f164e --- /dev/null +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/LibraryFunctionsFragment.java @@ -0,0 +1,62 @@ +package github.daneren2005.dsub.fragments; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.view.View; +import android.widget.EditText; +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.activity.DownloadActivity; +import github.daneren2005.dsub.util.Constants; +import github.daneren2005.dsub.util.Util; + +public class LibraryFunctionsFragment extends SubsonicTabFragment { + protected void onShuffleRequested() { + if(Util.isOffline(context)) { + Intent intent = new Intent(context, DownloadActivity.class); + intent.putExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, true); + Util.startActivityWithoutTransition(context, intent); + return; + } + + View dialogView = context.getLayoutInflater().inflate(R.layout.shuffle_dialog, null); + final EditText startYearBox = (EditText)dialogView.findViewById(R.id.start_year); + final EditText endYearBox = (EditText)dialogView.findViewById(R.id.end_year); + final EditText genreBox = (EditText)dialogView.findViewById(R.id.genre); + + final SharedPreferences prefs = Util.getPreferences(context); + final String oldStartYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, ""); + final String oldEndYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, ""); + final String oldGenre = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, ""); + + startYearBox.setText(oldStartYear); + endYearBox.setText(oldEndYear); + genreBox.setText(oldGenre); + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle("Shuffle By") + .setView(dialogView) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + Intent intent = new Intent(context, DownloadActivity.class); + intent.putExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, true); + String genre = genreBox.getText().toString(); + String startYear = startYearBox.getText().toString(); + String endYear = endYearBox.getText().toString(); + + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYear); + editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYear); + editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre); + editor.commit(); + + Util.startActivityWithoutTransition(context, intent); + } + }) + .setNegativeButton("Cancel", null); + AlertDialog dialog = builder.create(); + dialog.show(); + } +} diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java index 6b753a22..6f920896 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java @@ -1,8 +1,11 @@ package github.daneren2005.dsub.fragments; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; +import android.os.StatFs; import android.preference.PreferenceManager; import android.support.v4.app.FragmentTransaction; import android.view.ContextMenu; @@ -18,9 +21,19 @@ import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.MergeAdapter; import github.daneren2005.dsub.util.Util; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuItem; +import com.actionbarsherlock.view.MenuInflater; +import github.daneren2005.dsub.activity.HelpActivity; +import github.daneren2005.dsub.activity.MainActivity; +import github.daneren2005.dsub.activity.SettingsActivity; +import github.daneren2005.dsub.util.ModalBackgroundTask; +import java.io.File; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; -public class MainFragment extends SubsonicTabFragment { +public class MainFragment extends LibraryFunctionsFragment { private LayoutInflater inflater; private static final int MENU_GROUP_SERVER = 10; @@ -54,6 +67,41 @@ public class MainFragment extends SubsonicTabFragment { super.onDestroy(); } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { + menuInflater.inflate(R.menu.main, menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + Intent intent; + switch (item.getItemId()) { + case R.id.menu_shuffle: + onShuffleRequested(); + return true; + case R.id.menu_search: + context.onSearchRequested(); + return true; + case R.id.menu_exit: + exit(); + return true; + case R.id.menu_settings: + startActivity(new Intent(context, SettingsActivity.class)); + return true; + case R.id.menu_help: + startActivity(new Intent(context, HelpActivity.class)); + return true; + case R.id.menu_log: + getLogs(); + return true; + case R.id.menu_about: + showAboutDialog(); + return true; + } + + return false; + } + @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); @@ -192,7 +240,7 @@ public class MainFragment extends SubsonicTabFragment { Util.setOffline(context, !Util.isOffline(context)); refresh(); } - + private void showAlbumList(String type) { SubsonicTabFragment fragment = new SelectDirectoryFragment(); Bundle args = new Bundle(); @@ -206,4 +254,71 @@ public class MainFragment extends SubsonicTabFragment { trans.addToBackStack(null); trans.commit(); } + + private void showAboutDialog() { + try { + File rootFolder = FileUtil.getMusicDirectory(context); + StatFs stat = new StatFs(rootFolder.getPath()); + long bytesTotalFs = (long) stat.getBlockCount() * (long) stat.getBlockSize(); + long bytesAvailableFs = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); + + String msg = getResources().getString(R.string.main_about_text, + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName, + Util.formatBytes(FileUtil.getUsedSize(context, rootFolder)), + Util.formatBytes(Util.getCacheSizeMB(context) * 1024L * 1024L), + Util.formatBytes(bytesAvailableFs), + Util.formatBytes(bytesTotalFs)); + Util.info(context, R.string.main_about_title, msg); + } catch(Exception e) { + Util.toast(context, "Failed to open dialog"); + } + } + + private void getLogs() { + try { + final String version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; + new ModalBackgroundTask(context, false) { + @Override + protected File doInBackground() throws Throwable { + updateProgress("Gathering Logs"); + File logcat = new File(FileUtil.getSubsonicDirectory(), "logcat.txt"); + Process logcatProc = null; + + try { + List progs = new ArrayList(); + progs.add("logcat"); + progs.add("-v"); + progs.add("time"); + progs.add("-d"); + progs.add("-f"); + progs.add(logcat.getPath()); + progs.add("*:I"); + + logcatProc = Runtime.getRuntime().exec(progs.toArray(new String[0])); + logcatProc.waitFor(); + } catch(Exception e) { + Util.toast(context, "Failed to gather logs"); + } finally { + if(logcatProc != null) { + logcatProc.destroy(); + } + } + + return logcat; + } + + @Override + protected void done(File logcat) { + Intent email = new Intent(android.content.Intent.ACTION_SEND); + email.setType("text/plain"); + email.putExtra(Intent.EXTRA_EMAIL, new String[] {"daneren2005@gmail.com"}); + email.putExtra(Intent.EXTRA_SUBJECT, "DSub " + version + " Error Logs"); + email.putExtra(Intent.EXTRA_TEXT, "Describe the problem here"); + Uri attachment = Uri.fromFile(logcat); + email.putExtra(Intent.EXTRA_STREAM, attachment); + startActivity(email); + } + }.execute(); + } catch(Exception e) {} + } } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java index 9b79fd19..ee608378 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java @@ -20,6 +20,7 @@ package github.daneren2005.dsub.fragments; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -29,6 +30,7 @@ import github.daneren2005.dsub.R; import github.daneren2005.dsub.activity.SubsonicActivity; import github.daneren2005.dsub.activity.SubsonicTabActivity; import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.service.DownloadServiceImpl; import github.daneren2005.dsub.util.ImageLoader; public class SubsonicTabFragment extends SherlockFragment { @@ -39,6 +41,7 @@ public class SubsonicTabFragment extends SherlockFragment { @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); + setHasOptionsMenu(true); } @Override @@ -65,6 +68,11 @@ public class SubsonicTabFragment extends SherlockFragment { } + protected void exit() { + context.stopService(new Intent(context, DownloadServiceImpl.class)); + context.finish(); + } + public void setProgressVisible(boolean visible) { View view = rootView.findViewById(R.id.tab_progress); if (view != null) { @@ -78,10 +86,10 @@ public class SubsonicTabFragment extends SherlockFragment { view.setText(message); } } - + protected synchronized ImageLoader getImageLoader() { - return context.getImageLoader(); - } + return context.getImageLoader(); + } public synchronized static ImageLoader getStaticImageLoader(Context context) { return SubsonicActivity.getStaticImageLoader(context); } -- cgit v1.2.3