Simplify code syntax using Java 8 features

Change-Id: I3e59f0c38e4047595374a951619c9b43a46901df
This commit is contained in:
Gabriele M
2018-04-08 10:26:20 +02:00
committed by Joey
parent a72b78c31e
commit a64eae5fc0
6 changed files with 100 additions and 196 deletions

View File

@@ -175,36 +175,30 @@ public class ExportUpdateService extends Service {
startForeground(NOTIFICATION_ID, notificationBuilder.build()); startForeground(NOTIFICATION_ID, notificationBuilder.build());
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
Runnable runnableComplete = new Runnable() { Runnable runnableComplete = () -> {
@Override notificationStyle.setSummaryText(null);
public void run() { notificationStyle.setBigContentTitle(
notificationStyle.setSummaryText(null); getString(R.string.notification_export_success));
notificationStyle.setBigContentTitle( notificationBuilder.setContentTitle(
getString(R.string.notification_export_success)); getString(R.string.notification_export_success));
notificationBuilder.setContentTitle( notificationBuilder.setProgress(0, 0, false);
getString(R.string.notification_export_success)); notificationBuilder.setContentText(destination.getName());
notificationBuilder.setProgress(0, 0, false); notificationBuilder.mActions.clear();
notificationBuilder.setContentText(destination.getName()); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
notificationBuilder.mActions.clear(); stopForeground(STOP_FOREGROUND_DETACH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
stopForeground(STOP_FOREGROUND_DETACH);
}
}; };
Runnable runnableFailed = new Runnable() { Runnable runnableFailed = () -> {
@Override notificationStyle.setSummaryText(null);
public void run() { notificationStyle.setBigContentTitle(
notificationStyle.setSummaryText(null); getString(R.string.notification_export_fail));
notificationStyle.setBigContentTitle( notificationBuilder.setContentTitle(
getString(R.string.notification_export_fail)); getString(R.string.notification_export_fail));
notificationBuilder.setContentTitle( notificationBuilder.setProgress(0, 0, false);
getString(R.string.notification_export_fail)); notificationBuilder.setContentText(null);
notificationBuilder.setProgress(0, 0, false); notificationBuilder.mActions.clear();
notificationBuilder.setContentText(null); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
notificationBuilder.mActions.clear(); stopForeground(STOP_FOREGROUND_DETACH);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
stopForeground(STOP_FOREGROUND_DETACH);
}
}; };
mExportRunnable = new ExportRunnable(source, destination, progressCallBack, mExportRunnable = new ExportRunnable(source, destination, progressCallBack,

View File

@@ -60,8 +60,6 @@ import org.lineageos.updater.model.UpdateInfo;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
public class UpdatesActivity extends UpdatesListActivity { public class UpdatesActivity extends UpdatesListActivity {
@@ -308,12 +306,7 @@ public class UpdatesActivity extends UpdatesListActivity {
} else { } else {
findViewById(R.id.no_new_updates_view).setVisibility(View.GONE); findViewById(R.id.no_new_updates_view).setVisibility(View.GONE);
findViewById(R.id.recycler_view).setVisibility(View.VISIBLE); findViewById(R.id.recycler_view).setVisibility(View.VISIBLE);
Collections.sort(sortedUpdates, new Comparator<UpdateInfo>() { sortedUpdates.sort((u1, u2) -> Long.compare(u2.getTimestamp(), u1.getTimestamp()));
@Override
public int compare(UpdateInfo u1, UpdateInfo u2) {
return Long.compare(u2.getTimestamp(), u1.getTimestamp());
}
});
for (UpdateInfo update : sortedUpdates) { for (UpdateInfo update : sortedUpdates) {
updateIds.add(update.getDownloadId()); updateIds.add(update.getDownloadId());
} }
@@ -366,14 +359,11 @@ public class UpdatesActivity extends UpdatesListActivity {
@Override @Override
public void onFailure(final boolean cancelled) { public void onFailure(final boolean cancelled) {
Log.e(TAG, "Could not download updates list"); Log.e(TAG, "Could not download updates list");
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override if (!cancelled) {
public void run() { showSnackbar(R.string.snack_updates_check_failed, Snackbar.LENGTH_LONG);
if (!cancelled) {
showSnackbar(R.string.snack_updates_check_failed, Snackbar.LENGTH_LONG);
}
refreshAnimationStop();
} }
refreshAnimationStop();
}); });
} }
@@ -384,13 +374,10 @@ public class UpdatesActivity extends UpdatesListActivity {
@Override @Override
public void onSuccess(File destination) { public void onSuccess(File destination) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override Log.d(TAG, "List downloaded");
public void run() { processNewJson(jsonFile, jsonFileTmp, manualRefresh);
Log.d(TAG, "List downloaded"); refreshAnimationStop();
processNewJson(jsonFile, jsonFileTmp, manualRefresh);
refreshAnimationStop();
}
}); });
} }
}; };

View File

@@ -15,7 +15,6 @@
*/ */
package org.lineageos.updater; package org.lineageos.updater;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@@ -292,17 +291,14 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
.setMessage(R.string.update_on_mobile_data_message) .setMessage(R.string.update_on_mobile_data_message)
.setView(checkboxView) .setView(checkboxView)
.setPositiveButton(R.string.action_description_download, .setPositiveButton(R.string.action_description_download,
new DialogInterface.OnClickListener() { (dialog, which) -> {
@Override if (checkbox.isChecked()) {
public void onClick(DialogInterface dialog, int which) { preferences.edit()
if (checkbox.isChecked()) { .putBoolean(Constants.PREF_MOBILE_DATA_WARNING, false)
preferences.edit() .apply();
.putBoolean(Constants.PREF_MOBILE_DATA_WARNING, false) mActivity.supportInvalidateOptionsMenu();
.apply();
mActivity.supportInvalidateOptionsMenu();
}
mUpdaterController.startDownload(downloadId);
} }
mUpdaterController.startDownload(downloadId);
}) })
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();
@@ -317,24 +313,15 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setContentDescription( button.setContentDescription(
mActivity.getString(R.string.action_description_download)); mActivity.getString(R.string.action_description_download));
button.setEnabled(enabled); button.setEnabled(enabled);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> startDownloadWithWarning(downloadId) : null;
@Override
public void onClick(View view) {
startDownloadWithWarning(downloadId);
}
};
break; break;
case PAUSE: case PAUSE:
button.setImageResource(R.drawable.ic_pause); button.setImageResource(R.drawable.ic_pause);
button.setContentDescription( button.setContentDescription(
mActivity.getString(R.string.action_description_pause)); mActivity.getString(R.string.action_description_pause));
button.setEnabled(enabled); button.setEnabled(enabled);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> mUpdaterController.pauseDownload(downloadId)
@Override : null;
public void onClick(View view) {
mUpdaterController.pauseDownload(downloadId);
}
};
break; break;
case RESUME: { case RESUME: {
button.setImageResource(R.drawable.ic_resume); button.setImageResource(R.drawable.ic_resume);
@@ -344,17 +331,14 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
UpdateInfo update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
final boolean canInstall = Utils.canInstall(update) || final boolean canInstall = Utils.canInstall(update) ||
update.getFile().length() == update.getFileSize(); update.getFile().length() == update.getFileSize();
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> {
@Override if (canInstall) {
public void onClick(View view) { mUpdaterController.resumeDownload(downloadId);
if (canInstall) { } else {
mUpdaterController.resumeDownload(downloadId); mActivity.showSnackbar(R.string.snack_update_not_installable,
} else { Snackbar.LENGTH_LONG);
mActivity.showSnackbar(R.string.snack_update_not_installable,
Snackbar.LENGTH_LONG);
}
} }
}; } : null;
} }
break; break;
case INSTALL: { case INSTALL: {
@@ -364,29 +348,21 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setEnabled(enabled); button.setEnabled(enabled);
UpdateInfo update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
final boolean canInstall = Utils.canInstall(update); final boolean canInstall = Utils.canInstall(update);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> {
@Override if (canInstall) {
public void onClick(View view) { getInstallDialog(downloadId).show();
if (canInstall) { } else {
getInstallDialog(downloadId).show(); mActivity.showSnackbar(R.string.snack_update_not_installable,
} else { Snackbar.LENGTH_LONG);
mActivity.showSnackbar(R.string.snack_update_not_installable,
Snackbar.LENGTH_LONG);
}
} }
}; } : null;
} }
break; break;
case INFO: { case INFO: {
button.setImageResource(R.drawable.ic_info); button.setImageResource(R.drawable.ic_info);
button.setContentDescription(mActivity.getString(R.string.action_description_info)); button.setContentDescription(mActivity.getString(R.string.action_description_info));
button.setEnabled(enabled); button.setEnabled(enabled);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> showInfoDialog() : null;
@Override
public void onClick(View view) {
showInfoDialog();
}
};
} }
break; break;
case DELETE: { case DELETE: {
@@ -394,12 +370,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setContentDescription( button.setContentDescription(
mActivity.getString(R.string.action_description_delete)); mActivity.getString(R.string.action_description_delete));
button.setEnabled(enabled); button.setEnabled(enabled);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> getDeleteDialog(downloadId).show() : null;
@Override
public void onClick(View view) {
getDeleteDialog(downloadId).show();
}
};
} }
break; break;
case CANCEL_INSTALLATION: { case CANCEL_INSTALLATION: {
@@ -407,12 +378,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setContentDescription( button.setContentDescription(
mActivity.getString(R.string.action_description_cancel)); mActivity.getString(R.string.action_description_cancel));
button.setEnabled(enabled); button.setEnabled(enabled);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = enabled ? view -> getCancelInstallationDialog().show() : null;
@Override
public void onClick(View view) {
getCancelInstallationDialog().show();
}
};
} }
break; break;
default: default:
@@ -421,13 +387,10 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setAlpha(enabled ? 1.f : mAlphaDisabledValue); button.setAlpha(enabled ? 1.f : mAlphaDisabledValue);
// Disable action mode when a button is clicked // Disable action mode when a button is clicked
button.setOnClickListener(new View.OnClickListener() { button.setOnClickListener(v -> {
@Override if (clickListener != null) {
public void onClick(View v) { clickListener.onClick(v);
if (clickListener != null) { stopActionMode();
clickListener.onClick(v);
stopActionMode();
}
} }
}); });
} }
@@ -442,26 +405,20 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
.setTitle(R.string.confirm_delete_dialog_title) .setTitle(R.string.confirm_delete_dialog_title)
.setMessage(R.string.confirm_delete_dialog_message) .setMessage(R.string.confirm_delete_dialog_message)
.setPositiveButton(android.R.string.ok, .setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { (dialog, which) -> {
@Override mUpdaterController.pauseDownload(downloadId);
public void onClick(DialogInterface dialog, int which) { mUpdaterController.deleteUpdate(downloadId);
mUpdaterController.pauseDownload(downloadId);
mUpdaterController.deleteUpdate(downloadId);
}
}) })
.setNegativeButton(android.R.string.cancel, null); .setNegativeButton(android.R.string.cancel, null);
} }
private View.OnLongClickListener getLongClickListener(final UpdateInfo update, private View.OnLongClickListener getLongClickListener(final UpdateInfo update,
final boolean canDelete) { final boolean canDelete) {
return new View.OnLongClickListener() { return view -> {
@Override if (mActionMode == null) {
public boolean onLongClick(View view) { startActionMode(update, canDelete);
if (mActionMode == null) {
startActionMode(update, canDelete);
}
return true;
} }
return true;
}; };
} }
@@ -488,12 +445,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
.setMessage(mActivity.getString(resId, buildInfoText, .setMessage(mActivity.getString(resId, buildInfoText,
mActivity.getString(android.R.string.ok))) mActivity.getString(android.R.string.ok)))
.setPositiveButton(android.R.string.ok, .setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { (dialog, which) -> Utils.triggerUpdate(mActivity, downloadId))
@Override
public void onClick(DialogInterface dialog, int which) {
Utils.triggerUpdate(mActivity, downloadId);
}
})
.setNegativeButton(android.R.string.cancel, null); .setNegativeButton(android.R.string.cancel, null);
} }
@@ -501,13 +453,10 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
return new AlertDialog.Builder(mActivity) return new AlertDialog.Builder(mActivity)
.setMessage(R.string.cancel_installation_dialog_message) .setMessage(R.string.cancel_installation_dialog_message)
.setPositiveButton(android.R.string.ok, .setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { (dialog, which) -> {
@Override Intent intent = new Intent(mActivity, UpdaterService.class);
public void onClick(DialogInterface dialog, int which) { intent.setAction(UpdaterService.ACTION_INSTALL_STOP);
Intent intent = new Intent(mActivity, UpdaterService.class); mActivity.startService(intent);
intent.setAction(UpdaterService.ACTION_INSTALL_STOP);
mActivity.startService(intent);
}
}) })
.setNegativeButton(android.R.string.cancel, null); .setNegativeButton(android.R.string.cancel, null);
} }
@@ -559,12 +508,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.menu_delete_action: case R.id.menu_delete_action:
getDeleteDialog(update.getDownloadId()) getDeleteDialog(update.getDownloadId())
.setOnDismissListener(new DialogInterface.OnDismissListener() { .setOnDismissListener(dialog -> mode.finish())
@Override
public void onDismiss(DialogInterface dialog) {
mode.finish();
}
})
.show(); .show();
return true; return true;
case R.id.menu_copy_url: case R.id.menu_copy_url:

View File

@@ -172,13 +172,8 @@ public class UpdaterController implements Controller {
} }
update.setStatus(UpdateStatus.DOWNLOADING); update.setStatus(UpdateStatus.DOWNLOADING);
update.setPersistentStatus(UpdateStatus.Persistent.INCOMPLETE); update.setPersistentStatus(UpdateStatus.Persistent.INCOMPLETE);
new Thread(new Runnable() { new Thread(() -> mUpdatesDbHelper.addUpdateWithOnConflict(update,
@Override SQLiteDatabase.CONFLICT_REPLACE)).start();
public void run() {
mUpdatesDbHelper.addUpdateWithOnConflict(update,
SQLiteDatabase.CONFLICT_REPLACE);
}
}).start();
notifyUpdateChange(downloadId); notifyUpdateChange(downloadId);
} }
@@ -245,25 +240,22 @@ public class UpdaterController implements Controller {
private void verifyUpdateAsync(final String downloadId) { private void verifyUpdateAsync(final String downloadId) {
mVerifyingUpdates.add(downloadId); mVerifyingUpdates.add(downloadId);
new Thread(new Runnable() { new Thread(() -> {
@Override Update update = mDownloads.get(downloadId).mUpdate;
public void run() { File file = update.getFile();
Update update = mDownloads.get(downloadId).mUpdate; if (file.exists() && verifyPackage(file)) {
File file = update.getFile(); file.setReadable(true, false);
if (file.exists() && verifyPackage(file)) { update.setPersistentStatus(UpdateStatus.Persistent.VERIFIED);
file.setReadable(true, false); mUpdatesDbHelper.changeUpdateStatus(update);
update.setPersistentStatus(UpdateStatus.Persistent.VERIFIED); update.setStatus(UpdateStatus.VERIFIED);
mUpdatesDbHelper.changeUpdateStatus(update); } else {
update.setStatus(UpdateStatus.VERIFIED); update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN);
} else { mUpdatesDbHelper.removeUpdate(downloadId);
update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN); update.setProgress(0);
mUpdatesDbHelper.removeUpdate(downloadId); update.setStatus(UpdateStatus.VERIFICATION_FAILED);
update.setProgress(0);
update.setStatus(UpdateStatus.VERIFICATION_FAILED);
}
mVerifyingUpdates.remove(downloadId);
notifyUpdateChange(downloadId);
} }
mVerifyingUpdates.remove(downloadId);
notifyUpdateChange(downloadId);
}).start(); }).start();
} }
@@ -454,15 +446,12 @@ public class UpdaterController implements Controller {
} }
private void deleteUpdateAsync(final Update update) { private void deleteUpdateAsync(final Update update) {
new Thread(new Runnable() { new Thread(() -> {
@Override File file = update.getFile();
public void run() { if (file.exists() && !file.delete()) {
File file = update.getFile(); Log.e(TAG, "Could not delete " + file.getAbsolutePath());
if (file.exists() && !file.delete()) {
Log.e(TAG, "Could not delete " + file.getAbsolutePath());
}
mUpdatesDbHelper.removeUpdate(update.getDownloadId());
} }
mUpdatesDbHelper.removeUpdate(update.getDownloadId());
}).start(); }).start();
} }

View File

@@ -192,12 +192,7 @@ public class HttpURLConnectionClient implements DownloadClient {
for (Map.Entry<String, List<String>> entry : mClient.getHeaderFields().entrySet()) { for (Map.Entry<String, List<String>> entry : mClient.getHeaderFields().entrySet()) {
if ("Link".equalsIgnoreCase((entry.getKey()))) { if ("Link".equalsIgnoreCase((entry.getKey()))) {
duplicates = new PriorityQueue<>(entry.getValue().size(), duplicates = new PriorityQueue<>(entry.getValue().size(),
new Comparator<DuplicateLink>() { Comparator.comparingInt(d -> d.mPriority));
@Override
public int compare(DuplicateLink d1, DuplicateLink d2) {
return Integer.compare(d1.mPriority, d2.mPriority);
}
});
// https://tools.ietf.org/html/rfc6249 // https://tools.ietf.org/html/rfc6249
// https://tools.ietf.org/html/rfc5988#section-5 // https://tools.ietf.org/html/rfc5988#section-5

View File

@@ -43,7 +43,6 @@ import org.lineageos.updater.model.UpdateInfo;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
@@ -246,12 +245,8 @@ public class Utils {
} }
public static void removeUncryptFiles(File downloadPath) { public static void removeUncryptFiles(File downloadPath) {
File[] uncryptFiles = downloadPath.listFiles(new FilenameFilter() { File[] uncryptFiles = downloadPath.listFiles(
@Override (dir, name) -> name.endsWith(Constants.UNCRYPT_FILE_EXT));
public boolean accept(File dir, String name) {
return name.endsWith(Constants.UNCRYPT_FILE_EXT);
}
});
if (uncryptFiles == null) { if (uncryptFiles == null) {
return; return;
} }