Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2013 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | package org.mozilla.gecko.background.preferences; |
michael@0 | 18 | |
michael@0 | 19 | import java.lang.reflect.Constructor; |
michael@0 | 20 | import java.lang.reflect.Field; |
michael@0 | 21 | import java.lang.reflect.InvocationHandler; |
michael@0 | 22 | import java.lang.reflect.Method; |
michael@0 | 23 | import java.lang.reflect.Proxy; |
michael@0 | 24 | |
michael@0 | 25 | import android.app.Activity; |
michael@0 | 26 | import android.content.Context; |
michael@0 | 27 | import android.content.Intent; |
michael@0 | 28 | import android.preference.Preference; |
michael@0 | 29 | import android.preference.PreferenceManager; |
michael@0 | 30 | import android.preference.PreferenceScreen; |
michael@0 | 31 | import android.util.Log; |
michael@0 | 32 | |
michael@0 | 33 | public class PreferenceManagerCompat { |
michael@0 | 34 | |
michael@0 | 35 | private static final String TAG = PreferenceManagerCompat.class.getSimpleName(); |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Interface definition for a callback to be invoked when a |
michael@0 | 39 | * {@link Preference} in the hierarchy rooted at this {@link PreferenceScreen} is |
michael@0 | 40 | * clicked. |
michael@0 | 41 | */ |
michael@0 | 42 | interface OnPreferenceTreeClickListener { |
michael@0 | 43 | /** |
michael@0 | 44 | * Called when a preference in the tree rooted at this |
michael@0 | 45 | * {@link PreferenceScreen} has been clicked. |
michael@0 | 46 | * |
michael@0 | 47 | * @param preferenceScreen The {@link PreferenceScreen} that the |
michael@0 | 48 | * preference is located in. |
michael@0 | 49 | * @param preference The preference that was clicked. |
michael@0 | 50 | * @return Whether the click was handled. |
michael@0 | 51 | */ |
michael@0 | 52 | boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | static PreferenceManager newInstance(Activity activity, int firstRequestCode) { |
michael@0 | 56 | try { |
michael@0 | 57 | Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class); |
michael@0 | 58 | c.setAccessible(true); |
michael@0 | 59 | return c.newInstance(activity, firstRequestCode); |
michael@0 | 60 | } catch (Exception e) { |
michael@0 | 61 | Log.w(TAG, "Couldn't call constructor PreferenceManager by reflection", e); |
michael@0 | 62 | } |
michael@0 | 63 | return null; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Sets the owning preference fragment |
michael@0 | 68 | */ |
michael@0 | 69 | static void setFragment(PreferenceManager manager, PreferenceFragment fragment) { |
michael@0 | 70 | // stub |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Sets the callback to be invoked when a {@link Preference} in the |
michael@0 | 75 | * hierarchy rooted at this {@link PreferenceManager} is clicked. |
michael@0 | 76 | * |
michael@0 | 77 | * @param listener The callback to be invoked. |
michael@0 | 78 | */ |
michael@0 | 79 | static void setOnPreferenceTreeClickListener(PreferenceManager manager, final OnPreferenceTreeClickListener listener) { |
michael@0 | 80 | try { |
michael@0 | 81 | Field onPreferenceTreeClickListener = PreferenceManager.class.getDeclaredField("mOnPreferenceTreeClickListener"); |
michael@0 | 82 | onPreferenceTreeClickListener.setAccessible(true); |
michael@0 | 83 | if (listener != null) { |
michael@0 | 84 | Object proxy = Proxy.newProxyInstance( |
michael@0 | 85 | onPreferenceTreeClickListener.getType().getClassLoader(), |
michael@0 | 86 | new Class<?>[] { onPreferenceTreeClickListener.getType() }, |
michael@0 | 87 | new InvocationHandler() { |
michael@0 | 88 | @Override |
michael@0 | 89 | public Object invoke(Object proxy, Method method, Object[] args) { |
michael@0 | 90 | if (method.getName().equals("onPreferenceTreeClick")) { |
michael@0 | 91 | return Boolean.valueOf(listener.onPreferenceTreeClick((PreferenceScreen) args[0], (Preference) args[1])); |
michael@0 | 92 | } else { |
michael@0 | 93 | return null; |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | }); |
michael@0 | 97 | onPreferenceTreeClickListener.set(manager, proxy); |
michael@0 | 98 | } else { |
michael@0 | 99 | onPreferenceTreeClickListener.set(manager, null); |
michael@0 | 100 | } |
michael@0 | 101 | } catch (Exception e) { |
michael@0 | 102 | Log.w(TAG, "Couldn't set PreferenceManager.mOnPreferenceTreeClickListener by reflection", e); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | /** |
michael@0 | 107 | * Inflates a preference hierarchy from the preference hierarchies of |
michael@0 | 108 | * {@link Activity Activities} that match the given {@link Intent}. An |
michael@0 | 109 | * {@link Activity} defines its preference hierarchy with meta-data using |
michael@0 | 110 | * the {@link #METADATA_KEY_PREFERENCES} key. |
michael@0 | 111 | * <p> |
michael@0 | 112 | * If a preference hierarchy is given, the new preference hierarchies will |
michael@0 | 113 | * be merged in. |
michael@0 | 114 | * |
michael@0 | 115 | * @param queryIntent The intent to match activities. |
michael@0 | 116 | * @param rootPreferences Optional existing hierarchy to merge the new |
michael@0 | 117 | * hierarchies into. |
michael@0 | 118 | * @return The root hierarchy (if one was not provided, the new hierarchy's |
michael@0 | 119 | * root). |
michael@0 | 120 | */ |
michael@0 | 121 | static PreferenceScreen inflateFromIntent(PreferenceManager manager, Intent intent, PreferenceScreen screen) { |
michael@0 | 122 | try { |
michael@0 | 123 | Method m = PreferenceManager.class.getDeclaredMethod("inflateFromIntent", Intent.class, PreferenceScreen.class); |
michael@0 | 124 | m.setAccessible(true); |
michael@0 | 125 | PreferenceScreen prefScreen = (PreferenceScreen) m.invoke(manager, intent, screen); |
michael@0 | 126 | return prefScreen; |
michael@0 | 127 | } catch (Exception e) { |
michael@0 | 128 | Log.w(TAG, "Couldn't call PreferenceManager.inflateFromIntent by reflection", e); |
michael@0 | 129 | } |
michael@0 | 130 | return null; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | /** |
michael@0 | 134 | * Inflates a preference hierarchy from XML. If a preference hierarchy is |
michael@0 | 135 | * given, the new preference hierarchies will be merged in. |
michael@0 | 136 | * |
michael@0 | 137 | * @param context The context of the resource. |
michael@0 | 138 | * @param resId The resource ID of the XML to inflate. |
michael@0 | 139 | * @param rootPreferences Optional existing hierarchy to merge the new |
michael@0 | 140 | * hierarchies into. |
michael@0 | 141 | * @return The root hierarchy (if one was not provided, the new hierarchy's |
michael@0 | 142 | * root). |
michael@0 | 143 | * @hide |
michael@0 | 144 | */ |
michael@0 | 145 | static PreferenceScreen inflateFromResource(PreferenceManager manager, Activity activity, int resId, PreferenceScreen screen) { |
michael@0 | 146 | try { |
michael@0 | 147 | Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class); |
michael@0 | 148 | m.setAccessible(true); |
michael@0 | 149 | PreferenceScreen prefScreen = (PreferenceScreen) m.invoke(manager, activity, resId, screen); |
michael@0 | 150 | return prefScreen; |
michael@0 | 151 | } catch (Exception e) { |
michael@0 | 152 | Log.w(TAG, "Couldn't call PreferenceManager.inflateFromResource by reflection", e); |
michael@0 | 153 | } |
michael@0 | 154 | return null; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | /** |
michael@0 | 158 | * Returns the root of the preference hierarchy managed by this class. |
michael@0 | 159 | * |
michael@0 | 160 | * @return The {@link PreferenceScreen} object that is at the root of the hierarchy. |
michael@0 | 161 | */ |
michael@0 | 162 | static PreferenceScreen getPreferenceScreen(PreferenceManager manager) { |
michael@0 | 163 | try { |
michael@0 | 164 | Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen"); |
michael@0 | 165 | m.setAccessible(true); |
michael@0 | 166 | return (PreferenceScreen) m.invoke(manager); |
michael@0 | 167 | } catch (Exception e) { |
michael@0 | 168 | Log.w(TAG, "Couldn't call PreferenceManager.getPreferenceScreen by reflection", e); |
michael@0 | 169 | } |
michael@0 | 170 | return null; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | /** |
michael@0 | 174 | * Called by the {@link PreferenceManager} to dispatch a subactivity result. |
michael@0 | 175 | */ |
michael@0 | 176 | static void dispatchActivityResult(PreferenceManager manager, int requestCode, int resultCode, Intent data) { |
michael@0 | 177 | try { |
michael@0 | 178 | Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class); |
michael@0 | 179 | m.setAccessible(true); |
michael@0 | 180 | m.invoke(manager, requestCode, resultCode, data); |
michael@0 | 181 | } catch (Exception e) { |
michael@0 | 182 | Log.w(TAG, "Couldn't call PreferenceManager.dispatchActivityResult by reflection", e); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * Called by the {@link PreferenceManager} to dispatch the activity stop |
michael@0 | 188 | * event. |
michael@0 | 189 | */ |
michael@0 | 190 | static void dispatchActivityStop(PreferenceManager manager) { |
michael@0 | 191 | try { |
michael@0 | 192 | Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop"); |
michael@0 | 193 | m.setAccessible(true); |
michael@0 | 194 | m.invoke(manager); |
michael@0 | 195 | } catch (Exception e) { |
michael@0 | 196 | Log.w(TAG, "Couldn't call PreferenceManager.dispatchActivityStop by reflection", e); |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /** |
michael@0 | 201 | * Called by the {@link PreferenceManager} to dispatch the activity destroy |
michael@0 | 202 | * event. |
michael@0 | 203 | */ |
michael@0 | 204 | static void dispatchActivityDestroy(PreferenceManager manager) { |
michael@0 | 205 | try { |
michael@0 | 206 | Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy"); |
michael@0 | 207 | m.setAccessible(true); |
michael@0 | 208 | m.invoke(manager); |
michael@0 | 209 | } catch (Exception e) { |
michael@0 | 210 | Log.w(TAG, "Couldn't call PreferenceManager.dispatchActivityDestroy by reflection", e); |
michael@0 | 211 | } |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * Sets the root of the preference hierarchy. |
michael@0 | 216 | * |
michael@0 | 217 | * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy. |
michael@0 | 218 | * @return Whether the {@link PreferenceScreen} given is different than the previous. |
michael@0 | 219 | */ |
michael@0 | 220 | static boolean setPreferences(PreferenceManager manager, PreferenceScreen screen) { |
michael@0 | 221 | try { |
michael@0 | 222 | Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class); |
michael@0 | 223 | m.setAccessible(true); |
michael@0 | 224 | return ((Boolean) m.invoke(manager, screen)); |
michael@0 | 225 | } catch (Exception e) { |
michael@0 | 226 | Log.w(TAG, "Couldn't call PreferenceManager.setPreferences by reflection", e); |
michael@0 | 227 | } |
michael@0 | 228 | return false; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | } |