Search in sources :

Example 61 with ContextWrapper

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

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

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

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

use of android.content.ContextWrapper in project instructure-android by instructure.

the class CameraView method requestPermissions.

private void requestPermissions(boolean requestCamera, boolean requestAudio) {
    Activity activity = null;
    Context context = getContext();
    while (context instanceof ContextWrapper) {
        if (context instanceof Activity) {
            activity = (Activity) context;
        }
        context = ((ContextWrapper) context).getBaseContext();
    }
    List<String> permissions = new ArrayList<>();
    if (requestCamera)
        permissions.add(Manifest.permission.CAMERA);
    if (requestAudio)
        permissions.add(Manifest.permission.RECORD_AUDIO);
    if (activity != null) {
        ActivityCompat.requestPermissions(activity, permissions.toArray(new String[permissions.size()]), CameraKit.Constants.PERMISSION_REQUEST_CAMERA);
    }
}
Also used : Context(android.content.Context) ArrayList(java.util.ArrayList) Activity(android.app.Activity) ContextWrapper(android.content.ContextWrapper)

Example 64 with ContextWrapper

use of android.content.ContextWrapper in project Shuttle by timusus.

the class MusicServiceConnectionUtils method bindToService.

/**
 * @param context  The {@link Context} to use
 * @param callback The {@link ServiceConnection} to use
 * @return The new instance of {@link ServiceToken}
 */
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);
    if (contextWrapper.bindService(new Intent().setClass(contextWrapper, MusicService.class), binder, 0)) {
        connectionMap.put(contextWrapper, binder);
        return new ServiceToken(contextWrapper);
    }
    return null;
}
Also used : MusicService(com.simplecity.amp_library.playback.MusicService) Activity(android.app.Activity) Intent(android.content.Intent) ContextWrapper(android.content.ContextWrapper)

Example 65 with ContextWrapper

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

the class MusicUtils method unbindFromService.

/**
 * @param token The {@link ServiceToken} to unbind from
 */
public static void unbindFromService(final ServiceToken token) {
    if (token == null) {
        return;
    }
    final ContextWrapper mContextWrapper = token.mWrappedContext;
    try {
        final ServiceBinder mBinder = mConnectionMap.remove(mContextWrapper);
        if (mBinder == null) {
            return;
        }
        mContextWrapper.unbindService(mBinder);
        if (mConnectionMap.isEmpty()) {
            musicPlaybackService = null;
        }
    } catch (Throwable ignored) {
        LOG.error(ignored.getMessage(), ignored);
    }
}
Also used : ContextWrapper(android.content.ContextWrapper)

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