Skip to content

Commit

Permalink
Update to Lambda expressions
Browse files Browse the repository at this point in the history
Removed unnecessary global variables.
Added delay in showing sms switching dialog.
Fixed formatting.
  • Loading branch information
VenomVendor committed Jul 22, 2017
1 parent 9d6b68f commit 4c25478
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 214 deletions.
45 changes: 22 additions & 23 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.venomvendor.sms.deduplicate">

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

<application
android:allowBackup="false"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">

<activity
android:name=".activity.Deduplication"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Expand All @@ -32,7 +31,7 @@
android:exported="false"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER"/>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>

Expand All @@ -42,9 +41,9 @@
android:exported="false"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER"/>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />

<data android:mimeType="application/vnd.wap.mms-message"/>
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>

Expand All @@ -53,16 +52,16 @@
android:name=".ignore.ComposeSmsActivity"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SENDTO"/>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />

<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="sms"/>
<data android:scheme="smsto"/>
<data android:scheme="mms"/>
<data android:scheme="mmsto"/>
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>

Expand All @@ -72,14 +71,14 @@
android:exported="false"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />

<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="sms"/>
<data android:scheme="smsto"/>
<data android:scheme="mms"/>
<data android:scheme="mmsto"/>
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
import android.widget.TextView;
import android.widget.Toast;

import com.venomvendor.sms.deduplicate.BuildConfig;
import com.venomvendor.sms.deduplicate.R;
import com.venomvendor.sms.deduplicate.data.FindDuplicates;
import com.venomvendor.sms.deduplicate.service.DeleteSMSAsync;
import com.venomvendor.sms.deduplicate.service.OnDeletedListener;
import com.venomvendor.sms.deduplicate.util.Constants;
import com.venomvendor.sms.deduplicate.util.Utils;

Expand All @@ -65,21 +65,28 @@ public class Deduplication extends Activity implements View.OnClickListener {
private static final int UPDATE_PERMISSIONS = 254;
private static final int RUNTIME_PERMISSIONS_CODE = 255;
private static final String[] REQUIRED_PERMISSIONS = {permission.READ_SMS};
private static final String DEFAULT_DEL = "50";
private static final int DEFAULT_DEL = BuildConfig.DEBUG ? 2 : 50;
private String mFormat;
private DeleteSMSAsync mSmsDeleter;
private int mTotalMessages;

private DeleteSMSAsync mSmsDeleter;

private LinearLayout mProgressBarHolder;
private ProgressBar mProgressBar;

private Button mDeDuplicate;
private Button mCancel;
private Button mRevert;

private TextView mDeleted;
private TextView mRevertMessage;

private EditText mDeleteBy;

private CheckedTextView mIgnoreTimestamp;
private LinearLayout mIgnoreMessage;
private RadioButton mKeepFirst;

private SharedPreferences mPref;

@Override
Expand All @@ -103,23 +110,15 @@ private void showEula() {
.setMessage(getString(R.string.eula))
.setCancelable(false)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = mPref.edit();
editor.putBoolean(Constants.SHOW_EULA, false);
editor.commit();
dialog.dismiss();
initViews();
}
(dialog, which) -> {
SharedPreferences.Editor editor = mPref.edit();
editor.putBoolean(Constants.SHOW_EULA, false);
editor.commit();
dialog.dismiss();
initViews();
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
(dialog, which) -> finish());
builder.create().show();
}

Expand Down Expand Up @@ -177,19 +176,13 @@ private void checkPermissions() {
//Educate User
showCustomDialog(getString(R.string.required_permissions),
getString(R.string.m_permission_inform),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//ask permissions.
requestPermissions(REQUIRED_PERMISSIONS, RUNTIME_PERMISSIONS_CODE);
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
deadLock();
}
(dialog, which) -> {
dialog.dismiss();
//ask permissions.
requestPermissions(REQUIRED_PERMISSIONS, RUNTIME_PERMISSIONS_CODE);
}, (dialog, which) -> {
dialog.dismiss();
deadLock();
}
);
}
Expand All @@ -213,15 +206,12 @@ public void onRequestPermissionsResult(int requestCode, String permissions[],
private void permissionsDenied() {
showCustomDialog(getString(R.string.required_permissions),
String.format(getString(R.string.permission_explain), getString(R.string.app_name)),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, UPDATE_PERMISSIONS);
}
(dialog, which) -> {
dialog.dismiss();
Intent intent = new Intent(
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, UPDATE_PERMISSIONS);
}, null);
}

Expand All @@ -248,12 +238,9 @@ private void deadLock() {
showCustomDialog(getString(R.string.permissions_revoked), String.format("%s\n%s %s",
getString(R.string.app_cannot_run), getString(R.string.app_will_exit),
getString(android.R.string.ok)),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
(dialog, which) -> {
dialog.dismiss();
finish();
}, null
);
}
Expand Down Expand Up @@ -284,12 +271,7 @@ private void cancelDeletion() {
}

private void revertApp() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Utils.revertOldApp(getApplicationContext());
}
}, 3000);
new Handler().postDelayed(() -> Utils.revertOldApp(getApplicationContext()), 3000);
}

@TargetApi(Build.VERSION_CODES.KITKAT)
Expand All @@ -312,20 +294,10 @@ private void deduplicate() {
private void showWarning() {
AlertDialog.Builder warningDialog = new AlertDialog.Builder(this);
warningDialog.setMessage(R.string.warning_message);
warningDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
findDuplicates();
}
});
warningDialog.setPositiveButton(android.R.string.ok, (dialog, id) -> findDuplicates());

warningDialog.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
(dialog, id) -> dialog.dismiss());

AlertDialog dialog = warningDialog.create();
dialog.setCancelable(false);
Expand All @@ -336,55 +308,66 @@ private void findDuplicates() {
FindDuplicates findDuplicates = new FindDuplicates(this,
mIgnoreTimestamp.isChecked(),
mKeepFirst.isChecked());
findDuplicates.setOnDuplicatesFoundListener(new FindDuplicates.OnDuplicatesFoundListener() {
@Override
public void duplicatesFound(ArrayList<String> duplicateIds) {
if (duplicateIds.isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.no_duplicates,
Toast.LENGTH_SHORT).show();
Utils.revertOldApp(getApplicationContext());
} else {
mDeDuplicate.setVisibility(View.GONE);
mProgressBarHolder.setVisibility(View.VISIBLE);
startDeleteService(duplicateIds);
}
findDuplicates.setOnDuplicatesFoundListener(duplicateIds -> {
if (duplicateIds.isEmpty()) {
showToast(getString(R.string.no_duplicates));
revertApp();
} else {
showConfirmation(duplicateIds);
}
});
findDuplicates.execute();
}

private void startDeleteService(ArrayList<String> duplicateIds) {
private void showConfirmation(ArrayList<String> duplicateIds) {
mTotalMessages = duplicateIds.size();
mFormat = getResources().getQuantityString(R.plurals.deleted_messages, mTotalMessages);

AlertDialog.Builder confirmationDialog = new AlertDialog.Builder(this);
confirmationDialog.setCancelable(false);
confirmationDialog.setMessage(getResources()
.getQuantityString(R.plurals.delete_duplicates, mTotalMessages, mTotalMessages));

confirmationDialog.setPositiveButton(getString(android.R.string.ok),
(dialog, which) -> {
dialog.dismiss();
mDeDuplicate.setVisibility(View.GONE);
mProgressBarHolder.setVisibility(View.VISIBLE);
startDeleteService(duplicateIds);
});
confirmationDialog.setNegativeButton(getString(android.R.string.cancel),
(dialog, which) -> dialog.dismiss());
confirmationDialog.show();
}

private void startDeleteService(ArrayList<String> duplicateIds) {
mProgressBar.setMax(mTotalMessages);

String del = mDeleteBy.getText().toString().trim();
del = TextUtils.isEmpty(del) ? DEFAULT_DEL : del;
int deleteBy = Math.abs(Integer.valueOf(del));
int deleteBy = getPerBatch();
mSmsDeleter = new DeleteSMSAsync(this, duplicateIds, deleteBy,
this::updateDeletedItems);

mSmsDeleter = new DeleteSMSAsync(this, duplicateIds, deleteBy, new OnDeletedListener() {
@Override
public void onResponse(final int deletedMsgs, final boolean interrupted) {
updateDeletedItems(deletedMsgs, interrupted);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mSmsDeleter.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mSmsDeleter.execute();
}
}

private int getPerBatch() {
String del = mDeleteBy.getText().toString().trim();
del = TextUtils.isEmpty(del) ? "" + DEFAULT_DEL : del;
int deleteBy = Math.abs(Integer.valueOf(del));
deleteBy = Math.max(1, deleteBy);
return Math.min(deleteBy, 100);
}

private void updateDeletedItems(int deletedMsgs, boolean interrupted) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mDeleted.setText(String.format(mFormat, deletedMsgs, mTotalMessages));
if (interrupted) {
showToast(String.format(mFormat, deletedMsgs, mTotalMessages));
cancelDeletion();
}
runOnUiThread(() -> {
mDeleted.setText(String.format(mFormat, deletedMsgs, mTotalMessages));
if (interrupted) {
showToast(String.format(mFormat, deletedMsgs, mTotalMessages));
cancelDeletion();
}
});
}
Expand All @@ -407,12 +390,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mRevert.setVisibility(View.GONE);
mRevertMessage.setVisibility(View.VISIBLE);
showCustomDialog(getString(R.string.failed), getString(R.string.failure_message),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}, null);
(dialog, which) -> dialog.dismiss(), null);
}
} else if (requestCode == UPDATE_PERMISSIONS) {
if (checkSelfPermission(REQUIRED_PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED) {
Expand Down
Loading

0 comments on commit 4c25478

Please sign in to comment.