use of android.os.Build in project acra by ACRA.
the class ReflectionCollector method collect.
@NonNull
@Override
Element collect(ReportField reportField, ReportBuilder reportBuilder) {
ComplexElement result = new ComplexElement();
try {
switch(reportField) {
case BUILD:
collectConstants(Build.class, result);
JSONObject version = new JSONObject();
collectConstants(Build.VERSION.class, version);
result.put("VERSION", version);
break;
case BUILD_CONFIG:
try {
collectConstants(getBuildConfigClass(), result);
} catch (ClassNotFoundException e) {
//already logged in getBuildConfigClass
}
break;
case ENVIRONMENT:
collectStaticGettersResults(Environment.class, result);
break;
default:
//will not happen if used correctly
throw new IllegalArgumentException();
}
} catch (JSONException e) {
ACRA.log.w("Couldn't collect constants", e);
return ACRAConstants.NOT_AVAILABLE;
}
return result;
}
use of android.os.Build in project mobile-center-sdk-android by Microsoft.
the class DistributeDownloadTest method notifyThenRestartAppTwice.
@Test
@PrepareForTest(Uri.class)
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void notifyThenRestartAppTwice() throws Exception {
/* Simulate async task. */
waitDownloadTask();
/* Process fake download completion, should not interfere and will be ignored. */
{
Intent completionIntent = mock(Intent.class);
when(completionIntent.getAction()).thenReturn(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
when(completionIntent.getLongExtra(eq(EXTRA_DOWNLOAD_ID), anyLong())).thenReturn(404L);
new DownloadManagerReceiver().onReceive(mContext, completionIntent);
waitCheckDownloadTask();
verify(mDownloadManager, never()).query(any(DownloadManager.Query.class));
}
/* Process download completion with the real download identifier. */
completeDownload();
Cursor cursor = mockSuccessCursor();
Intent installIntent = mockInstallIntent();
/* In background. */
Distribute.getInstance().onActivityPaused(mActivity);
/* Mock notification. */
when(mPackageManager.getApplicationInfo(mContext.getPackageName(), 0)).thenReturn(mock(ApplicationInfo.class));
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
Notification.Builder notificationBuilder = mockNotificationBuilderChain();
when(notificationBuilder.build()).thenReturn(mock(Notification.class));
/* Simulate task. */
waitCheckDownloadTask();
/* Verify notification. */
verify(mContext, never()).startActivity(installIntent);
verifyStatic();
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
verify(notificationBuilder).build();
verify(notificationBuilder, never()).getNotification();
verify(mNotificationManager).notify(eq(DistributeUtils.getNotificationId()), any(Notification.class));
verifyNoMoreInteractions(mNotificationManager);
verify(cursor).close();
/* Launch app should pop install U.I. and cancel notification. */
when(mActivity.getPackageManager()).thenReturn(mPackageManager);
Intent launcherIntent = mock(Intent.class);
when(mPackageManager.getLaunchIntentForPackage(anyString())).thenReturn(launcherIntent);
ComponentName launcher = mock(ComponentName.class);
when(launcherIntent.resolveActivity(mPackageManager)).thenReturn(launcher);
when(launcher.getClassName()).thenReturn(mActivity.getClass().getName());
restartActivity();
/* Wait again. */
waitCheckDownloadTask();
/* Verify U.I shown after restart and workflow completed. */
verify(mContext).startActivity(installIntent);
verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
verifyStatic();
/* Verify workflow completed. */
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* Verify however downloaded file was kept. */
verifyStatic(never());
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verify(mDownloadManager, never()).remove(DOWNLOAD_ID);
/* Verify second download (restart app again) cleans first one. */
when(mDownloadManager.enqueue(mDownloadRequest)).thenReturn(DOWNLOAD_ID + 1);
restartActivity();
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), clickListener.capture());
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
waitDownloadTask();
/* Verify new download id in storage. */
verifyStatic();
PreferencesStorage.putLong(PREFERENCE_KEY_DOWNLOAD_ID, DOWNLOAD_ID + 1);
/* Verify previous download removed. */
verify(mDownloadManager).remove(DOWNLOAD_ID);
/* Notification already canceled so no more call, i.e. only once. */
verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
}
use of android.os.Build in project mobile-center-sdk-android by Microsoft.
the class DistributeDownloadTest method notifyThenRestartAppTwice.
@Test
@PrepareForTest(Uri.class)
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void notifyThenRestartAppTwice() throws Exception {
/* Simulate async task. */
waitDownloadTask();
/* Process fake download completion, should not interfere and will be ignored. */
{
Intent completionIntent = mock(Intent.class);
when(completionIntent.getAction()).thenReturn(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
when(completionIntent.getLongExtra(eq(EXTRA_DOWNLOAD_ID), anyLong())).thenReturn(404L);
new DownloadManagerReceiver().onReceive(mContext, completionIntent);
waitCheckDownloadTask();
verify(mDownloadManager, never()).query(any(DownloadManager.Query.class));
}
/* Process download completion with the real download identifier. */
completeDownload();
Cursor cursor = mockSuccessCursor();
Intent installIntent = mockInstallIntent();
/* In background. */
Distribute.getInstance().onActivityPaused(mActivity);
/* Mock notification. */
when(mPackageManager.getApplicationInfo(mContext.getPackageName(), 0)).thenReturn(mock(ApplicationInfo.class));
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
Notification.Builder notificationBuilder = mockNotificationBuilderChain();
when(notificationBuilder.build()).thenReturn(mock(Notification.class));
/* Simulate task. */
waitCheckDownloadTask();
/* Verify notification. */
verify(mContext, never()).startActivity(installIntent);
verifyStatic();
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
verify(notificationBuilder).build();
verify(notificationBuilder, never()).getNotification();
verify(mNotificationManager).notify(eq(DistributeUtils.getNotificationId()), any(Notification.class));
verifyNoMoreInteractions(mNotificationManager);
verify(cursor).close();
/* Launch app should pop install U.I. and cancel notification. */
when(mActivity.getPackageManager()).thenReturn(mPackageManager);
Intent launcherIntent = mock(Intent.class);
when(mPackageManager.getLaunchIntentForPackage(anyString())).thenReturn(launcherIntent);
ComponentName launcher = mock(ComponentName.class);
when(launcherIntent.resolveActivity(mPackageManager)).thenReturn(launcher);
when(launcher.getClassName()).thenReturn(mActivity.getClass().getName());
restartActivity();
/* Wait again. */
waitCheckDownloadTask();
/* Verify U.I shown after restart and workflow completed. */
verify(mContext).startActivity(installIntent);
verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
verifyStatic();
/* Verify workflow completed. */
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* Verify however downloaded file was kept. */
verifyStatic(never());
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verify(mDownloadManager, never()).remove(DOWNLOAD_ID);
/* Verify second download (restart app again) cleans first one. */
when(mDownloadManager.enqueue(mDownloadRequest)).thenReturn(DOWNLOAD_ID + 1);
restartActivity();
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
waitDownloadTask();
/* Verify new download id in storage. */
verifyStatic();
PreferencesStorage.putLong(PREFERENCE_KEY_DOWNLOAD_ID, DOWNLOAD_ID + 1);
/* Verify previous download removed. */
verify(mDownloadManager).remove(DOWNLOAD_ID);
/* Notification already canceled so no more call, i.e. only once. */
verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
}
use of android.os.Build in project Shuttle by timusus.
the class ShuttleUtils method setRingtone.
/**
* Method setRingtone.
*
* @param context context
* @param song Song
*/
public static void setRingtone(final Context context, final Song song) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context)) {
new AlertDialog.Builder(context).setTitle(R.string.dialog_title_set_ringtone).setMessage(R.string.dialog_message_set_ringtone).setPositiveButton(R.string.button_ok, (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + ShuttleApplication.getInstance().getPackageName()));
context.startActivity(intent);
}).setNegativeButton(R.string.cancel, null).show();
return;
}
}
Observable.fromCallable(() -> {
boolean success = false;
final ContentResolver resolver = context.getContentResolver();
// Set the flag in the database to mark this as a ringtone
final Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, song.id);
try {
final ContentValues values = new ContentValues(2);
values.put(MediaStore.Audio.AudioColumns.IS_RINGTONE, "1");
values.put(MediaStore.Audio.AudioColumns.IS_ALARM, "1");
if (ringUri != null) {
resolver.update(ringUri, values, null, null);
}
} catch (final UnsupportedOperationException ex) {
// most likely the card just got unmounted
Log.e(TAG, "couldn't set ringtone flag for song " + song);
return false;
}
Query query = new Query.Builder().uri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI).projection(new String[] { BaseColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.TITLE }).selection(BaseColumns._ID + "=" + song.id).build();
final Cursor cursor = SqlUtils.createQuery(context, query);
if (cursor != null) {
try {
if (cursor.getCount() == 1) {
// Set the system setting to make this the current ringtone
cursor.moveToFirst();
if (ringUri != null) {
Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
}
success = true;
}
} finally {
cursor.close();
}
}
return success;
}).map(success -> success ? context.getString(R.string.ringtone_set, song.name) : context.getString(R.string.ringtone_set_failed)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(message -> Toast.makeText(context, message, Toast.LENGTH_SHORT).show(), error -> LogUtils.logException(TAG, "Error setting ringtone", error));
}
use of android.os.Build in project AmazeFileManager by TeamAmaze.
the class GeneralDialogCreation method deleteFilesDialog.
@SuppressWarnings("ConstantConditions")
public static void deleteFilesDialog(final Context c, final ArrayList<LayoutElementParcelable> layoutElements, final MainActivity mainActivity, final List<LayoutElementParcelable> positions, AppTheme appTheme) {
final ArrayList<HybridFileParcelable> itemsToDelete = new ArrayList<>();
int accentColor = mainActivity.getColorPreference().getColor(ColorUsage.ACCENT);
// Build dialog with custom view layout and accent color.
MaterialDialog dialog = new MaterialDialog.Builder(c).title(c.getString(R.string.dialog_delete_title)).customView(R.layout.dialog_delete, true).theme(appTheme.getMaterialDialogTheme()).negativeText(c.getString(R.string.cancel).toUpperCase()).positiveText(c.getString(R.string.delete).toUpperCase()).positiveColor(accentColor).negativeColor(accentColor).onPositive((dialog1, which) -> {
Toast.makeText(c, c.getString(R.string.deleting), Toast.LENGTH_SHORT).show();
mainActivity.mainActivityHelper.deleteFiles(itemsToDelete);
}).build();
// Get views from custom layout to set text values.
final TextView categoryDirectories = (TextView) dialog.getCustomView().findViewById(R.id.category_directories);
final TextView categoryFiles = (TextView) dialog.getCustomView().findViewById(R.id.category_files);
final TextView listDirectories = (TextView) dialog.getCustomView().findViewById(R.id.list_directories);
final TextView listFiles = (TextView) dialog.getCustomView().findViewById(R.id.list_files);
final TextView total = (TextView) dialog.getCustomView().findViewById(R.id.total);
// Parse items to delete.
new AsyncTask<Void, Object, Void>() {
long sizeTotal = 0;
StringBuilder files = new StringBuilder();
StringBuilder directories = new StringBuilder();
int counterDirectories = 0;
int counterFiles = 0;
@Override
protected void onPreExecute() {
super.onPreExecute();
listFiles.setText(c.getString(R.string.loading));
listDirectories.setText(c.getString(R.string.loading));
total.setText(c.getString(R.string.loading));
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < positions.size(); i++) {
final LayoutElementParcelable layoutElement = positions.get(i);
itemsToDelete.add(layoutElement.generateBaseFile());
// Build list of directories to delete.
if (layoutElement.isDirectory) {
// Don't add newline between category and list.
if (counterDirectories != 0) {
directories.append("\n");
}
long sizeDirectory = layoutElement.generateBaseFile().folderSize(c);
directories.append(++counterDirectories).append(". ").append(layoutElement.title).append(" (").append(Formatter.formatFileSize(c, sizeDirectory)).append(")");
sizeTotal += sizeDirectory;
// Build list of files to delete.
} else {
// Don't add newline between category and list.
if (counterFiles != 0) {
files.append("\n");
}
files.append(++counterFiles).append(". ").append(layoutElement.title).append(" (").append(layoutElement.size).append(")");
sizeTotal += layoutElement.longSize;
}
publishProgress(sizeTotal, counterFiles, counterDirectories, files, directories);
}
return null;
}
@Override
protected void onProgressUpdate(Object... result) {
super.onProgressUpdate(result);
int tempCounterFiles = (int) result[1];
int tempCounterDirectories = (int) result[2];
long tempSizeTotal = (long) result[0];
StringBuilder tempFilesStringBuilder = (StringBuilder) result[3];
StringBuilder tempDirectoriesStringBuilder = (StringBuilder) result[4];
updateViews(tempSizeTotal, tempFilesStringBuilder, tempDirectoriesStringBuilder, tempCounterFiles, tempCounterDirectories);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
updateViews(sizeTotal, files, directories, counterFiles, counterDirectories);
}
private void updateViews(long tempSizeTotal, StringBuilder filesStringBuilder, StringBuilder directoriesStringBuilder, int... values) {
int tempCounterFiles = values[0];
int tempCounterDirectories = values[1];
// Hide category and list for directories when zero.
if (tempCounterDirectories == 0) {
if (tempCounterDirectories == 0) {
categoryDirectories.setVisibility(View.GONE);
listDirectories.setVisibility(View.GONE);
}
// Hide category and list for files when zero.
}
if (tempCounterFiles == 0) {
categoryFiles.setVisibility(View.GONE);
listFiles.setVisibility(View.GONE);
}
if (tempCounterDirectories != 0 || tempCounterFiles != 0) {
listDirectories.setText(directoriesStringBuilder);
if (listDirectories.getVisibility() != View.VISIBLE && tempCounterDirectories != 0)
listDirectories.setVisibility(View.VISIBLE);
listFiles.setText(filesStringBuilder);
if (listFiles.getVisibility() != View.VISIBLE && tempCounterFiles != 0)
listFiles.setVisibility(View.VISIBLE);
if (categoryDirectories.getVisibility() != View.VISIBLE && tempCounterDirectories != 0)
categoryDirectories.setVisibility(View.VISIBLE);
if (categoryFiles.getVisibility() != View.VISIBLE && tempCounterFiles != 0)
categoryFiles.setVisibility(View.VISIBLE);
}
// Show total size with at least one directory or file and size is not zero.
if (tempCounterFiles + tempCounterDirectories > 1 && tempSizeTotal > 0) {
StringBuilder builderTotal = new StringBuilder().append(c.getString(R.string.total)).append(" ").append(Formatter.formatFileSize(c, tempSizeTotal));
total.setText(builderTotal);
if (total.getVisibility() != View.VISIBLE)
total.setVisibility(View.VISIBLE);
} else {
total.setVisibility(View.GONE);
}
}
}.execute();
// Set category text color for Jelly Bean (API 16) and later.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
categoryDirectories.setTextColor(accentColor);
categoryFiles.setTextColor(accentColor);
}
// Show dialog on screen.
dialog.show();
}
Aggregations