Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
22eb6a14ae | 3 days ago |
@ -1,131 +0,0 @@
|
||||
package net.micode.notes.ui;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.widget.Toast;
|
||||
import android.view.View;
|
||||
|
||||
import net.micode.notes.R;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class BackgroundManager {
|
||||
public static final int REQUEST_CODE_PICK_IMAGE = 2001;
|
||||
|
||||
private static final String PREF_BG_TYPE = "pref_bg_type"; // 0=color,1=builtin,2=uri
|
||||
private static final String PREF_BG_COLOR = "pref_bg_color";
|
||||
private static final String PREF_BG_RES_ID = "pref_bg_res_id";
|
||||
private static final String PREF_BG_URI = "pref_bg_uri";
|
||||
|
||||
private static final int BG_TYPE_COLOR = 0;
|
||||
private static final int BG_TYPE_BUILTIN = 1;
|
||||
private static final int BG_TYPE_URI = 2;
|
||||
|
||||
private Activity mActivity;
|
||||
private View mRootView;
|
||||
|
||||
public BackgroundManager(Activity activity, int rootViewId) {
|
||||
mActivity = activity;
|
||||
mRootView = activity.findViewById(rootViewId);
|
||||
if (mRootView == null) {
|
||||
mRootView = activity.getWindow().getDecorView();
|
||||
}
|
||||
}
|
||||
|
||||
public void applyBackgroundFromPrefs() {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
||||
int type = sp.getInt(PREF_BG_TYPE, -1);
|
||||
if (type == BG_TYPE_COLOR) {
|
||||
int color = sp.getInt(PREF_BG_COLOR, 0xFFFFFFFF);
|
||||
applyColorBackground(color);
|
||||
} else if (type == BG_TYPE_BUILTIN) {
|
||||
int resId = sp.getInt(PREF_BG_RES_ID, R.drawable.list_background);
|
||||
applyBuiltinBackground(resId);
|
||||
} else if (type == BG_TYPE_URI) {
|
||||
String uriStr = sp.getString(PREF_BG_URI, null);
|
||||
if (uriStr != null) {
|
||||
applyUriBackground(Uri.parse(uriStr));
|
||||
}
|
||||
} else {
|
||||
mRootView.setBackgroundResource(R.drawable.list_background);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyColorAndSave(int color) {
|
||||
applyColorBackground(color);
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
||||
sp.edit().putInt(PREF_BG_TYPE, BG_TYPE_COLOR).putInt(PREF_BG_COLOR, color).commit();
|
||||
}
|
||||
|
||||
public void applyBuiltinAndSave(int resId) {
|
||||
applyBuiltinBackground(resId);
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
||||
sp.edit().putInt(PREF_BG_TYPE, BG_TYPE_BUILTIN).putInt(PREF_BG_RES_ID, resId).commit();
|
||||
}
|
||||
|
||||
public void pickImageFromGallery() {
|
||||
Intent intent = new Intent(Intent.ACTION_PICK);
|
||||
intent.setType("image/*");
|
||||
mActivity.startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
|
||||
}
|
||||
|
||||
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
|
||||
Uri uri = data.getData();
|
||||
if (uri != null) {
|
||||
applyUriAndSave(uri);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void resetToDefaultAndClear() {
|
||||
mRootView.setBackgroundResource(R.drawable.list_background);
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
||||
sp.edit().remove(PREF_BG_TYPE).remove(PREF_BG_COLOR).remove(PREF_BG_RES_ID).remove(PREF_BG_URI).commit();
|
||||
}
|
||||
|
||||
private void applyColorBackground(int color) {
|
||||
mRootView.setBackgroundColor(color);
|
||||
}
|
||||
|
||||
private void applyBuiltinBackground(int resId) {
|
||||
mRootView.setBackgroundResource(resId);
|
||||
}
|
||||
|
||||
private void applyUriBackground(Uri uri) {
|
||||
try {
|
||||
InputStream is = mActivity.getContentResolver().openInputStream(uri);
|
||||
if (is != null) {
|
||||
Drawable d = Drawable.createFromStream(is, uri.toString());
|
||||
mRootView.setBackgroundDrawable(d);
|
||||
is.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(mActivity, "无法加载图片", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
private void applyUriAndSave(Uri uri) {
|
||||
try {
|
||||
InputStream is = mActivity.getContentResolver().openInputStream(uri);
|
||||
if (is != null) {
|
||||
Drawable d = Drawable.createFromStream(is, uri.toString());
|
||||
mRootView.setBackgroundDrawable(d);
|
||||
is.close();
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
||||
sp.edit().putInt(PREF_BG_TYPE, BG_TYPE_URI).putString(PREF_BG_URI, uri.toString()).commit();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(mActivity, "无法加载图片", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue