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,9 +175,7 @@ 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
public void run() {
notificationStyle.setSummaryText(null); notificationStyle.setSummaryText(null);
notificationStyle.setBigContentTitle( notificationStyle.setBigContentTitle(
getString(R.string.notification_export_success)); getString(R.string.notification_export_success));
@@ -188,12 +186,9 @@ public class ExportUpdateService extends Service {
notificationBuilder.mActions.clear(); notificationBuilder.mActions.clear();
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
stopForeground(STOP_FOREGROUND_DETACH); stopForeground(STOP_FOREGROUND_DETACH);
}
}; };
Runnable runnableFailed = new Runnable() { Runnable runnableFailed = () -> {
@Override
public void run() {
notificationStyle.setSummaryText(null); notificationStyle.setSummaryText(null);
notificationStyle.setBigContentTitle( notificationStyle.setBigContentTitle(
getString(R.string.notification_export_fail)); getString(R.string.notification_export_fail));
@@ -204,7 +199,6 @@ public class ExportUpdateService extends Service {
notificationBuilder.mActions.clear(); notificationBuilder.mActions.clear();
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
stopForeground(STOP_FOREGROUND_DETACH); 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
public void run() {
if (!cancelled) { if (!cancelled) {
showSnackbar(R.string.snack_updates_check_failed, Snackbar.LENGTH_LONG); 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
public void run() {
Log.d(TAG, "List downloaded"); Log.d(TAG, "List downloaded");
processNewJson(jsonFile, jsonFileTmp, manualRefresh); processNewJson(jsonFile, jsonFileTmp, manualRefresh);
refreshAnimationStop(); 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,9 +291,7 @@ 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
public void onClick(DialogInterface dialog, int which) {
if (checkbox.isChecked()) { if (checkbox.isChecked()) {
preferences.edit() preferences.edit()
.putBoolean(Constants.PREF_MOBILE_DATA_WARNING, false) .putBoolean(Constants.PREF_MOBILE_DATA_WARNING, false)
@@ -302,7 +299,6 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
mActivity.supportInvalidateOptionsMenu(); 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
public void onClick(View view) {
if (canInstall) { if (canInstall) {
mUpdaterController.resumeDownload(downloadId); mUpdaterController.resumeDownload(downloadId);
} else { } else {
mActivity.showSnackbar(R.string.snack_update_not_installable, mActivity.showSnackbar(R.string.snack_update_not_installable,
Snackbar.LENGTH_LONG); 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
public void onClick(View view) {
if (canInstall) { if (canInstall) {
getInstallDialog(downloadId).show(); getInstallDialog(downloadId).show();
} else { } else {
mActivity.showSnackbar(R.string.snack_update_not_installable, mActivity.showSnackbar(R.string.snack_update_not_installable,
Snackbar.LENGTH_LONG); 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,14 +387,11 @@ 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
public void onClick(View v) {
if (clickListener != null) { if (clickListener != null) {
clickListener.onClick(v); clickListener.onClick(v);
stopActionMode(); 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
public void onClick(DialogInterface dialog, int which) {
mUpdaterController.pauseDownload(downloadId); mUpdaterController.pauseDownload(downloadId);
mUpdaterController.deleteUpdate(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
public boolean onLongClick(View view) {
if (mActionMode == null) { if (mActionMode == null) {
startActionMode(update, canDelete); 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
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mActivity, UpdaterService.class); Intent intent = new Intent(mActivity, UpdaterService.class);
intent.setAction(UpdaterService.ACTION_INSTALL_STOP); intent.setAction(UpdaterService.ACTION_INSTALL_STOP);
mActivity.startService(intent); 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,9 +240,7 @@ 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
public void run() {
Update update = mDownloads.get(downloadId).mUpdate; Update update = mDownloads.get(downloadId).mUpdate;
File file = update.getFile(); File file = update.getFile();
if (file.exists() && verifyPackage(file)) { if (file.exists() && verifyPackage(file)) {
@@ -263,7 +256,6 @@ public class UpdaterController implements Controller {
} }
mVerifyingUpdates.remove(downloadId); mVerifyingUpdates.remove(downloadId);
notifyUpdateChange(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
public void run() {
File file = update.getFile(); File file = update.getFile();
if (file.exists() && !file.delete()) { if (file.exists() && !file.delete()) {
Log.e(TAG, "Could not delete " + file.getAbsolutePath()); 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;
} }