Fixed dir settings

This commit is contained in:
David 2016-04-25 08:07:45 -03:00
parent 0156a4f39e
commit 6940021293
6 changed files with 50 additions and 120 deletions

View File

@ -28,6 +28,8 @@ import android.support.annotation.NonNull;
import java.io.File; import java.io.File;
import us.shandian.giga.util.Utility;
/** /**
* Helper for global settings * Helper for global settings
*/ */
@ -46,10 +48,34 @@ public class NewPipeSettings {
return getFolder(context, R.string.download_path_key, Environment.DIRECTORY_MOVIES); return getFolder(context, R.string.download_path_key, Environment.DIRECTORY_MOVIES);
} }
public static String getVideoDownloadPath(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String key = context.getString(R.string.download_path_key);
String downloadPath = prefs.getString(key, Environment.DIRECTORY_MOVIES);
return downloadPath;
}
public static File getAudioDownloadFolder(Context context) { public static File getAudioDownloadFolder(Context context) {
return getFolder(context, R.string.download_path_audio_key, Environment.DIRECTORY_MUSIC); return getFolder(context, R.string.download_path_audio_key, Environment.DIRECTORY_MUSIC);
} }
public static String getAudioDownloadPath(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String key = context.getString(R.string.download_path_audio_key);
String downloadPath = prefs.getString(key, Environment.DIRECTORY_MUSIC);
return downloadPath;
}
public static String getDownloadPath(Context context, String fileName)
{
if(Utility.isVideoFile(fileName)) {
return NewPipeSettings.getVideoDownloadPath(context);
}
return NewPipeSettings.getAudioDownloadPath(context);
}
private static File getFolder(Context context, int keyID, String defaultDirectoryName) { private static File getFolder(Context context, int keyID, String defaultDirectoryName) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String key = context.getString(keyID); final String key = context.getString(keyID);

View File

@ -28,7 +28,6 @@ import android.widget.Toast;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.VideoItemDetailActivity; import org.schabi.newpipe.VideoItemDetailActivity;
import org.schabi.newpipe.VideoItemListActivity;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -139,7 +138,6 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
.replace(R.id.frame, mFragment) .replace(R.id.frame, mFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit(); .commit();
} }
private void showUrlDialog() { private void showUrlDialog() {
@ -297,7 +295,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
NavUtils.navigateUpTo(this, intent); NavUtils.navigateUpTo(this, intent);
return true; return true;
} else { } else {
return false; return mFragment.onOptionsItemSelected(item) ||
super.onOptionsItemSelected(item);
} }
} }

View File

@ -5,6 +5,8 @@ import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.schabi.newpipe.NewPipeSettings;
import java.io.File; import java.io.File;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -33,7 +35,7 @@ public class DownloadManagerImpl implements DownloadManager
DownloadMission mission = new DownloadMission(); DownloadMission mission = new DownloadMission();
mission.url = url; mission.url = url;
mission.name = name; mission.name = name;
mission.location = mLocation; mission.location = NewPipeSettings.getDownloadPath(mContext, name);
mission.timestamp = System.currentTimeMillis(); mission.timestamp = System.currentTimeMillis();
mission.threadCount = threads; mission.threadCount = threads;
new Initializer(mContext, mission).start(); new Initializer(mContext, mission).start();

View File

@ -14,12 +14,12 @@ import android.os.Message;
import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log; import android.util.Log;
import org.schabi.newpipe.NewPipeSettings;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import us.shandian.giga.get.DownloadManager; import us.shandian.giga.get.DownloadManager;
import us.shandian.giga.get.DownloadManagerImpl; import us.shandian.giga.get.DownloadManagerImpl;
import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.DownloadMission;
import org.schabi.newpipe.download.MainActivity; import org.schabi.newpipe.download.MainActivity;
import us.shandian.giga.util.Settings;
import static org.schabi.newpipe.BuildConfig.DEBUG; import static org.schabi.newpipe.BuildConfig.DEBUG;
public class DownloadManagerService extends Service implements DownloadMission.MissionListener public class DownloadManagerService extends Service implements DownloadMission.MissionListener
@ -43,7 +43,7 @@ public class DownloadManagerService extends Service implements DownloadMission.M
mBinder = new DMBinder(); mBinder = new DMBinder();
if (mManager == null) { if (mManager == null) {
String path = Settings.getInstance(this).getString(Settings.DOWNLOAD_DIRECTORY, Settings.DEFAULT_PATH); String path = NewPipeSettings.getVideoDownloadPath(this);
mManager = new DownloadManagerImpl(this, path); mManager = new DownloadManagerImpl(this, path);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "mManager == null"); Log.d(TAG, "mManager == null");

View File

@ -1,60 +0,0 @@
package us.shandian.giga.util;
import android.content.Context;
import android.content.SharedPreferences;
/*
Settings Provider
*/
public class Settings
{
public static final String XML_NAME = "settings";
public static final String DOWNLOAD_DIRECTORY = "download_directory";
public static final String DEFAULT_PATH = "/storage/sdcard0/GigaGet";
private static Settings sInstance;
private SharedPreferences mPrefs;
public static Settings getInstance(Context context) {
if (sInstance == null) {
sInstance = new Settings(context);
}
return sInstance;
}
private Settings(Context context) {
mPrefs = context.getSharedPreferences(XML_NAME, Context.MODE_PRIVATE);
}
public Settings putBoolean(String key, boolean value) {
mPrefs.edit().putBoolean(key, value).commit();
return this;
}
public boolean getBoolean(String key, boolean def) {
return mPrefs.getBoolean(key, def);
}
public Settings putInt(String key, int value) {
mPrefs.edit().putInt(key, value).commit();
return this;
}
public int getInt(String key, int defValue) {
return mPrefs.getInt(key, defValue);
}
public Settings putString(String key, String value) {
mPrefs.edit().putString(key, value).commit();
return this;
}
public String getString(String key, String defValue) {
return mPrefs.getString(key, defValue);
}
}

View File

@ -17,9 +17,9 @@ import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import org.schabi.newpipe.NewPipeSettings;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.DownloadMission;
import us.shandian.giga.util.Settings;
import com.nononsenseapps.filepicker.FilePickerActivity; import com.nononsenseapps.filepicker.FilePickerActivity;
import com.nononsenseapps.filepicker.AbstractFilePickerFragment; import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
@ -154,10 +154,10 @@ public class Utility
public static FileType getFileType(String file) { public static FileType getFileType(String file) {
if (file.endsWith(".apk")) { if (file.endsWith(".apk")) {
return FileType.APP; return FileType.APP;
} else if (file.endsWith(".mp3") || file.endsWith(".wav") || file.endsWith(".flac")) { } else if (file.endsWith(".mp3") || file.endsWith(".wav") || file.endsWith(".flac") || file.endsWith(".m4a")) {
return FileType.MUSIC; return FileType.MUSIC;
} else if (file.endsWith(".mp4") || file.endsWith(".mpeg") || file.endsWith(".rm") || file.endsWith(".rmvb") } else if (file.endsWith(".mp4") || file.endsWith(".mpeg") || file.endsWith(".rm") || file.endsWith(".rmvb")
|| file.endsWith(".flv") || file.endsWith(".webp")) { || file.endsWith(".flv") || file.endsWith(".webp") || file.endsWith(".webm")) {
return FileType.VIDEO; return FileType.VIDEO;
} else if (file.endsWith(".doc") || file.endsWith(".docx")) { } else if (file.endsWith(".doc") || file.endsWith(".docx")) {
return FileType.WORD; return FileType.WORD;
@ -173,6 +173,16 @@ public class Utility
} }
} }
public static Boolean isMusicFile(String file)
{
return Utility.getFileType(file) == FileType.MUSIC;
}
public static Boolean isVideoFile(String file)
{
return Utility.getFileType(file) == FileType.VIDEO;
}
public static int getBackgroundForFileType(FileType type) { public static int getBackgroundForFileType(FileType type) {
switch (type) { switch (type) {
case APP: case APP:
@ -213,60 +223,13 @@ public class Utility
} }
} }
public static int getThemeForFileType(FileType type) {
/*switch (type) {
case APP:
return R.style.Theme_App_Orange;
case MUSIC:
return R.style.Theme_App_Cyan;
case ARCHIVE:
return R.style.Theme_App_Blue;
case VIDEO:
return R.style.Theme_App_Green;
case WORD:
case EXCEL:
case POWERPOINT:
return R.style.Theme_App_Brown;
case UNKNOWN:
default:
return R.style.Theme_App_BlueGray;
}*/
return 0;
}
public static int getIconForFileType(FileType type) {
switch (type) {
case APP:
return R.drawable.apps;
case MUSIC:
return R.drawable.music;
case ARCHIVE:
return R.drawable.archive;
case VIDEO:
return R.drawable.video;
case WORD:
return R.drawable.word;
case EXCEL:
return R.drawable.excel;
case POWERPOINT:
return R.drawable.powerpoint;
case UNKNOWN:
default:
return R.drawable.unknown;
}
}
public static boolean isDirectoryAvailble(String path) { public static boolean isDirectoryAvailble(String path) {
File dir = new File(path); File dir = new File(path);
return dir.exists() && dir.isDirectory(); return dir.exists() && dir.isDirectory();
} }
public static boolean isDownloadDirectoryAvailble(Context context) { public static boolean isDownloadDirectoryAvailble(Context context) {
return isDirectoryAvailble(Settings.getInstance(context).getString(Settings.DOWNLOAD_DIRECTORY, Settings.DEFAULT_PATH)); return isDirectoryAvailble(NewPipeSettings.getVideoDownloadPath(context));
}
public static void changeDownloadDirectory(Context context, String path) {
Settings.getInstance(context).putString(Settings.DOWNLOAD_DIRECTORY, path);
} }
public static void showDirectoryChooser(Activity activity) { public static void showDirectoryChooser(Activity activity) {