123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- package epson.provider;
- import android.app.ActivityManager;
- import android.content.ContentProvider;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.UriMatcher;
- import android.database.Cursor;
- import android.database.MatrixCursor;
- import android.net.Uri;
- import android.os.Binder;
- import com.box.androidsdk.content.requests.BoxRequestsMetadata;
- import com.epson.iprint.prtlogger.AnalyticsPreferences;
- import com.google.android.gms.actions.SearchIntents;
- import epson.print.IprintApplication;
- import epson.print.Util.EPLog;
- import java.util.Map;
- public class SharedPreferencesProvider extends ContentProvider {
- private static final String ANALYTICS = "analytics";
- public static Uri BASE_URI = null;
- private static final String BOOLEAN_TYPE = "boolean";
- private static final String FLOAT_TYPE = "float";
- private static final String INT_TYPE = "integer";
- private static final String KEY = "key";
- private static final String LONG_TYPE = "long";
- private static final int MATCH_DATA = 65536;
- private static final int MATCH_DATA_ANALYTICS = 65537;
- public static String PREFFERENCE_AUTHORITY = "epson.print.provider.SharedPreferences";
- private static final String STRING_TYPE = "string";
- private static final String TYPE = "type";
- private static final String mAuthorizationPackageName = "epson.print:remote";
- private static final String mAuthorizationPackageName2 = "epson.print";
- private static SharedPreferences mPrefs;
- private static UriMatcher matcher;
- String TAG = "SharedPreferencesProvider";
- private static void init(Context context) {
- matcher = new UriMatcher(-1);
- matcher.addURI(PREFFERENCE_AUTHORITY, "analytics/*/*", 65537);
- matcher.addURI(PREFFERENCE_AUTHORITY, "*/*", 65536);
- BASE_URI = Uri.parse("content://" + PREFFERENCE_AUTHORITY);
- }
- public boolean onCreate() {
- String str = TAG;
- EPLog.m305d(str, "onCreate " + Binder.getCallingPid() + ", " + Thread.currentThread().getName());
- mPrefs = getContext().getSharedPreferences("PrintSetting", 0);
- if (matcher != null) {
- return true;
- }
- init(getContext());
- return true;
- }
- @Nullable
- public Cursor query(@NonNull Uri uri, @Nullable String[] strArr, @Nullable String str, @Nullable String[] strArr2, @Nullable String str2) {
- Object obj;
- if (uri == null || uri.getPathSegments() == null) {
- EPLog.m306e(SearchIntents.EXTRA_QUERY);
- throw new UnsupportedOperationException("");
- } else if (matcher.match(uri) != 65536) {
- MatrixCursor matrixCursor = new MatrixCursor(new String[]{KEY, BoxRequestsMetadata.UpdateItemMetadata.BoxMetadataUpdateTask.VALUE});
- Map<String, ?> all = mPrefs.getAll();
- for (String next : all.keySet()) {
- matrixCursor.addRow(new Object[]{next, all.get(next)});
- }
- return matrixCursor;
- } else {
- String str3 = uri.getPathSegments().get(0);
- String str4 = uri.getPathSegments().get(1);
- MatrixCursor matrixCursor2 = new MatrixCursor(new String[]{str3});
- if (!mPrefs.contains(str3)) {
- return matrixCursor2;
- }
- MatrixCursor.RowBuilder newRow = matrixCursor2.newRow();
- if (STRING_TYPE.equals(str4)) {
- obj = mPrefs.getString(str3, (String) null);
- } else if (BOOLEAN_TYPE.equals(str4)) {
- obj = Integer.valueOf(mPrefs.getBoolean(str3, false) ? 1 : 0);
- } else if (LONG_TYPE.equals(str4)) {
- obj = Long.valueOf(mPrefs.getLong(str3, 0));
- } else if (INT_TYPE.equals(str4)) {
- obj = Integer.valueOf(mPrefs.getInt(str3, 0));
- } else if (FLOAT_TYPE.equals(str4)) {
- obj = Float.valueOf(mPrefs.getFloat(str3, 0.0f));
- } else {
- throw new IllegalArgumentException("Unsupported type " + uri);
- }
- newRow.add(obj);
- return matrixCursor2;
- }
- }
- @Nullable
- private String getPackageNameFromPid(int i) {
- for (ActivityManager.RunningAppProcessInfo next : ((ActivityManager) getContext().getSystemService("activity")).getRunningAppProcesses()) {
- if (next.pid == i) {
- return next.processName;
- }
- }
- return null;
- }
- private static String getPackageName(int i) {
- for (ActivityManager.RunningAppProcessInfo next : ((ActivityManager) IprintApplication.getInstance().getSystemService("activity")).getRunningAppProcesses()) {
- if (next.pid == i) {
- return next.processName;
- }
- }
- return null;
- }
- private void checkPermission(int i) {
- String packageNameFromPid = getPackageNameFromPid(i);
- if (mAuthorizationPackageName.equals(packageNameFromPid) || "epson.print".equals(packageNameFromPid)) {
- EPLog.m312i("Access OK !!: from " + packageNameFromPid);
- return;
- }
- EPLog.m312i("Bad access !! : from " + packageNameFromPid);
- throw new SecurityException();
- }
- public static boolean checkPackageUseSharedPreferencesProvider(Context context) {
- String packageName = getPackageName(Binder.getCallingPid());
- if (context != null) {
- EPLog.m304d("★ PrintSetting from " + context.toString());
- }
- if (!mAuthorizationPackageName.equals(packageName)) {
- return context != null && context.toString().contains("EpsonService");
- }
- return true;
- }
- public Uri insert(Uri uri, ContentValues contentValues) {
- SharedPreferences.Editor editor;
- EpLog.m77i("ContentValues ####### " + contentValues);
- checkPermission(Binder.getCallingPid());
- switch (matcher.match(uri)) {
- case 65536:
- editor = mPrefs.edit();
- EpLog.m77i(" !!!!!!!! MATCH_DATA !!!!!!!!!!!");
- break;
- case 65537:
- editor = getContext().getSharedPreferences(AnalyticsPreferences.PREFS_NAME, 0).edit();
- EpLog.m77i(" !!!!!!!! MATCH_DATA_ANALYTICS !!!!!!!!!!!");
- break;
- default:
- throw new IllegalArgumentException("Unsupported uri " + uri);
- }
- switch (matcher.match(uri)) {
- case 65536:
- case 65537:
- for (Map.Entry next : contentValues.valueSet()) {
- Object value = next.getValue();
- String str = (String) next.getKey();
- if (value == null) {
- editor.remove(str);
- } else if (value instanceof String) {
- editor.putString(str, (String) value);
- } else if (value instanceof Boolean) {
- editor.putBoolean(str, ((Boolean) value).booleanValue());
- } else if (value instanceof Long) {
- editor.putLong(str, ((Long) value).longValue());
- } else if (value instanceof Integer) {
- editor.putInt(str, ((Integer) value).intValue());
- } else if (value instanceof Float) {
- editor.putFloat(str, ((Float) value).floatValue());
- } else {
- throw new IllegalArgumentException("Unsupported type " + uri);
- }
- }
- editor.apply();
- return null;
- default:
- throw new IllegalArgumentException("Unsupported uri " + uri);
- }
- }
- public int update(@NonNull Uri uri, @NonNull ContentValues contentValues, @Nullable String str, @Nullable String[] strArr) {
- EPLog.m312i("★★ update ");
- checkPermission(Binder.getCallingPid());
- SharedPreferences.Editor edit = mPrefs.edit();
- for (Map.Entry next : contentValues.valueSet()) {
- String str2 = (String) next.getKey();
- Object value = next.getValue();
- if (value instanceof Integer) {
- edit.putInt(str2, ((Integer) next.getValue()).intValue());
- } else if (value instanceof Boolean) {
- edit.putBoolean(str2, ((Boolean) next.getValue()).booleanValue());
- } else {
- EPLog.m306e("★ " + value.toString());
- }
- }
- edit.apply();
- return contentValues.size();
- }
- public int delete(@NonNull Uri uri, @NonNull String str, @Nullable String[] strArr) {
- checkPermission(Binder.getCallingPid());
- if (matcher.match(uri) == 65536) {
- mPrefs.edit().clear().apply();
- return 0;
- }
- throw new IllegalArgumentException("Unsupported uri " + uri);
- }
- @Nullable
- public String getType(@NonNull Uri uri) {
- throw new UnsupportedOperationException();
- }
- private static String getStringValue(Cursor cursor, String str) {
- if (cursor == null) {
- return str;
- }
- if (cursor.moveToFirst()) {
- str = cursor.getString(0);
- }
- cursor.close();
- return str;
- }
- private static boolean getBooleanValue(Cursor cursor, boolean z) {
- if (cursor == null) {
- return z;
- }
- if (cursor.moveToFirst()) {
- z = false;
- if (cursor.getInt(0) > 0) {
- z = true;
- }
- }
- cursor.close();
- return z;
- }
- private static int getIntValue(Cursor cursor, int i) {
- if (cursor == null) {
- return i;
- }
- if (cursor.moveToFirst()) {
- i = cursor.getInt(0);
- }
- cursor.close();
- return i;
- }
- private static long getLongValue(Cursor cursor, long j) {
- if (cursor == null) {
- return j;
- }
- if (cursor.moveToFirst()) {
- j = cursor.getLong(0);
- }
- cursor.close();
- return j;
- }
- private static float getFloatValue(Cursor cursor, float f) {
- if (cursor == null) {
- return f;
- }
- if (cursor.moveToFirst()) {
- f = cursor.getFloat(0);
- }
- cursor.close();
- return f;
- }
- @NonNull
- public static Editor edit(Context context) {
- return new Editor(context);
- }
- @NonNull
- public static SharedPreferencesMulti getInstace(Context context) {
- mPrefs = context.getSharedPreferences("PrintSetting", 0);
- return new SharedPreferencesMulti(context);
- }
- @NonNull
- public static SharedPreferencesMulti getInstace(Context context, String str) {
- mPrefs = context.getSharedPreferences(str, 0);
- return new SharedPreferencesMulti(context);
- }
- public static class Editor {
- Context context;
- SharedPreferences.Editor editor;
- private ContentValues values;
- private Editor(Context context2) {
- values = new ContentValues();
- context = context2;
- editor = SharedPreferencesProvider.mPrefs.edit();
- }
- public void apply() {
- context.getContentResolver().insert(SharedPreferencesProvider.getContentUri(this.context, SharedPreferencesProvider.KEY, "type"), values);
- }
- public void apply(String str) {
- if (((str.hashCode() == 829777079 && str.equals(AnalyticsPreferences.PREFS_NAME)) ? (char) 0 : 65535) == 0) {
- context.getContentResolver().insert(SharedPreferencesProvider.getContentUri(this.context, SharedPreferencesProvider.ANALYTICS), values);
- return;
- }
- throw new IllegalArgumentException("Unknown preference!" + str);
- }
- public void commit() {
- apply();
- }
- public Editor putString(String str, String str2) {
- values.put(str, str2);
- return this;
- }
- public Editor putLong(String str, long j) {
- values.put(str, Long.valueOf(j));
- return this;
- }
- public Editor putBoolean(String str, boolean z) {
- values.put(str, Boolean.valueOf(z));
- return this;
- }
- public Editor putInt(String str, int i) {
- values.put(str, Integer.valueOf(i));
- return this;
- }
- public Editor putFloat(String str, float f) {
- values.put(str, Float.valueOf(f));
- return this;
- }
- public void remove(String str) {
- values.putNull(str);
- }
- public void clear() {
- if (SharedPreferencesProvider.checkPackageUseSharedPreferencesProvider(this.context)) {
- context.getContentResolver().delete(SharedPreferencesProvider.getContentUri(this.context, SharedPreferencesProvider.KEY, "type"), (String) null, (String[]) null);
- } else {
- editor.clear();
- }
- }
- }
- public static class SharedPreferencesMulti {
- private Context context;
- private SharedPreferencesMulti(Context context2) {
- context = context2;
- }
- public Editor edit() {
- return new Editor(this.context);
- }
- public String getString(String str, String str2) {
- return SharedPreferencesProvider.getStringValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.STRING_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), str2);
- }
- public long getLong(String str, long j) {
- return SharedPreferencesProvider.getLongValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.LONG_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), j);
- }
- public float getFloat(String str, float f) {
- return SharedPreferencesProvider.getFloatValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.FLOAT_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), f);
- }
- public boolean getBoolean(String str, boolean z) {
- return SharedPreferencesProvider.getBooleanValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.BOOLEAN_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), z);
- }
- public int getInt(String str, int i) {
- return SharedPreferencesProvider.getIntValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.INT_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), i);
- }
- }
- private static final Uri getContentUri(Context context, String str, String str2) {
- if (BASE_URI == null) {
- init(context);
- }
- Uri build = BASE_URI.buildUpon().appendPath(str).appendPath(str2).build();
- EpLog.d("URI = " + build.toString());
- return build;
- }
- private static final Uri getContentUri(Context context, String str) {
- if (BASE_URI == null) {
- init(context);
- }
- Uri build = BASE_URI.buildUpon().appendPath(str).appendPath(str).appendPath(str).build();
- EpLog.d("URI = " + build.toString());
- return build;
- }
- }
|