@SuppressWarnings("deprecation")
public static Contextwrapper wrap(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String lang = prefs.getString(Constants.PREF_APP_LANGUAGE,"");
Locale locale = StringUtils.getLocale(lang);
locale = (locale == null) ? Locale.getDefault() : locale;
Configuration configuration = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
Locale.setDefault(locale);
configuration.setLocale(locale);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = locale;
context.getResources().updateConfiguration(configuration,null);
}
return new InventumContextwrapper(context);
}
@NonNull
private void resolveMethod(@Nullable Context context,@NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
Method method = context.getClass().getmethod(this.mMethodName,new Class[]{View.class});
if (method != null) {
this.mResolvedMethod = method;
this.mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
}
if (context instanceof Contextwrapper) {
context = ((Contextwrapper) context).getBaseContext();
} else {
context = null;
}
}
int id = this.mHostView.getId();
throw new IllegalStateException("Could not find method " + this.mMethodName + "(View) in a parent or ancestor Context for android:onClick " + "attribute defined on view " + this.mHostView.getClass() + (id == -1 ? "" : " with id '" + this.mHostView.getContext().getResources().getResourceEntryName(id) + "'"));
}
项目:Android-skin-support
文件:SkinCompatViewInflater.java
/**
* android:onClick doesn't handle views with a Contextwrapper context. This method
* backports new framework functionality to traverse the Context wrappers to find a
* suitable target.
*/
private void checkOnClickListener(View view,AttributeSet attrs) {
final Context context = view.getContext();
if (!(context instanceof Contextwrapper) ||
(Build.VERSION.SDK_INT >= 15 && !ViewCompat.hasOnClickListeners(view))) {
// Skip our compat functionality if: the Context isn't a Contextwrapper,or
// the view doesn't have an OnClickListener (we can only rely on this on API 15+ so
// always use our compat code on older devices)
return;
}
final TypedArray a = context.obtainStyledAttributes(attrs,sOnClickAttrs);
final String handlerName = a.getString(0);
if (handlerName != null) {
view.setonClickListener(new DeclaredOnClickListener(view,handlerName));
}
a.recycle();
}
项目:GitHub
文件:PresenterManager.java
/**
* Get the Activity of a context. This is typically used to determine the hosting activity of a
* {@link View}
*
* @param context The context
* @return The Activity or throws an Exception if Activity Couldnt be determined
*/
@NonNull public static Activity getActivity(@NonNull Context context) {
if (context == null) {
throw new NullPointerException("context == null");
}
if (context instanceof Activity) {
return (Activity) context;
}
while (context instanceof Contextwrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((Contextwrapper) context).getBaseContext();
}
throw new IllegalStateException("Could not find the surrounding Activity");
}
@SuppressWarnings("deprecation")
public static Contextwrapper wrap(Context context,String language) {
Configuration config = context.getResources().getConfiguration();
Locale sysLocale = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = getSystemLocale(config);
} else {
sysLocale = getSystemLocaleLegacy(config);
}
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config,locale);
} else {
setSystemLocaleLegacy(config,locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config,context.getResources().getdisplayMetrics());
}
}
return new MyContextwrapper(context);
}
项目:Phoenix-for-VK
文件:UploadUtils.java
/**
* @param token The {@link Servicetoken} to unbind from
*/
public static void unbindFromService(final Servicetoken token) {
if (token == null) {
return;
}
final Contextwrapper mContextwrapper = token.mWrappedContext;
final ServiceBinder mBinder = mConnectionMap.remove(mContextwrapper);
if (mBinder == null) {
return;
}
mContextwrapper.unbindService(mBinder);
if (mConnectionMap.isEmpty()) {
mService = null;
}
}
项目:Phoenix-for-VK
文件:MusicUtils.java
/**
* @param token The {@link Servicetoken} to unbind from
*/
public static void unbindFromService(final Servicetoken token) {
if (token == null) {
return;
}
final Contextwrapper mContextwrapper = token.mWrappedContext;
final ServiceBinder mBinder = mConnectionMap.remove(mContextwrapper);
if (mBinder == null) {
return;
}
mContextwrapper.unbindService(mBinder);
if (mConnectionMap.isEmpty()) {
mService = null;
}
}
项目:ScanLinks
文件:WhitelistActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelist);
if (getSupportActionBar() != null) {
getSupportActionBar().setElevation(0.0f);
getSupportActionBar().setdisplayShowHomeEnabled(true);
getSupportActionBar().setdisplayHomeAsUpEnabled(true);
}
new Prefs.Builder()
.setContext(this)
.setMode(Contextwrapper.MODE_PRIVATE)
.setPrefsName(getPackageName())
.setUseDefaultSharedPreference(true)
.build();
whitelistArray = Prefs.getorderedStringSet("whitelistArray",new LinkedHashSet<String>());
listView = (RecyclerView) findViewById(R.id.listView1);
RecyclerView.LayoutManager manager = new linearlayoutmanager(this);
listView.setLayoutManager(manager);
listView.setHasFixedSize(true);
listView.setAdapter(new RecyclerAdapter(whitelistArray.toArray(new String[whitelistArray.size()])));
}
项目:AndroidSkinAnimator
文件:SkinCompatViewInflater.java
/**
* android:onClick doesn't handle views with a Contextwrapper context. This method
* backports new framework functionality to traverse the Context wrappers to find a
* suitable target.
*/
private void checkOnClickListener(View view,handlerName));
}
a.recycle();
}
项目:Android-skin-support
文件:SkinCompatViewInflater.java
@NonNull
private void resolveMethod(@Nullable Context context,@NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getmethod(mMethodName,View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method,keep searching up the hierarchy.
}
if (context instanceof Contextwrapper) {
context = ((Contextwrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy,null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
项目:diycode
文件:IMMLeaks.java
private Activity extractActivity(Context context) {
while (true) {
if (context instanceof Application) {
return null;
} else if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof Contextwrapper) {
Context baseContext = ((Contextwrapper) context).getBaseContext();
// Prevent Stack Overflow.
if (baseContext == context) {
return null;
}
context = baseContext;
} else {
return null;
}
}
}
项目:MiPushFramework
文件:CondomKitTest.java
@Test @SuppressLint("HardwareIds") public void testNulldeviceidKit() {
final CondomContext condom = CondomContext.wrap(new Contextwrapper(context),"Nulldeviceid",new CondomOptions().addKit(new NulldeviceidKit()));
final TelephonyManager tm = (TelephonyManager) condom.getSystemService(Context.TELEPHONY_SERVICE);
assertTrue(condom.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE).getClass().getName().startsWith(NulldeviceidKit.class.getName()));
assertPermission(condom,READ_PHONE_STATE,true);
assertNull(tm.getdeviceid());
if (SDK_INT >= M) assertNull(tm.getdeviceid(0));
assertNull(tm.getImei());
assertNull(tm.getImei(0));
if (SDK_INT >= O) assertNull(tm.getMeid());
if (SDK_INT >= O) assertNull(tm.getMeid(0));
assertNull(tm.getSimserialNumber());
assertNull(tm.getLine1Number());
assertNull(tm.getSubscriberId());
}
项目:container
文件:NotificationCompatCompatV14.java
Context getAppContext(final String packageName) {
final Resources resources = getResources(packageName);
Context context = null;
try {
context = getHostContext().createPackageContext(packageName,Context.CONTEXT_IGnorE_Security | Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
context = getHostContext();
}
return new Contextwrapper(context) {
@Override
public Resources getResources() {
return resources;
}
@Override
public String getPackageName() {
return packageName;
}
};
}
@SmallTest
@Feature({"cronet"})
public void testinitDifferentEngines() throws Exception {
// Test that concurrently instantiating cronet context's upon varIoUs
// different versions of the same Android Context does not cause crashes
// like crbug.com/453845
mTestFramework = startcronetTestFramework();
cronetEngine firstEngine =
new cronetUrlRequestContext(mTestFramework.createcronetEngineBuilder(getContext()));
cronetEngine secondEngine = new cronetUrlRequestContext(
mTestFramework.createcronetEngineBuilder(getContext().getApplicationContext()));
cronetEngine thirdEngine = new cronetUrlRequestContext(
mTestFramework.createcronetEngineBuilder(new Contextwrapper(getContext())));
firstEngine.shutdown();
secondEngine.shutdown();
thirdEngine.shutdown();
}
项目:Bigbang
文件:IMMLeaks.java
private Activity extractActivity(Context context) {
while (true) {
if (context instanceof Application) {
return null;
} else if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof Contextwrapper) {
Context baseContext = ((Contextwrapper) context).getBaseContext();
// Prevent Stack Overflow.
if (baseContext == context) {
return null;
}
context = baseContext;
} else {
return null;
}
}
}
项目:atlas
文件:ActivityTaskMgr.java
public void popFromActivityStack(Activity activity) {
if(sReminderDialog!=null &&
(sReminderDialog.getContext()==activity ||
(sReminderDialog.getContext() instanceof Contextwrapper && ((Contextwrapper)sReminderDialog.getContext()).getBaseContext()==activity))){
try{
sReminderDialog.dismiss();
}catch (Throwable e){}finally {
sReminderDialog = null;
}
}
for(int x=0; x<activityList.size(); x++){
WeakReference<Activity> ref = activityList.get(x);
if(ref!=null && ref.get()!=null && ref.get()==activity){
activityList.remove(ref);
}
}
}
项目:ReadMark
文件:SkinAppCompatViewInflater.java
@NonNull
private void resolveMethod(@Nullable Context context,null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
项目:VirtualAPK
文件:VAInstrumentation.java
@Override
public void callActivityOnCreate(Activity activity,Bundle icicle) {
final Intent intent = activity.getIntent();
if (PluginUtil.isIntentFromPlugin(intent)) {
Context base = activity.getBaseContext();
try {
Loadedplugin plugin = this.mPluginManager.getLoadedplugin(intent);
ReflectUtil.setField(base.getClass(),base,"mResources",plugin.getResources());
ReflectUtil.setField(Contextwrapper.class,activity,"mBase",plugin.getPluginContext());
ReflectUtil.setField(Activity.class,"mApplication",plugin.getApplication());
ReflectUtil.setFieldNoException(ContextThemeWrapper.class,plugin.getPluginContext());
// set screenorientation
ActivityInfo activityInfo = plugin.getActivityInfo(PluginUtil.getComponent(intent));
if (activityInfo.screenorientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
activity.setRequestedOrientation(activityInfo.screenorientation);
}
} catch (Exception e) {
e.printstacktrace();
}
}
mBase.callActivityOnCreate(activity,icicle);
}
项目:condom
文件:CondomKitTest.java
@Test @SuppressLint("HardwareIds") public void testNulldeviceidKit() throws NameNotFoundException {
final CondomContext condom = CondomContext.wrap(new Contextwrapper(context),new CondomOptions().addKit(new NulldeviceidKit()));
final TelephonyManager tm = (TelephonyManager) condom.getSystemService(Context.TELEPHONY_SERVICE);
assertNotNull(tm);
assertTrue(tm.getClass().getName().startsWith(NulldeviceidKit.class.getName()));
final TelephonyManager app_tm = (TelephonyManager) condom.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
assertNotNull(app_tm);
assertTrue(app_tm.getClass().getName().startsWith(NulldeviceidKit.class.getName()));
assertPermission(condom,true);
assertNull(tm.getdeviceid());
if (SDK_INT >= LOLLIPOP) {
if (SDK_INT >= M) assertNull(tm.getdeviceid(0));
assertNull(tm.getImei());
assertNull(tm.getImei(0));
if (SDK_INT >= O) assertNull(tm.getMeid());
if (SDK_INT >= O) assertNull(tm.getMeid(0));
}
assertNull(tm.getSimserialNumber());
assertNull(tm.getLine1Number());
assertNull(tm.getSubscriberId());
}
项目:AndroidSnooper
文件:TestDbRule.java
private void copyDataBase(String finaldbname) {
Context applicationContext = InstrumentationRegistry.getTargetContext();
Contextwrapper cw = new Contextwrapper(applicationContext);
File dbDir = new File(getDBDirectory());
if(!dbDir.exists()) {
dbDir.mkdir();
}
Logger.d("Database","New database is being copied to device!");
byte[] buffer = new byte[1024];
int length;
try {
InputStream myInput = applicationContext.getResources().openRawResource(this.dbRawResourceId);
File file = new File(dbDir,finaldbname);
OutputStream myOutput = new FileOutputStream(file);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer,length);
}
myOutput.close();
myOutput.flush();
myInput.close();
Logger.d("Database","New database has been copied to device!");
} catch (IOException e) {
Logger.e("Database",e.getMessage(),e);
}
}
项目:simple-stack
文件:Navigator.java
/**
* Attempt to find the Activity in the Context through the chain of its base contexts.
*
* @throws IllegalArgumentException if context is null
* @throws IllegalStateException if the context's base context hierarchy doesn't contain an Activity
*
* @param context the context
* @param <T> the type of the Activity
* @return the Activity
*/
@NonNull
public static <T extends Activity> T findActivity(@NonNull Context context) {
if(context == null) {
throw new IllegalArgumentException("Context cannot be null!");
}
if(context instanceof Activity) {
// noinspection unchecked
return (T) context;
} else {
Contextwrapper contextwrapper = (Contextwrapper) context;
Context baseContext = contextwrapper.getBaseContext();
if(baseContext == null) {
throw new IllegalStateException("Activity was not found as base context of view!");
}
return findActivity(baseContext);
}
}
项目:UsoppBubble
文件:Utils.java
/***
* 得到活动对象
*
* @param context 上下文
* @return Activity activity
*/
public static Activity scanForActivity(@NonNull Context context) {
if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof Contextwrapper) {
return scanForActivity(((Contextwrapper) context).getBaseContext());
}
return null;
}
项目:GitHub
文件:RequestManagerRetriever.java
public RequestManager get(Context context) {
if (context == null) {
throw new IllegalArgumentException("You cannot start a load on a null Context");
} else if (Util.isOnMainThread() && !(context instanceof Application)) {
if (context instanceof FragmentActivity) {
return get((FragmentActivity) context);
} else if (context instanceof Activity) {
return get((Activity) context);
} else if (context instanceof Contextwrapper) {
return get(((Contextwrapper) context).getBaseContext());
}
}
return getApplicationManager(context);
}
项目:GitHub
文件:RequestManagerRetriever.java
项目:GitHub
文件:RequestManagerRetrieverTest.java
@Test
public void testHandlesContextwrappersForActivities() {
RetrieverHarness harness = new DefaultRetrieverHarness();
RequestManager requestManager = harness.doGet();
Contextwrapper contextwrapper = new Contextwrapper(harness.getController().get());
assertEquals(requestManager,retriever.get(contextwrapper));
}
项目:GitHub
文件:Utils.java
/**
* Resolves bug #161. Necessary when {@code theme} attribute is used in the layout.
* Used by {@code FlexibleAdapter.getStickyHeaderContainer()} method.
*/
//Todo: review comment
public static Activity scanForActivity(Context context) {
if (context instanceof Activity)
return (Activity) context;
else if (context instanceof Contextwrapper)
return scanForActivity(((Contextwrapper) context).getBaseContext());
return null;
}
项目:GitHub
文件:NucleusLayout.java
/**
* Returns the unwrapped activity of the view or throws an exception.
*
* @return an unwrapped activity
*/
public Activity getActivity() {
Context context = getContext();
while (!(context instanceof Activity) && context instanceof Contextwrapper)
context = ((Contextwrapper) context).getBaseContext();
if (!(context instanceof Activity))
throw new IllegalStateException("Expected an activity context,got " + context.getClass().getSimpleName());
return (Activity) context;
}
项目:GitHub
文件:RequestManagerRetrieverTest.java
项目:ListVideoPlayer
文件:ListVideoUtils.java
项目:GitHub
文件:ActivityListenerManager.java
/**
* If given context is an instance of ListenableActivity then creates new instance of
* WeakReferenceActivityListenerAdapter and adds it to activity's listeners
*/
public static void register(
ActivityListener activityListener,Context context) {
if (!(context instanceof ListenableActivity) && context instanceof Contextwrapper) {
context = ((Contextwrapper) context).getBaseContext();
}
if (context instanceof ListenableActivity) {
ListenableActivity listenableActivity = (ListenableActivity) context;
Listener listener = new Listener(activityListener);
listenableActivity.addActivityListener(listener);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。