SharedPreferencesProvider.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package epson.provider;
  2. import android.app.ActivityManager;
  3. import android.content.ContentProvider;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.content.UriMatcher;
  8. import android.database.Cursor;
  9. import android.database.MatrixCursor;
  10. import android.net.Uri;
  11. import android.os.Binder;
  12. import com.box.androidsdk.content.requests.BoxRequestsMetadata;
  13. import com.epson.iprint.prtlogger.AnalyticsPreferences;
  14. import com.google.android.gms.actions.SearchIntents;
  15. import epson.print.IprintApplication;
  16. import epson.print.Util.EPLog;
  17. import java.util.Map;
  18. public class SharedPreferencesProvider extends ContentProvider {
  19. private static final String ANALYTICS = "analytics";
  20. public static Uri BASE_URI = null;
  21. private static final String BOOLEAN_TYPE = "boolean";
  22. private static final String FLOAT_TYPE = "float";
  23. private static final String INT_TYPE = "integer";
  24. private static final String KEY = "key";
  25. private static final String LONG_TYPE = "long";
  26. private static final int MATCH_DATA = 65536;
  27. private static final int MATCH_DATA_ANALYTICS = 65537;
  28. public static String PREFFERENCE_AUTHORITY = "epson.print.provider.SharedPreferences";
  29. private static final String STRING_TYPE = "string";
  30. private static final String TYPE = "type";
  31. private static final String mAuthorizationPackageName = "epson.print:remote";
  32. private static final String mAuthorizationPackageName2 = "epson.print";
  33. private static SharedPreferences mPrefs;
  34. private static UriMatcher matcher;
  35. String TAG = "SharedPreferencesProvider";
  36. private static void init(Context context) {
  37. matcher = new UriMatcher(-1);
  38. matcher.addURI(PREFFERENCE_AUTHORITY, "analytics/*/*", 65537);
  39. matcher.addURI(PREFFERENCE_AUTHORITY, "*/*", 65536);
  40. BASE_URI = Uri.parse("content://" + PREFFERENCE_AUTHORITY);
  41. }
  42. public boolean onCreate() {
  43. String str = TAG;
  44. EPLog.d(str, "onCreate " + Binder.getCallingPid() + ", " + Thread.currentThread().getName());
  45. mPrefs = getContext().getSharedPreferences("PrintSetting", 0);
  46. if (matcher != null) {
  47. return true;
  48. }
  49. init(getContext());
  50. return true;
  51. }
  52. @Nullable
  53. public Cursor query(@NonNull Uri uri, @Nullable String[] strArr, @Nullable String str, @Nullable String[] strArr2, @Nullable String str2) {
  54. Object obj;
  55. if (uri == null || uri.getPathSegments() == null) {
  56. EPLog.m306e(SearchIntents.EXTRA_QUERY);
  57. throw new UnsupportedOperationException("");
  58. } else if (matcher.match(uri) != 65536) {
  59. MatrixCursor matrixCursor = new MatrixCursor(new String[]{KEY, BoxRequestsMetadata.UpdateItemMetadata.BoxMetadataUpdateTask.VALUE});
  60. Map<String, ?> all = mPrefs.getAll();
  61. for (String next : all.keySet()) {
  62. matrixCursor.addRow(new Object[]{next, all.get(next)});
  63. }
  64. return matrixCursor;
  65. } else {
  66. String str3 = uri.getPathSegments().get(0);
  67. String str4 = uri.getPathSegments().get(1);
  68. MatrixCursor matrixCursor2 = new MatrixCursor(new String[]{str3});
  69. if (!mPrefs.contains(str3)) {
  70. return matrixCursor2;
  71. }
  72. MatrixCursor.RowBuilder newRow = matrixCursor2.newRow();
  73. if (STRING_TYPE.equals(str4)) {
  74. obj = mPrefs.getString(str3, (String) null);
  75. } else if (BOOLEAN_TYPE.equals(str4)) {
  76. obj = Integer.valueOf(mPrefs.getBoolean(str3, false) ? 1 : 0);
  77. } else if (LONG_TYPE.equals(str4)) {
  78. obj = Long.valueOf(mPrefs.getLong(str3, 0));
  79. } else if (INT_TYPE.equals(str4)) {
  80. obj = Integer.valueOf(mPrefs.getInt(str3, 0));
  81. } else if (FLOAT_TYPE.equals(str4)) {
  82. obj = Float.valueOf(mPrefs.getFloat(str3, 0.0f));
  83. } else {
  84. throw new IllegalArgumentException("Unsupported type " + uri);
  85. }
  86. newRow.add(obj);
  87. return matrixCursor2;
  88. }
  89. }
  90. @Nullable
  91. private String getPackageNameFromPid(int i) {
  92. for (ActivityManager.RunningAppProcessInfo next : ((ActivityManager) getContext().getSystemService("activity")).getRunningAppProcesses()) {
  93. if (next.pid == i) {
  94. return next.processName;
  95. }
  96. }
  97. return null;
  98. }
  99. private static String getPackageName(int i) {
  100. for (ActivityManager.RunningAppProcessInfo next : ((ActivityManager) IprintApplication.getInstance().getSystemService("activity")).getRunningAppProcesses()) {
  101. if (next.pid == i) {
  102. return next.processName;
  103. }
  104. }
  105. return null;
  106. }
  107. private void checkPermission(int i) {
  108. String packageNameFromPid = getPackageNameFromPid(i);
  109. if (mAuthorizationPackageName.equals(packageNameFromPid) || "epson.print".equals(packageNameFromPid)) {
  110. EPLog.m312i("Access OK !!: from " + packageNameFromPid);
  111. return;
  112. }
  113. EPLog.m312i("Bad access !! : from " + packageNameFromPid);
  114. throw new SecurityException();
  115. }
  116. public static boolean checkPackageUseSharedPreferencesProvider(Context context) {
  117. String packageName = getPackageName(Binder.getCallingPid());
  118. if (context != null) {
  119. EPLog.m304d("★ PrintSetting from " + context.toString());
  120. }
  121. if (!mAuthorizationPackageName.equals(packageName)) {
  122. return context != null && context.toString().contains("EpsonService");
  123. }
  124. return true;
  125. }
  126. public Uri insert(Uri uri, ContentValues contentValues) {
  127. SharedPreferences.Editor editor;
  128. EpLog.m77i("ContentValues ####### " + contentValues);
  129. checkPermission(Binder.getCallingPid());
  130. switch (matcher.match(uri)) {
  131. case 65536:
  132. editor = mPrefs.edit();
  133. EpLog.m77i(" !!!!!!!! MATCH_DATA !!!!!!!!!!!");
  134. break;
  135. case 65537:
  136. editor = getContext().getSharedPreferences(AnalyticsPreferences.PREFS_NAME, 0).edit();
  137. EpLog.m77i(" !!!!!!!! MATCH_DATA_ANALYTICS !!!!!!!!!!!");
  138. break;
  139. default:
  140. throw new IllegalArgumentException("Unsupported uri " + uri);
  141. }
  142. switch (matcher.match(uri)) {
  143. case 65536:
  144. case 65537:
  145. for (Map.Entry next : contentValues.valueSet()) {
  146. Object value = next.getValue();
  147. String str = (String) next.getKey();
  148. if (value == null) {
  149. editor.remove(str);
  150. } else if (value instanceof String) {
  151. editor.putString(str, (String) value);
  152. } else if (value instanceof Boolean) {
  153. editor.putBoolean(str, ((Boolean) value).booleanValue());
  154. } else if (value instanceof Long) {
  155. editor.putLong(str, ((Long) value).longValue());
  156. } else if (value instanceof Integer) {
  157. editor.putInt(str, ((Integer) value).intValue());
  158. } else if (value instanceof Float) {
  159. editor.putFloat(str, ((Float) value).floatValue());
  160. } else {
  161. throw new IllegalArgumentException("Unsupported type " + uri);
  162. }
  163. }
  164. editor.apply();
  165. return null;
  166. default:
  167. throw new IllegalArgumentException("Unsupported uri " + uri);
  168. }
  169. }
  170. public int update(@NonNull Uri uri, @NonNull ContentValues contentValues, @Nullable String str, @Nullable String[] strArr) {
  171. EPLog.m312i("★★    update ");
  172. checkPermission(Binder.getCallingPid());
  173. SharedPreferences.Editor edit = mPrefs.edit();
  174. for (Map.Entry next : contentValues.valueSet()) {
  175. String str2 = (String) next.getKey();
  176. Object value = next.getValue();
  177. if (value instanceof Integer) {
  178. edit.putInt(str2, ((Integer) next.getValue()).intValue());
  179. } else if (value instanceof Boolean) {
  180. edit.putBoolean(str2, ((Boolean) next.getValue()).booleanValue());
  181. } else {
  182. EPLog.m306e("★ " + value.toString());
  183. }
  184. }
  185. edit.apply();
  186. return contentValues.size();
  187. }
  188. public int delete(@NonNull Uri uri, @NonNull String str, @Nullable String[] strArr) {
  189. checkPermission(Binder.getCallingPid());
  190. if (matcher.match(uri) == 65536) {
  191. mPrefs.edit().clear().apply();
  192. return 0;
  193. }
  194. throw new IllegalArgumentException("Unsupported uri " + uri);
  195. }
  196. @Nullable
  197. public String getType(@NonNull Uri uri) {
  198. throw new UnsupportedOperationException();
  199. }
  200. private static String getStringValue(Cursor cursor, String str) {
  201. if (cursor == null) {
  202. return str;
  203. }
  204. if (cursor.moveToFirst()) {
  205. str = cursor.getString(0);
  206. }
  207. cursor.close();
  208. return str;
  209. }
  210. private static boolean getBooleanValue(Cursor cursor, boolean z) {
  211. if (cursor == null) {
  212. return z;
  213. }
  214. if (cursor.moveToFirst()) {
  215. z = false;
  216. if (cursor.getInt(0) > 0) {
  217. z = true;
  218. }
  219. }
  220. cursor.close();
  221. return z;
  222. }
  223. private static int getIntValue(Cursor cursor, int i) {
  224. if (cursor == null) {
  225. return i;
  226. }
  227. if (cursor.moveToFirst()) {
  228. i = cursor.getInt(0);
  229. }
  230. cursor.close();
  231. return i;
  232. }
  233. private static long getLongValue(Cursor cursor, long j) {
  234. if (cursor == null) {
  235. return j;
  236. }
  237. if (cursor.moveToFirst()) {
  238. j = cursor.getLong(0);
  239. }
  240. cursor.close();
  241. return j;
  242. }
  243. private static float getFloatValue(Cursor cursor, float f) {
  244. if (cursor == null) {
  245. return f;
  246. }
  247. if (cursor.moveToFirst()) {
  248. f = cursor.getFloat(0);
  249. }
  250. cursor.close();
  251. return f;
  252. }
  253. @NonNull
  254. public static Editor edit(Context context) {
  255. return new Editor(context);
  256. }
  257. @NonNull
  258. public static SharedPreferencesMulti getInstace(Context context) {
  259. mPrefs = context.getSharedPreferences("PrintSetting", 0);
  260. return new SharedPreferencesMulti(context);
  261. }
  262. @NonNull
  263. public static SharedPreferencesMulti getInstace(Context context, String str) {
  264. mPrefs = context.getSharedPreferences(str, 0);
  265. return new SharedPreferencesMulti(context);
  266. }
  267. public static class Editor {
  268. Context context;
  269. SharedPreferences.Editor editor;
  270. private ContentValues values;
  271. private Editor(Context context2) {
  272. values = new ContentValues();
  273. context = context2;
  274. editor = SharedPreferencesProvider.mPrefs.edit();
  275. }
  276. public void apply() {
  277. context.getContentResolver().insert(SharedPreferencesProvider.getContentUri(this.context, SharedPreferencesProvider.KEY, "type"), values);
  278. }
  279. public void apply(String str) {
  280. if (((str.hashCode() == 829777079 && str.equals(AnalyticsPreferences.PREFS_NAME)) ? (char) 0 : 65535) == 0) {
  281. context.getContentResolver().insert(SharedPreferencesProvider.getContentUri(this.context, SharedPreferencesProvider.ANALYTICS), values);
  282. return;
  283. }
  284. throw new IllegalArgumentException("Unknown preference!" + str);
  285. }
  286. public void commit() {
  287. apply();
  288. }
  289. public Editor putString(String str, String str2) {
  290. values.put(str, str2);
  291. return this;
  292. }
  293. public Editor putLong(String str, long j) {
  294. values.put(str, Long.valueOf(j));
  295. return this;
  296. }
  297. public Editor putBoolean(String str, boolean z) {
  298. values.put(str, Boolean.valueOf(z));
  299. return this;
  300. }
  301. public Editor putInt(String str, int i) {
  302. values.put(str, Integer.valueOf(i));
  303. return this;
  304. }
  305. public Editor putFloat(String str, float f) {
  306. values.put(str, Float.valueOf(f));
  307. return this;
  308. }
  309. public void remove(String str) {
  310. values.putNull(str);
  311. }
  312. public void clear() {
  313. if (SharedPreferencesProvider.checkPackageUseSharedPreferencesProvider(this.context)) {
  314. context.getContentResolver().delete(SharedPreferencesProvider.getContentUri(this.context, SharedPreferencesProvider.KEY, "type"), (String) null, (String[]) null);
  315. } else {
  316. editor.clear();
  317. }
  318. }
  319. }
  320. public static class SharedPreferencesMulti {
  321. private Context context;
  322. private SharedPreferencesMulti(Context context2) {
  323. context = context2;
  324. }
  325. public Editor edit() {
  326. return new Editor(this.context);
  327. }
  328. public String getString(String str, String str2) {
  329. return SharedPreferencesProvider.getStringValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.STRING_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), str2);
  330. }
  331. public long getLong(String str, long j) {
  332. return SharedPreferencesProvider.getLongValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.LONG_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), j);
  333. }
  334. public float getFloat(String str, float f) {
  335. return SharedPreferencesProvider.getFloatValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.FLOAT_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), f);
  336. }
  337. public boolean getBoolean(String str, boolean z) {
  338. return SharedPreferencesProvider.getBooleanValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.BOOLEAN_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), z);
  339. }
  340. public int getInt(String str, int i) {
  341. return SharedPreferencesProvider.getIntValue(this.context.getContentResolver().query(SharedPreferencesProvider.getContentUri(this.context, str, SharedPreferencesProvider.INT_TYPE), (String[]) null, (String) null, (String[]) null, (String) null), i);
  342. }
  343. }
  344. private static final Uri getContentUri(Context context, String str, String str2) {
  345. if (BASE_URI == null) {
  346. init(context);
  347. }
  348. Uri build = BASE_URI.buildUpon().appendPath(str).appendPath(str2).build();
  349. EpLog.d("URI = " + build.toString());
  350. return build;
  351. }
  352. private static final Uri getContentUri(Context context, String str) {
  353. if (BASE_URI == null) {
  354. init(context);
  355. }
  356. Uri build = BASE_URI.buildUpon().appendPath(str).appendPath(str).appendPath(str).build();
  357. EpLog.d("URI = " + build.toString());
  358. return build;
  359. }
  360. }