use of android.util.Pair in project android_frameworks_base by ParanoidAndroid.
the class DatabaseGeneralTest method testDefaultDatabaseErrorHandler.
@LargeTest
public void testDefaultDatabaseErrorHandler() {
DefaultDatabaseErrorHandler errorHandler = new DefaultDatabaseErrorHandler();
// close the database. and call corruption handler.
// it should delete the database file.
File dbfile = new File(mDatabase.getPath());
mDatabase.close();
assertFalse(mDatabase.isOpen());
assertTrue(dbfile.exists());
try {
errorHandler.onCorruption(mDatabase);
assertFalse(dbfile.exists());
} catch (Exception e) {
fail("unexpected");
}
// create an in-memory database. and corruption handler shouldn't try to delete it
SQLiteDatabase memoryDb = SQLiteDatabase.openOrCreateDatabase(":memory:", null);
assertNotNull(memoryDb);
memoryDb.close();
assertFalse(memoryDb.isOpen());
try {
errorHandler.onCorruption(memoryDb);
} catch (Exception e) {
fail("unexpected");
}
// create a database, keep it open, call corruption handler. database file should be deleted
SQLiteDatabase dbObj = SQLiteDatabase.openOrCreateDatabase(mDatabase.getPath(), null);
assertTrue(dbfile.exists());
assertNotNull(dbObj);
assertTrue(dbObj.isOpen());
try {
errorHandler.onCorruption(dbObj);
assertFalse(dbfile.exists());
} catch (Exception e) {
fail("unexpected");
}
// create a database, attach 2 more databases to it
// attached database # 1: ":memory:"
// attached database # 2: mDatabase.getPath() + "1";
// call corruption handler. database files including the one for attached database # 2
// should be deleted
String attachedDb1File = mDatabase.getPath() + "1";
dbObj = SQLiteDatabase.openOrCreateDatabase(mDatabase.getPath(), null);
dbObj.execSQL("ATTACH DATABASE ':memory:' as memoryDb");
dbObj.execSQL("ATTACH DATABASE '" + attachedDb1File + "' as attachedDb1");
assertTrue(dbfile.exists());
assertTrue(new File(attachedDb1File).exists());
assertNotNull(dbObj);
assertTrue(dbObj.isOpen());
List<Pair<String, String>> attachedDbs = dbObj.getAttachedDbs();
try {
errorHandler.onCorruption(dbObj);
assertFalse(dbfile.exists());
assertFalse(new File(attachedDb1File).exists());
} catch (Exception e) {
fail("unexpected");
}
// same as above, except this is a bit of stress testing. attach 5 database files
// and make sure they are all removed.
int N = 5;
ArrayList<String> attachedDbFiles = new ArrayList<String>(N);
for (int i = 0; i < N; i++) {
attachedDbFiles.add(mDatabase.getPath() + i);
}
dbObj = SQLiteDatabase.openOrCreateDatabase(mDatabase.getPath(), null);
dbObj.execSQL("ATTACH DATABASE ':memory:' as memoryDb");
for (int i = 0; i < N; i++) {
dbObj.execSQL("ATTACH DATABASE '" + attachedDbFiles.get(i) + "' as attachedDb" + i);
}
assertTrue(dbfile.exists());
for (int i = 0; i < N; i++) {
assertTrue(new File(attachedDbFiles.get(i)).exists());
}
assertNotNull(dbObj);
assertTrue(dbObj.isOpen());
attachedDbs = dbObj.getAttachedDbs();
try {
errorHandler.onCorruption(dbObj);
assertFalse(dbfile.exists());
for (int i = 0; i < N; i++) {
assertFalse(new File(attachedDbFiles.get(i)).exists());
}
} catch (Exception e) {
fail("unexpected");
}
}
use of android.util.Pair in project android_frameworks_base by ParanoidAndroid.
the class MediaArtistNativeHelper method findVideoResolution.
/**
* Calculates video resolution for output clip
* based on clip's height and aspect ratio of storyboard
*
* @param aspectRatio The aspect ratio of story board
* @param height The height of clip
*
* @return The video resolution
*/
private int findVideoResolution(int aspectRatio, int height) {
final Pair<Integer, Integer>[] resolutions;
final Pair<Integer, Integer> maxResolution;
int retValue = VideoFrameSize.SIZE_UNDEFINED;
switch(aspectRatio) {
case MediaProperties.ASPECT_RATIO_3_2:
if (height == MediaProperties.HEIGHT_480)
retValue = VideoFrameSize.NTSC;
else if (height == MediaProperties.HEIGHT_720)
retValue = VideoFrameSize.W720p;
break;
case MediaProperties.ASPECT_RATIO_16_9:
if (height == MediaProperties.HEIGHT_480)
retValue = VideoFrameSize.WVGA16x9;
else if (height == MediaProperties.HEIGHT_720)
retValue = VideoFrameSize.V720p;
else if (height == MediaProperties.HEIGHT_1080)
retValue = VideoFrameSize.V1080p;
break;
case MediaProperties.ASPECT_RATIO_4_3:
if (height == MediaProperties.HEIGHT_480)
retValue = VideoFrameSize.VGA;
else if (height == MediaProperties.HEIGHT_720)
retValue = VideoFrameSize.S720p;
break;
case MediaProperties.ASPECT_RATIO_5_3:
if (height == MediaProperties.HEIGHT_480)
retValue = VideoFrameSize.WVGA;
break;
case MediaProperties.ASPECT_RATIO_11_9:
if (height == MediaProperties.HEIGHT_144)
retValue = VideoFrameSize.QCIF;
else if (height == MediaProperties.HEIGHT_288)
retValue = VideoFrameSize.CIF;
break;
}
if (retValue == VideoFrameSize.SIZE_UNDEFINED) {
resolutions = MediaProperties.getSupportedResolutions(mVideoEditor.getAspectRatio());
// Get the highest resolution
maxResolution = resolutions[resolutions.length - 1];
retValue = findVideoResolution(mVideoEditor.getAspectRatio(), maxResolution.second);
}
return retValue;
}
use of android.util.Pair in project android_frameworks_base by ParanoidAndroid.
the class UserTile method queryForUserInformation.
private void queryForUserInformation() {
Context currentUserContext = null;
UserInfo userInfo = null;
try {
userInfo = ActivityManagerNative.getDefault().getCurrentUser();
currentUserContext = mContext.createPackageContextAsUser("android", 0, new UserHandle(userInfo.id));
} catch (NameNotFoundException e) {
Log.e(TAG, "Couldn't create user context", e);
throw new RuntimeException(e);
} catch (RemoteException e) {
Log.e(TAG, "Couldn't get user info", e);
}
final int userId = userInfo.id;
final String userName = userInfo.name;
final Context context = currentUserContext;
mUserInfoTask = new AsyncTask<Void, Void, Pair<String, Drawable>>() {
@Override
protected Pair<String, Drawable> doInBackground(Void... params) {
try {
// The system needs some time to change the picture, if we try to load it when we receive the broadcast, we will load the old one
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
// Fall back to the UserManager nickname if we can't read the name from the local
// profile below.
String name = userName;
Drawable avatar = null;
Bitmap rawAvatar = um.getUserIcon(userId);
if (rawAvatar != null) {
avatar = new BitmapDrawable(mContext.getResources(), rawAvatar);
} else {
avatar = mContext.getResources().getDrawable(R.drawable.ic_qs_default_user);
}
// usually valid
if (um.getUsers().size() <= 1) {
// Try and read the display name from the local profile
final Cursor cursor = context.getContentResolver().query(Profile.CONTENT_URI, new String[] { Phone._ID, Phone.DISPLAY_NAME }, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
}
return new Pair<String, Drawable>(name, avatar);
}
@Override
protected void onPostExecute(Pair<String, Drawable> result) {
super.onPostExecute(result);
setUserTileInfo(result.first, result.second);
mUserInfoTask = null;
}
};
mUserInfoTask.execute();
}
use of android.util.Pair in project android_frameworks_base by ParanoidAndroid.
the class PhoneStatusBar method recreateStatusBar.
private void recreateStatusBar() {
mRecreating = true;
mStatusBarContainer.removeAllViews();
// extract icons from the soon-to-be recreated viewgroup.
int nIcons = mStatusIcons.getChildCount();
ArrayList<StatusBarIcon> icons = new ArrayList<StatusBarIcon>(nIcons);
ArrayList<String> iconSlots = new ArrayList<String>(nIcons);
for (int i = 0; i < nIcons; i++) {
StatusBarIconView iconView = (StatusBarIconView) mStatusIcons.getChildAt(i);
icons.add(iconView.getStatusBarIcon());
iconSlots.add(iconView.getStatusBarSlot());
}
// extract notifications.
int nNotifs = mNotificationData.size();
ArrayList<Pair<IBinder, StatusBarNotification>> notifications = new ArrayList<Pair<IBinder, StatusBarNotification>>(nNotifs);
copyNotifications(notifications, mNotificationData);
mNotificationData.clear();
makeStatusBarView();
repositionNavigationBar();
mNavigationBarView.updateResources();
// recreate StatusBarIconViews.
for (int i = 0; i < nIcons; i++) {
StatusBarIcon icon = icons.get(i);
String slot = iconSlots.get(i);
addIcon(slot, i, i, icon);
}
// recreate notifications.
for (int i = 0; i < nNotifs; i++) {
Pair<IBinder, StatusBarNotification> notifData = notifications.get(i);
addNotificationViews(notifData.first, notifData.second);
}
setAreThereNotifications();
mStatusBarContainer.addView(mStatusBarWindow);
updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
mRecreating = false;
}
use of android.util.Pair in project android_frameworks_base by ParanoidAndroid.
the class QuickSettings method queryForUserInformation.
private void queryForUserInformation() {
Context currentUserContext = null;
UserInfo userInfo = null;
try {
userInfo = ActivityManagerNative.getDefault().getCurrentUser();
currentUserContext = mContext.createPackageContextAsUser("android", 0, new UserHandle(userInfo.id));
} catch (NameNotFoundException e) {
Log.e(TAG, "Couldn't create user context", e);
throw new RuntimeException(e);
} catch (RemoteException e) {
Log.e(TAG, "Couldn't get user info", e);
}
final int userId = userInfo.id;
final String userName = userInfo.name;
final Context context = currentUserContext;
mUserInfoTask = new AsyncTask<Void, Void, Pair<String, Drawable>>() {
@Override
protected Pair<String, Drawable> doInBackground(Void... params) {
final UserManager um = UserManager.get(mContext);
// Fall back to the UserManager nickname if we can't read the name from the local
// profile below.
String name = userName;
Drawable avatar = null;
Bitmap rawAvatar = um.getUserIcon(userId);
if (rawAvatar != null) {
avatar = new BitmapDrawable(mContext.getResources(), rawAvatar);
} else {
avatar = mContext.getResources().getDrawable(R.drawable.ic_qs_default_user);
mUseDefaultAvatar = true;
}
// usually valid
if (um.getUsers().size() <= 1) {
// Try and read the display name from the local profile
final Cursor cursor = context.getContentResolver().query(Profile.CONTENT_URI, new String[] { Phone._ID, Phone.DISPLAY_NAME }, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
}
return new Pair<String, Drawable>(name, avatar);
}
@Override
protected void onPostExecute(Pair<String, Drawable> result) {
super.onPostExecute(result);
mModel.setUserTileInfo(result.first, result.second);
mUserInfoTask = null;
}
};
mUserInfoTask.execute();
}
Aggregations