Search in sources :

Example 16 with ContextWrapper

use of android.content.ContextWrapper in project android_frameworks_base by AOSPA.

the class TestContext method createStorageTestContext.

/**
     * Returns a Context configured with test provider for authority.
     */
static Context createStorageTestContext(Context context, String authority) {
    final MockContentResolver testResolver = new MockContentResolver();
    TestContentProvider provider = new TestContentProvider();
    testResolver.addProvider(authority, provider);
    return new ContextWrapper(context) {

        @Override
        public ContentResolver getContentResolver() {
            return testResolver;
        }
    };
}
Also used : MockContentResolver(android.test.mock.MockContentResolver) ContextWrapper(android.content.ContextWrapper)

Example 17 with ContextWrapper

use of android.content.ContextWrapper in project android_frameworks_base by AOSPA.

the class ModelTest method setupTestContext.

private void setupTestContext() {
    final MockContentResolver resolver = new MockContentResolver();
    context = new ContextWrapper(getContext()) {

        @Override
        public ContentResolver getContentResolver() {
            return resolver;
        }
    };
    provider = new TestContentProvider();
    resolver.addProvider(AUTHORITY, provider);
}
Also used : MockContentResolver(android.test.mock.MockContentResolver) ContextWrapper(android.content.ContextWrapper) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver)

Example 18 with ContextWrapper

use of android.content.ContextWrapper in project android_frameworks_base by DirtyUnicorns.

the class Dialog method getAssociatedActivity.

/**
     * @return The activity associated with this dialog, or null if there is no associated activity.
     */
private ComponentName getAssociatedActivity() {
    Activity activity = mOwnerActivity;
    Context context = getContext();
    while (activity == null && context != null) {
        if (context instanceof Activity) {
            // found it!
            activity = (Activity) context;
        } else {
            context = (context instanceof ContextWrapper) ? // unwrap one level
            ((ContextWrapper) context).getBaseContext() : // done
            null;
        }
    }
    return activity == null ? null : activity.getComponentName();
}
Also used : Context(android.content.Context) ContextWrapper(android.content.ContextWrapper)

Example 19 with ContextWrapper

use of android.content.ContextWrapper in project android_packages_apps_Settings by LineageOS.

the class ThemePreferenceControllerTest method setup.

@Before
public void setup() {
    mMockOverlayManager = mock(OverlayManager.class);
    mMockPackageManager = mock(PackageManager.class);
    mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {

        @Override
        public PackageManager getPackageManager() {
            return mMockPackageManager;
        }
    };
    mPreferenceController = new ThemePreferenceController(mContext, mMockOverlayManager);
}
Also used : OverlayManager(com.android.settings.display.ThemePreferenceController.OverlayManager) ThemePreferenceController(com.android.settings.display.ThemePreferenceController) PackageManager(android.content.pm.PackageManager) ContextWrapper(android.content.ContextWrapper) Before(org.junit.Before)

Example 20 with ContextWrapper

use of android.content.ContextWrapper in project BaseProject by fly803.

the class SharedPreferencesUtils method getSharedPreferencesFromPath.

public static String getSharedPreferencesFromPath(Context context, String dirName, String sharePreName, String key, String defaultValue) {
    SharedPreferences mySharedPreferences = null;
    try {
        Field field;
        // 获取ContextWrapper对象中的mBase变量。该变量保存了ContextImpl对象
        field = ContextWrapper.class.getDeclaredField("mBase");
        field.setAccessible(true);
        // 获取mBase变量
        Object obj = field.get(context);
        // 获取ContextImpl。mPreferencesDir变量,该变量保存了数据文件的保存路径
        field = obj.getClass().getDeclaredField("mPreferencesDir");
        field.setAccessible(true);
        // 创建自定义路径
        File file;
        if (isHaveSD()) {
            file = new File(SDCARDDIR, dirName);
        } else {
            file = new File(context.getCacheDir().getAbsolutePath() + File.separator, dirName);
        }
        // 修改mPreferencesDir变量的值
        field.set(obj, file);
        mySharedPreferences = context.getSharedPreferences(sharePreName, Context.MODE_PRIVATE);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return mySharedPreferences.getString(key, defaultValue);
}
Also used : Field(java.lang.reflect.Field) SharedPreferences(android.content.SharedPreferences) ContextWrapper(android.content.ContextWrapper) File(java.io.File)

Aggregations

ContextWrapper (android.content.ContextWrapper)108 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 Resources (android.content.res.Resources)9 View (android.view.View)9 IOException (java.io.IOException)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)6 UserManager (android.os.UserManager)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 LayoutInflater (android.view.LayoutInflater)5 JsonParser (com.google.gson.JsonParser)5