|
| 1 | +package com.thirtydegreesray.dataautoaccess; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.os.Parcelable; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import java.io.Serializable; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.util.ArrayList; |
| 10 | + |
| 11 | +/** |
| 12 | + * data auto access tool |
| 13 | + * @author ThirtyDegreesRay |
| 14 | + * |
| 15 | + */ |
| 16 | +public class DataAutoAccessTool { |
| 17 | + |
| 18 | + private final static String TAG = "DataAutoAccessTool"; |
| 19 | + |
| 20 | + /** |
| 21 | + * save data |
| 22 | + * @param injectedSource the object need to save data |
| 23 | + * @param outState the bundle to save data |
| 24 | + */ |
| 25 | + @SuppressWarnings("unchecked") |
| 26 | + public static void saveData(Object injectedSource, Bundle outState){ |
| 27 | + if(injectedSource == null || outState == null) |
| 28 | + return ; |
| 29 | + |
| 30 | + Field[] fields = injectedSource.getClass().getDeclaredFields(); |
| 31 | + if(fields!=null && fields.length>0){ |
| 32 | + for(Field field : fields){ |
| 33 | + try { |
| 34 | + field.setAccessible(true); |
| 35 | + |
| 36 | + DataAutoAccess dataAutoAccess = field.getAnnotation(DataAutoAccess.class); |
| 37 | + if(dataAutoAccess != null){ |
| 38 | + Class<?> type = field.getType(); |
| 39 | + Object value = field.get(injectedSource); |
| 40 | + String key = field.getName(); |
| 41 | + |
| 42 | + if(type.equals(String.class)){ |
| 43 | + outState.putString(key, (String) value); |
| 44 | + }else if(type.equals(int.class)){ |
| 45 | + outState.putInt(key, (Integer) value); |
| 46 | + }else if(type.equals(boolean.class)){ |
| 47 | + outState.putBoolean(key, (Boolean) value); |
| 48 | + }else if(type.equals(double.class)){ |
| 49 | + outState.putDouble(key, (Double) value); |
| 50 | + }else if(type.equals(float.class)){ |
| 51 | + outState.putFloat(key, (Float) value); |
| 52 | + }else if(type.equals(long.class)){ |
| 53 | + outState.putLong(key, (Long) value); |
| 54 | + }else if(type.equals(byte.class)){ |
| 55 | + outState.putByte(key, (Byte) value); |
| 56 | + }else if(type.equals(char.class)){ |
| 57 | + outState.putChar(key, (Character) value); |
| 58 | + }else if(type.equals(short.class)){ |
| 59 | + outState.putShort(key, (Short) value); |
| 60 | + }else if(type.equals(Parcelable.class)){ |
| 61 | + outState.putParcelable(key, (Parcelable) value); |
| 62 | + }else if(type.equals(Serializable.class)){ |
| 63 | + outState.putSerializable(key, (Serializable) value); |
| 64 | + }else if(type.equals(Bundle.class)){ |
| 65 | + outState.putBundle(key, (Bundle) value); |
| 66 | + } |
| 67 | + |
| 68 | + else if(type.equals(ArrayList.class)){ |
| 69 | + Class<?> arrayListType = dataAutoAccess.arrayListType(); |
| 70 | + if(arrayListType.equals(String.class)){ |
| 71 | + outState.putStringArrayList(key, (ArrayList<String>) value); |
| 72 | + }else if(arrayListType.equals(Integer.class)){ |
| 73 | + outState.putIntegerArrayList(key, (ArrayList<Integer>) value); |
| 74 | + }else if(arrayListType.equals(Parcelable.class)){ |
| 75 | + outState.putParcelableArrayList(key, (ArrayList<? extends Parcelable>) value); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + else if(type.equals(String[].class)){ |
| 80 | + outState.putStringArray(key, (String[]) value); |
| 81 | + }else if(type.equals(int[].class)){ |
| 82 | + outState.putIntArray(key, (int[]) value); |
| 83 | + }else if(type.equals(boolean[].class)){ |
| 84 | + outState.putBooleanArray(key, (boolean[]) value); |
| 85 | + }else if(type.equals(double[].class)){ |
| 86 | + outState.putDoubleArray(key, (double[]) value); |
| 87 | + }else if(type.equals(float[].class)){ |
| 88 | + outState.putFloatArray(key, (float[]) value); |
| 89 | + }else if(type.equals(long[].class)){ |
| 90 | + outState.putLongArray(key, (long[]) value); |
| 91 | + }else if(type.equals(byte[].class)){ |
| 92 | + outState.putByteArray(key, (byte[]) value); |
| 93 | + }else if(type.equals(char[].class)){ |
| 94 | + outState.putCharArray(key, (char[]) value); |
| 95 | + }else if(type.equals(short[].class)){ |
| 96 | + outState.putShortArray(key, (short[]) value); |
| 97 | + }else if(type.equals(Parcelable[].class)){ |
| 98 | + outState.putParcelableArray(key, (Parcelable[]) value); |
| 99 | + } |
| 100 | +// Log.i("Save", "save success:filed " + field.getName() ); |
| 101 | + } |
| 102 | + } catch (Exception e) { |
| 103 | + e.printStackTrace(); |
| 104 | + Log.w("Save", "save exception:filed " + field.getName() + " " + e.getMessage().toString()); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * get data from bundle, and init filed value |
| 112 | + * @param injectedSource the object need init |
| 113 | + * @param data bundle data |
| 114 | + * @param isFromIntent if the data from intent, set true, otherwise set false |
| 115 | + */ |
| 116 | + public static void getData(Object injectedSource, Bundle data, boolean isFromIntent){ |
| 117 | + if(injectedSource == null || data == null) |
| 118 | + return ; |
| 119 | + |
| 120 | + Field[] fields = injectedSource.getClass().getDeclaredFields(); |
| 121 | + if(fields!=null && fields.length>0){ |
| 122 | + for(Field field : fields){ |
| 123 | + String error = null; |
| 124 | + try { |
| 125 | + field.setAccessible(true); |
| 126 | + |
| 127 | + DataAutoAccess dataAutoAccess = field.getAnnotation(DataAutoAccess.class); |
| 128 | + if(dataAutoAccess != null){ |
| 129 | + String key ; |
| 130 | + //because the field name will changed when proguard, so we need to set ‘dataName' as key |
| 131 | + if(isFromIntent){ |
| 132 | + key = dataAutoAccess.dataName(); |
| 133 | + }else{ |
| 134 | + //when onSaveInstanceState, we save field name as key, so get use filed name too |
| 135 | + key = field.getName(); |
| 136 | + } |
| 137 | + |
| 138 | + if(key.equals("")) |
| 139 | + continue; |
| 140 | + |
| 141 | + if(data.containsKey(key)){ |
| 142 | + Object value = data.get(key); |
| 143 | + field.set(injectedSource, value); |
| 144 | +// Log.i("Save", "get success:filed " + field.getName() ); |
| 145 | + }else{ |
| 146 | + error = key + "don't exits"; |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + } catch (Exception e) { |
| 151 | + error = field.getName() + " get Exception:" + e.getMessage().toString(); |
| 152 | + } |
| 153 | + |
| 154 | + if(error != null){ |
| 155 | + if(isFromIntent){ |
| 156 | + Log.w("GetIntentDataError", injectedSource.getClass().getName() + " " + error); |
| 157 | + }else{ |
| 158 | + Log.w("GetSaveInstanceError", |
| 159 | + injectedSource.getClass().getName() + " " + error); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments