Search in sources :

Example 6 with ContextWrapper

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

the class ShadowApplication method assertNoBroadcastListenersOfActionRegistered.

public void assertNoBroadcastListenersOfActionRegistered(ContextWrapper context, String action) {
    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 : ContextWrapper(android.content.ContextWrapper)

Example 7 with ContextWrapper

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

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 8 with ContextWrapper

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

the class LockSettingsStorageTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    mStorageDir = new File(getContext().getFilesDir(), "locksettings");
    mDb = getContext().getDatabasePath("locksettings.db");
    assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
    assertTrue(FileUtils.deleteContents(mStorageDir));
    assertTrue(!mDb.exists() || mDb.delete());
    final Context ctx = getContext();
    setContext(new ContextWrapper(ctx) {

        @Override
        public Object getSystemService(String name) {
            if (USER_SERVICE.equals(name)) {
                return new UserManager(ctx, null) {

                    @Override
                    public UserInfo getProfileParent(int userHandle) {
                        if (userHandle == 2) {
                            // User 2 is a profile of user 1.
                            return new UserInfo(1, "name", 0);
                        }
                        if (userHandle == 3) {
                            // User 3 is a profile of user 0.
                            return new UserInfo(0, "name", 0);
                        }
                        return null;
                    }
                };
            }
            return super.getSystemService(name);
        }
    });
    mStorage = new LockSettingsStorage(getContext(), new LockSettingsStorage.Callback() {

        @Override
        public void initialize(SQLiteDatabase db) {
            mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
        }
    }) {

        @Override
        String getLockPatternFilename(int userId) {
            return new File(mStorageDir, super.getLockPatternFilename(userId).replace('/', '-')).getAbsolutePath();
        }

        @Override
        String getLockPasswordFilename(int userId) {
            return new File(mStorageDir, super.getLockPasswordFilename(userId).replace('/', '-')).getAbsolutePath();
        }

        @Override
        String getChildProfileLockFile(int userId) {
            return new File(mStorageDir, super.getChildProfileLockFile(userId).replace('/', '-')).getAbsolutePath();
        }
    };
}
Also used : Context(android.content.Context) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) UserManager(android.os.UserManager) UserInfo(android.content.pm.UserInfo) File(java.io.File) ContextWrapper(android.content.ContextWrapper)

Example 9 with ContextWrapper

use of android.content.ContextWrapper in project ListenerMusicPlayer by hefuyicoder.

the class MusicPlayer method bindToService.

public static ServiceToken bindToService(final Context context, final ServiceConnection callback) {
    Activity realActivity = ((Activity) context).getParent();
    if (realActivity == null) {
        realActivity = (Activity) context;
    }
    final ContextWrapper contextWrapper = new ContextWrapper(realActivity);
    contextWrapper.startService(new Intent(contextWrapper, MusicService.class));
    final ServiceBinder binder = new ServiceBinder(callback, contextWrapper.getApplicationContext());
    if (contextWrapper.bindService(new Intent().setClass(contextWrapper, MusicService.class), binder, 0)) {
        mConnectionMap.put(contextWrapper, binder);
        return new ServiceToken(contextWrapper);
    }
    return null;
}
Also used : Activity(android.app.Activity) Intent(android.content.Intent) ContextWrapper(android.content.ContextWrapper)

Example 10 with ContextWrapper

use of android.content.ContextWrapper in project ListenerMusicPlayer by hefuyicoder.

the class MusicPlayer method unbindFromService.

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;
    }
}
Also used : ContextWrapper(android.content.ContextWrapper)

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