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 us.shandian.giga.util.Utility;
/**
* Helper for global settings
*/
@ -46,10 +48,34 @@ public class NewPipeSettings {
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) {
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) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
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.VideoItemDetailActivity;
import org.schabi.newpipe.VideoItemListActivity;
import java.io.File;
import java.io.IOException;
@ -139,7 +138,6 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
.replace(R.id.frame, mFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
private void showUrlDialog() {
@ -297,7 +295,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
NavUtils.navigateUpTo(this, intent);
return true;
} 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 org.schabi.newpipe.NewPipeSettings;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
@ -33,7 +35,7 @@ public class DownloadManagerImpl implements DownloadManager
DownloadMission mission = new DownloadMission();
mission.url = url;
mission.name = name;
mission.location = mLocation;
mission.location = NewPipeSettings.getDownloadPath(mContext, name);
mission.timestamp = System.currentTimeMillis();
mission.threadCount = threads;
new Initializer(mContext, mission).start();
@ -64,7 +66,7 @@ public class DownloadManagerImpl implements DownloadManager
private void loadMissions() {
File f = new File(mLocation);
if (f.exists() && f.isDirectory()) {
File[] subs = f.listFiles();

View file

@ -14,12 +14,12 @@ import android.os.Message;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import org.schabi.newpipe.NewPipeSettings;
import org.schabi.newpipe.R;
import us.shandian.giga.get.DownloadManager;
import us.shandian.giga.get.DownloadManagerImpl;
import us.shandian.giga.get.DownloadMission;
import org.schabi.newpipe.download.MainActivity;
import us.shandian.giga.util.Settings;
import static org.schabi.newpipe.BuildConfig.DEBUG;
public class DownloadManagerService extends Service implements DownloadMission.MissionListener
@ -43,7 +43,7 @@ public class DownloadManagerService extends Service implements DownloadMission.M
mBinder = new DMBinder();
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);
if (DEBUG) {
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.NoSuchAlgorithmException;
import org.schabi.newpipe.NewPipeSettings;
import org.schabi.newpipe.R;
import us.shandian.giga.get.DownloadMission;
import us.shandian.giga.util.Settings;
import com.nononsenseapps.filepicker.FilePickerActivity;
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
@ -154,10 +154,10 @@ public class Utility
public static FileType getFileType(String file) {
if (file.endsWith(".apk")) {
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;
} 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;
} else if (file.endsWith(".doc") || file.endsWith(".docx")) {
return FileType.WORD;
@ -172,6 +172,16 @@ public class Utility
return FileType.UNKNOWN;
}
}
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) {
switch (type) {
@ -212,63 +222,16 @@ public class Utility
return R.color.bluegray_dark;
}
}
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) {
File dir = new File(path);
return dir.exists() && dir.isDirectory();
}
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) {
Intent i = new Intent(activity, FilePickerActivity.class);
i.setAction(Intent.ACTION_GET_CONTENT);