Add download settings

This commit is contained in:
seven332 committed 2015-07-28 22:31:05 +08:00
1 parent 93396f4181
commit 74c1c2144f
13 files changed
+730 -10

No files matched your search

@@ -29,6 +29,7 @@ public final class AppConfig {
private static final String APP_DIRNAME = "EhViewer-Test";
private static final String CRASH_DIRNAME = "crash";
private static final String DOWNLOAD_DIRNAME = "download";
private static final String ARCHIVE_DIRNAME = "archive";
private static final String TORRENT_DIRNAME = "torrent";
public static @Nullable
@@ -58,6 +59,10 @@ public final class AppConfig {
return getFileInAppDir(DOWNLOAD_DIRNAME);
}
public static @Nullable File getArchiveDir() {
return getFileInAppDir(ARCHIVE_DIRNAME);
}
public static @Nullable File getTorrentDir() {
return getFileInAppDir(TORRENT_DIRNAME);
}
@@ -23,6 +23,7 @@ import com.hippo.ehviewer.R;
import com.hippo.ehviewer.ui.ContentActivity;
import com.hippo.ehviewer.ui.scene.preference.AdvanceSettingsScene;
import com.hippo.ehviewer.ui.scene.preference.DisplaySettingsScene;
import com.hippo.ehviewer.ui.scene.preference.DownloadSettingsScene;
import com.hippo.ehviewer.ui.scene.preference.EHSettingsScene;
import com.hippo.scene.preference.PreferenceHeader;
import com.hippo.scene.preference.PreferenceHeaderScene;
@@ -51,6 +52,10 @@ public final class MainSettingsScene extends PreferenceHeaderScene {
R.drawable.ic_eh_theme_accent,
R.string.settings_eh,
EHSettingsScene.class),
newPreferenceHeader(
R.drawable.ic_download_theme_accent,
R.string.settings_download,
DownloadSettingsScene.class),
newPreferenceHeader(
R.drawable.ic_black_mesa_theme_accent,
R.string.settings_advance,
@@ -0,0 +1,175 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.scene.preference;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.util.Settings;
import com.hippo.rippleold.RippleSalon;
import com.hippo.scene.Announcer;
import com.hippo.scene.AppbarScene;
import com.hippo.scene.Scene;
import com.hippo.scene.SimpleDialog;
import com.hippo.unifile.UniFile;
import com.hippo.widget.DirExplorer;
import com.hippo.yorozuya.ViewUtils;
import java.io.File;
public class DirPickScene extends AppbarScene implements View.OnClickListener,
Scene.ActivityResultListener {
public static final String ACTION_IMAGE_DOWNLOAD_LOCATION = "com.hippo.ehviewer.dirpick.action.IMAGE_DOWNLOAD_LOCATION";
public static final String ACTION_ARCHIVE_DOWNLOAD_LOCATION = "com.hippo.ehviewer.dirpick.action.ARCHIVE_DOWNLOAD_LOCATION";
private String mAction;
private View mDocument;
private View mDocumentHelp;
private View mOk;
private DirExplorer mDirExplorer;
private int mDirExplorerOriginalPaddingBottom;
private File getCurrentFile() {
UniFile dir = null;
if (ACTION_IMAGE_DOWNLOAD_LOCATION.equals(mAction)) {
dir = Settings.getImageDownloadLocation();
} else if (ACTION_ARCHIVE_DOWNLOAD_LOCATION.equals(mAction)) {
dir = Settings.getArchiveDownloadLocation();
}
if (dir != null) {
Uri uri = dir.getUri();
if (UniFile.isFileUri(uri)) {
return new File(uri.getPath());
}
}
return null;
}
@Override
protected void onCreate(boolean rebirth) {
super.onCreate(rebirth);
setContentView(R.layout.scene_dir_pick);
setTitle(R.string.dir_pick);
setIcon(R.drawable.ic_arrow_left_dark);
Announcer announcer = getAnnouncer();
if (announcer != null) {
mAction = announcer.getAction();
}
ViewGroup documentGroup = (ViewGroup) findViewById(R.id.document_group);
mDocument = documentGroup.findViewById(R.id.document);
mDocumentHelp = documentGroup.findViewById(R.id.document_help);
mOk = findViewById(R.id.ok);
mDirExplorer = (DirExplorer) findViewById(R.id.dir_explorer);
mDirExplorerOriginalPaddingBottom = mDirExplorer.getPaddingBottom();
mDirExplorer.setHasFixedSize(true);
mDirExplorer.setClipToPadding(false);
File file = getCurrentFile();
if (file != null) {
mDirExplorer.setCurrentFile(file);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
documentGroup.setVisibility(View.GONE);
}
RippleSalon.addRipple(mDocument, false);
RippleSalon.addRipple(mOk, false);
mDocument.setOnClickListener(this);
mDocumentHelp.setOnClickListener(this);
mOk.setOnClickListener(this);
}
@Override
protected void onGetFitPaddingBottom(int b) {
super.onGetFitPaddingBottom(b);
mDirExplorer.setPadding(mDirExplorer.getPaddingLeft(),
mDirExplorer.getPaddingTop(), mDirExplorer.getPaddingRight(),
mDirExplorerOriginalPaddingBottom + b);
}
@Override
public void onIconClick() {
finish();
}
private void save(UniFile uniFile) {
if (ACTION_IMAGE_DOWNLOAD_LOCATION.equals(mAction)) {
Settings.putImageDownloadLocation(uniFile);
} else if (ACTION_ARCHIVE_DOWNLOAD_LOCATION.equals(mAction)) {
Settings.putArchiveDownloadLocation(uniFile);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
if (v == mDocument) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, this);
} else if (v == mDocumentHelp) {
int[] location = new int[2];
ViewUtils.getCenterInAncestor(mDocumentHelp, location, getSceneView());
new SimpleDialog.Builder(getStageActivity()).setTitle(R.string.document)
.setMessage(R.string.document_help)
.setStartPoint(location[0], location[1])
.show(this);
} else if (v == mOk) {
UniFile dir = UniFile.fromFile(mDirExplorer.getCurrentFile());
if (dir == null) {
Toast.makeText(getStageActivity(), R.string.em_cant_get_folder, Toast.LENGTH_SHORT).show();
} else if (!dir.canWrite()) {
Toast.makeText(getStageActivity(), R.string.em_cant_write_folder, Toast.LENGTH_SHORT).show();
} else {
save(dir);
finish();
}
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onGetResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri treeUri = data.getData();
getStageActivity().getContentResolver().takePersistableUriPermission(
treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
UniFile dir = UniFile.fromTreeUri(getStageActivity(), treeUri);
if (dir != null) {
save(dir);
finish();
} else {
Toast.makeText(getStageActivity(), R.string.em_cant_parse_document_uri, Toast.LENGTH_SHORT).show();
}
}
}
}
@@ -0,0 +1,104 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.scene.preference;
import android.content.res.Resources;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.util.Settings;
import com.hippo.scene.Announcer;
import com.hippo.scene.preference.Preference;
import com.hippo.scene.preference.PreferenceScene;
import com.hippo.scene.preference.PreferenceSet;
import com.hippo.unifile.UniFile;
public class DownloadSettingsScene extends PreferenceScene implements Preference.OnClickListener {
private static final String KEY_IMAGE_DOWNLOAD_LOACTION = "image_download_location";
private static final String KEY_ARCHIVE_DOWNLOAD_LOACTION = "archive_download_location";
@Override
protected void onCreate(boolean rebirth) {
super.onCreate(rebirth);
setTitle(R.string.settings_download);
setIcon(R.drawable.ic_arrow_left_dark);
setPreferenceSet(getPreferenceSet());
}
@Override
public void onIconClick() {
finish();
}
private PreferenceSet[] getPreferenceSet() {
Resources resources = getStageActivity().getResources();
String imageSummary;
UniFile imageDir = Settings.getImageDownloadLocation();
if (imageDir == null) {
imageSummary = resources.getString(R.string.unknown);
} else {
imageSummary = imageDir.getUri().getPath();
}
Preference imagePreference = new Preference(
KEY_IMAGE_DOWNLOAD_LOACTION,
resources.getString(R.string.settings_image_download_location),
imageSummary);
imagePreference.setOnClickListener(this);
String archiveSummary;
UniFile archiveDir = Settings.getImageDownloadLocation();
if (archiveDir == null) {
archiveSummary = resources.getString(R.string.unknown);
} else {
archiveSummary = archiveDir.getUri().getPath();
}
Preference archivePreference = new Preference(
KEY_ARCHIVE_DOWNLOAD_LOACTION,
resources.getString(R.string.settings_archive_download_location),
archiveSummary);
archivePreference.setOnClickListener(this);
PreferenceSet preferenceSet = new PreferenceSet();
preferenceSet.setPreferenceList(new Preference[] {
imagePreference,
archivePreference
});
return new PreferenceSet[] {
preferenceSet
};
}
@Override
public boolean onClick(Preference preference) {
String key = preference.getKey();
switch (key) {
case KEY_IMAGE_DOWNLOAD_LOACTION:
Announcer announcer = new Announcer();
announcer.setAction(DirPickScene.ACTION_IMAGE_DOWNLOAD_LOCATION);
startScene(DirPickScene.class, announcer);
return true;
default:
return true;
}
}
}
@@ -18,6 +18,7 @@ package com.hippo.ehviewer.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import com.hippo.ehviewer.AppConfig;
@@ -25,16 +26,15 @@ import com.hippo.ehviewer.BuildConfig;
import com.hippo.ehviewer.client.EhConfig;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.unifile.UniFile;
import com.hippo.yorozuya.FileUtils;
import java.io.File;
public final class Settings {
private static Context sContext;
private static SharedPreferences sSettingsPre;
public static void initialize(Context context) {
sSettingsPre = PreferenceManager.getDefaultSharedPreferences(context);
sContext = context.getApplicationContext();
sSettingsPre = PreferenceManager.getDefaultSharedPreferences(sContext);
}
public static boolean getBoolean(String key, boolean defValue) {
@@ -193,11 +193,65 @@ public final class Settings {
return getInt(KEY_PREVIEW_SIZE, DEFAULT_PREVIEW_SIZE);
}
public static final String KEY_IMAGE_LOCATION_SCHEME = "image_scheme";
public static final String KEY_IMAGE_LOCATION_AUTHORITY = "image_authority";
public static final String KEY_IMAGE_LOCATION_PATH = "image_path";
public static final String KEY_IMAGE_LOCATION_QUERY = "image_query";
public static final String KEY_IMAGE_LOCATION_FRAGMENT = "image_fragment";
public static final String KEY_ARCHIVE_LOCATION_SCHEME = "archive_scheme";
public static final String KEY_ARCHIVE_LOCATION_AUTHORITY = "archive_authority";
public static final String KEY_ARCHIVE_LOCATION_PATH = "archive_path";
public static final String KEY_ARCHIVE_LOCATION_QUERY = "archive_query";
public static final String KEY_ARCHIVE_LOCATION_FRAGMENT = "archive_fragment";
public static UniFile getDownloadPath() {
File file = AppConfig.getDownloadDir();
FileUtils.ensureDirectory(file);
return UniFile.fromFile(file);
public static UniFile getImageDownloadLocation() {
UniFile dir = null;
try {
Uri.Builder builder = new Uri.Builder();
builder.scheme(getString(KEY_IMAGE_LOCATION_SCHEME, null));
builder.encodedAuthority(getString(KEY_IMAGE_LOCATION_AUTHORITY, null));
builder.encodedPath(getString(KEY_IMAGE_LOCATION_PATH, null));
builder.encodedQuery(getString(KEY_IMAGE_LOCATION_QUERY, null));
builder.encodedFragment(getString(KEY_IMAGE_LOCATION_FRAGMENT, null));
dir = UniFile.fromUri(sContext, builder.build());
} catch (Exception e) {
e.printStackTrace();
}
return dir != null ? dir : UniFile.fromFile(AppConfig.getDownloadDir());
}
public static void putImageDownloadLocation(UniFile location) {
Uri uri = location.getUri();
putString(KEY_IMAGE_LOCATION_SCHEME, uri.getScheme());
putString(KEY_IMAGE_LOCATION_AUTHORITY, uri.getEncodedAuthority());
putString(KEY_IMAGE_LOCATION_PATH, uri.getEncodedPath());
putString(KEY_IMAGE_LOCATION_QUERY, uri.getEncodedQuery());
putString(KEY_IMAGE_LOCATION_FRAGMENT, uri.getEncodedFragment());
}
public static UniFile getArchiveDownloadLocation() {
UniFile dir = null;
try {
Uri.Builder builder = new Uri.Builder();
builder.scheme(getString(KEY_ARCHIVE_LOCATION_SCHEME, null));
builder.encodedAuthority(getString(KEY_ARCHIVE_LOCATION_AUTHORITY, null));
builder.encodedPath(getString(KEY_ARCHIVE_LOCATION_PATH, null));
builder.encodedQuery(getString(KEY_ARCHIVE_LOCATION_QUERY, null));
builder.encodedFragment(getString(KEY_ARCHIVE_LOCATION_FRAGMENT, null));
dir = UniFile.fromUri(sContext, builder.build());
} catch (Exception e) {
e.printStackTrace();
}
return dir != null ? dir : UniFile.fromFile(AppConfig.getDownloadDir());
}
public static void putArchiveDownloadLocation(UniFile location) {
Uri uri = location.getUri();
putString(KEY_ARCHIVE_LOCATION_SCHEME, uri.getScheme());
putString(KEY_ARCHIVE_LOCATION_AUTHORITY, uri.getEncodedAuthority());
putString(KEY_ARCHIVE_LOCATION_PATH, uri.getEncodedPath());
putString(KEY_ARCHIVE_LOCATION_QUERY, uri.getEncodedQuery());
putString(KEY_ARCHIVE_LOCATION_FRAGMENT, uri.getEncodedFragment());
}
/****** Advance ******/
@@ -0,0 +1,173 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.widget;
import android.content.Context;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.hippo.ehviewer.R;
import com.hippo.widget.recyclerview.EasyRecyclerView;
import com.hippo.widget.recyclerview.LinearDividerItemDecoration;
import com.hippo.yorozuya.LayoutUtils;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DirExplorer extends EasyRecyclerView implements EasyRecyclerView.OnItemClickListener {
private static final DirFilter DIR_FILTER = new DirFilter();
private static final FileSort FILE_SORT = new FileSort();
private static final File PARENT_DIR = null;
private static final String PARENT_DIR_NAME = "..";
private File mCurrentFile;
private List<File> mFiles = new ArrayList<>();
private DirAdapter mAdapter;
public DirExplorer(Context context) {
super(context);
init(context);
}
public DirExplorer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DirExplorer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mAdapter = new DirAdapter();
setAdapter(mAdapter);
setLayoutManager(new LinearLayoutManager(context));
LinearDividerItemDecoration decoration = new LinearDividerItemDecoration(
LinearDividerItemDecoration.VERTICAL, context.getResources().getColor(R.color.divider_light),
LayoutUtils.dp2pix(context, 1));
decoration.setShowLastDivider(true);
decoration.setPadding(context.getResources().getDimensionPixelOffset(R.dimen.keyline_margin));
addItemDecoration(decoration);
setOnItemClickListener(this);
mCurrentFile = Environment.getExternalStorageDirectory();
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| mCurrentFile == null) {
mCurrentFile = new File("/");
}
updateFileList();
}
private void updateFileList() {
File[] files = mCurrentFile.listFiles(DIR_FILTER);
mFiles.clear();
if (mCurrentFile.getParent() != null) {
mFiles.add(PARENT_DIR);
}
if (files != null) {
Collections.addAll(mFiles, files);
}
// sort
Collections.sort(mFiles, FILE_SORT);
}
public File getCurrentFile() {
return mCurrentFile;
}
public void setCurrentFile(File file) {
if (file != null && file.isDirectory()) {
mCurrentFile = file;
updateFileList();
mAdapter.notifyDataSetChanged();
}
}
@Override
public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
File file = mFiles.get(position);
if (file == PARENT_DIR) {
file = mCurrentFile.getParentFile();
}
setCurrentFile(file);
return true;
}
private class DirHolder extends RecyclerView.ViewHolder {
public TextView textView;
public DirHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
}
}
private class DirAdapter extends RecyclerView.Adapter<DirHolder> {
@Override
public DirHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new DirHolder( LayoutInflater.from(getContext()).inflate(R.layout.item_dir_explorer, parent, false));
}
@Override
public void onBindViewHolder(DirHolder holder, int position) {
File file = mFiles.get(position);
holder.textView.setText(file == PARENT_DIR ? PARENT_DIR_NAME : file.getName());
}
@Override
public int getItemCount() {
return mFiles.size();
}
}
static class DirFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
}
static class FileSort implements Comparator<File> {
@Override
public int compare(File lhs, File rhs) {
if (lhs == null) {
return Integer.MIN_VALUE;
} else if (rhs == null) {
return Integer.MAX_VALUE;
} else {
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
}
}
}