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();
}
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();
}
};
}
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);
}
}
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;
}
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);
}
}
Aggregations