mobile/android/base/background/preferences/PreferenceManagerCompat.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial