Search in sources :

Example 81 with ContextWrapper

use of android.content.ContextWrapper in project CameraKit-Android by flurgle.

the class CameraKitView method onStart.

public void onStart() {
    if (isInEditMode()) {
        return;
    }
    List<String> missingPermissions = getMissingPermissions();
    if (Build.VERSION.SDK_INT >= 23 && missingPermissions.size() > 0) {
        Activity activity = null;
        Context context = getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                activity = (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        if (activity != null) {
            List<String> requestPermissions = new ArrayList<>();
            List<String> rationalePermissions = new ArrayList<>();
            for (String permission : missingPermissions) {
                if (!activity.shouldShowRequestPermissionRationale(permission)) {
                    requestPermissions.add(permission);
                } else {
                    rationalePermissions.add(permission);
                }
            }
            if (requestPermissions.size() > 0) {
                activity.requestPermissions(requestPermissions.toArray(new String[requestPermissions.size()]), PERMISSION_REQUEST_CODE);
            }
            if (rationalePermissions.size() > 0 && mPermissionsListener != null) {
                mPermissionsListener.onPermissionsFailure();
            }
        }
        return;
    }
    if (mPermissionsListener != null) {
        mPermissionsListener.onPermissionsSuccess();
    }
    setFlash(mFlash);
    setImageMegaPixels(mImageMegaPixels);
    cameraFacing = getFacing() == CameraKit.FACING_BACK ? CameraFacing.BACK : CameraFacing.FRONT;
    mCameraPreview.start(cameraFacing);
}
Also used : Context(android.content.Context) ArrayList(java.util.ArrayList) Activity(android.app.Activity) ContextWrapper(android.content.ContextWrapper)

Example 82 with ContextWrapper

use of android.content.ContextWrapper in project atlas by alibaba.

the class ActivityTaskMgr method updateBundleActivityResource.

public void updateBundleActivityResource(String bundleName) {
    for (int x = 0; x < activityList.size(); x++) {
        WeakReference<Activity> ref = activityList.get(x);
        if (ref != null && ref.get() != null && !ref.get().isFinishing()) {
            Activity activity = ref.get();
            try {
                if (Build.VERSION.SDK_INT >= 17 && activity.getResources() != RuntimeVariables.delegateResources) {
                    Field mResourcesField = Activity.class.getSuperclass().getDeclaredField("mResources");
                    mResourcesField.setAccessible(true);
                    mResourcesField.set(activity, RuntimeVariables.delegateResources);
                }
                Field mThemeField = Activity.class.getSuperclass().getDeclaredField("mTheme");
                mThemeField.setAccessible(true);
                Object mTheme = mThemeField.get(activity);
                Field mReferenceResource = mTheme.getClass().getDeclaredField("this$0");
                mReferenceResource.setAccessible(true);
                if (mReferenceResource.get(mTheme) != RuntimeVariables.delegateResources) {
                    mThemeField.set(activity, null);
                }
                Class TintContextWrapper = Class.forName("android.support.v7.widget.TintContextWrapper");
                Field tintWrapperField = TintContextWrapper.getDeclaredField("sCache");
                tintWrapperField.setAccessible(true);
                ArrayList<WeakReference<Object>> sCache = (ArrayList<WeakReference<Object>>) tintWrapperField.get(TintContextWrapper);
                if (sCache != null) {
                    for (int n = 0; n < sCache.size(); n++) {
                        final WeakReference<Object> wrappRef = sCache.get(n);
                        final Object wrapper = wrappRef != null ? wrappRef.get() : null;
                        if (wrapper != null && ((ContextWrapper) wrapper).getBaseContext() == activity) {
                            Field mTintResourcesField = TintContextWrapper.getDeclaredField("mResources");
                            mTintResourcesField.setAccessible(true);
                            mTintResourcesField.set(wrapper, RuntimeVariables.delegateResources);
                            break;
                        }
                    }
                }
            } catch (Throwable e) {
            // e.printStackTrace();
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) WeakReference(java.lang.ref.WeakReference) ArrayList(java.util.ArrayList) Activity(android.app.Activity) ContextWrapper(android.content.ContextWrapper)

Example 83 with ContextWrapper

use of android.content.ContextWrapper in project robolectric by robolectric.

the class ShadowInstrumentation method assertNoBroadcastListenersOfActionRegistered.

void assertNoBroadcastListenersOfActionRegistered(ContextWrapper context, String action) {
    synchronized (registeredReceivers) {
        for (Wrapper registeredReceiver : registeredReceivers) {
            if (registeredReceiver.context == context.getBaseContext()) {
                Iterator<String> actions = registeredReceiver.intentFilter.actionsIterator();
                while (actions.hasNext()) {
                    if (actions.next().equals(action)) {
                        RuntimeException e = new IllegalStateException("Unexpected BroadcastReceiver on " + context + " with action " + action + " " + registeredReceiver.broadcastReceiver + " that was originally registered here:");
                        e.setStackTrace(registeredReceiver.exception.getStackTrace());
                        throw e;
                    }
                }
            }
        }
    }
}
Also used : Wrapper(org.robolectric.shadows.ShadowApplication.Wrapper) ContextWrapper(android.content.ContextWrapper)

Example 84 with ContextWrapper

use of android.content.ContextWrapper in project robolectric by robolectric.

the class ShadowAlertDialogTest method shouldCallTheClickListenerOfTheCheckedAdapterInASingleChoiceDialog.

@Test
public void shouldCallTheClickListenerOfTheCheckedAdapterInASingleChoiceDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextWrapper(context));
    TestDialogOnClickListener listener = new TestDialogOnClickListener();
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    ArrayAdapter<Integer> arrayAdapter = new ArrayAdapter<>(context, R.layout.main, R.id.title, list);
    builder.setSingleChoiceItems(arrayAdapter, 1, listener);
    AlertDialog alert = builder.create();
    alert.show();
    ShadowAlertDialog shadowAlertDialog = shadowOf(alert);
    shadowAlertDialog.clickOnItem(0);
    assertThat(listener.transcript).containsExactly("clicked on 0");
    listener.transcript.clear();
    shadowAlertDialog.clickOnItem(1);
    assertThat(listener.transcript).containsExactly("clicked on 1");
}
Also used : AlertDialog(android.app.AlertDialog) ArrayList(java.util.ArrayList) ContextWrapper(android.content.ContextWrapper) ArrayAdapter(android.widget.ArrayAdapter) Test(org.junit.Test)

Example 85 with ContextWrapper

use of android.content.ContextWrapper in project robolectric by robolectric.

the class ShadowAlertDialogTest method testBuilderWithItemArrayViaResourceId.

@Test
public void testBuilderWithItemArrayViaResourceId() {
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextWrapper(context));
    builder.setTitle("title");
    builder.setItems(R.array.alertDialogTestItems, new TestDialogOnClickListener());
    AlertDialog alert = builder.create();
    alert.show();
    assertThat(alert.isShowing()).isTrue();
    ShadowAlertDialog shadowAlertDialog = shadowOf(alert);
    assertThat(shadowAlertDialog.getTitle().toString()).isEqualTo("title");
    assertThat(shadowAlertDialog.getItems().length).isEqualTo(2);
    assertThat(shadowAlertDialog.getItems()[0].toString()).isEqualTo("Aloha");
    assertThat(shadowOf(ShadowAlertDialog.getLatestAlertDialog())).isSameInstanceAs(shadowAlertDialog);
    assertThat(ShadowAlertDialog.getLatestAlertDialog()).isSameInstanceAs(alert);
}
Also used : AlertDialog(android.app.AlertDialog) ContextWrapper(android.content.ContextWrapper) Test(org.junit.Test)

Aggregations

ContextWrapper (android.content.ContextWrapper)109 Context (android.content.Context)37 Activity (android.app.Activity)25 File (java.io.File)16 Test (org.junit.Test)13 Intent (android.content.Intent)11 MockContentResolver (android.test.mock.MockContentResolver)11 PackageManager (android.content.pm.PackageManager)10 View (android.view.View)10 Resources (android.content.res.Resources)9 IOException (java.io.IOException)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)6 UserManager (android.os.UserManager)6 LayoutInflater (android.view.LayoutInflater)6 ThemePreferenceController (com.android.settings.display.ThemePreferenceController)6 OverlayManager (com.android.settings.display.ThemePreferenceController.OverlayManager)6 ArrayList (java.util.ArrayList)6 AppWidgetHostView (android.appwidget.AppWidgetHostView)5 JsonParser (com.google.gson.JsonParser)5