Skip to content

Commit

Permalink
[Refactor] Replace custom toasts with UIUtils.display*Toast methods
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
  • Loading branch information
MuntashirAkon committed Jul 9, 2024
1 parent 4b7748c commit 1a19bc9
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IntDef;
import androidx.annotation.MainThread;
Expand Down Expand Up @@ -658,7 +657,7 @@ private void getServicesView(@NonNull Context context, @NonNull ViewHolder holde
ActivityManagerCompat.startService(intent, mUserId, true);
} catch (Throwable th) {
th.printStackTrace();
Toast.makeText(context, th.toString(), Toast.LENGTH_LONG).show();
UIUtils.displayShortToast(th.toString());
}
});
holder.launchBtn.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.UiThread;
Expand All @@ -21,6 +20,7 @@
import io.github.muntashirakon.AppManager.apk.ApkSource;
import io.github.muntashirakon.AppManager.editor.CodeEditorFragment;
import io.github.muntashirakon.AppManager.intercept.IntentCompat;
import io.github.muntashirakon.AppManager.utils.UIUtils;

public class ManifestViewerActivity extends BaseActivity {
public static final String EXTRA_PACKAGE_NAME = "pkg";
Expand Down Expand Up @@ -66,7 +66,7 @@ protected void onAuthenticated(Bundle savedInstanceState) {

@UiThread
private void showErrorAndFinish() {
Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
UIUtils.displayShortToast(R.string.error);
finish();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -57,10 +56,11 @@
import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.filters.options.FilterOption;
import io.github.muntashirakon.AppManager.filters.options.FilterOptions;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.DateUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.adapters.SelectedArrayAdapter;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.view.TextInputLayoutCompat;
import io.github.muntashirakon.widget.MaterialSpinner;
import io.github.muntashirakon.widget.RecyclerView;
Expand Down Expand Up @@ -252,7 +252,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
builder.setView(view)
.setPositiveButton(editMode ? R.string.update : R.string.add, (dialog, which) -> {
if (mCurrentFilterOption == null) {
Toast.makeText(getActivity(), R.string.key_name_cannot_be_null, Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(R.string.key_name_cannot_be_null);
return;
}
WrappedFilterOption newWrappedFilterOption;
Expand All @@ -268,7 +268,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
mCurrentFilterOption.setKeyValue(mCurrentKey, TextUtils.isEmpty(editable) ? null : editable.toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), R.string.error_evaluating_input, Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(R.string.error_evaluating_input);
return;
}
if (editMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
Expand Down Expand Up @@ -239,8 +238,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
showResetIntentButton(true);
refreshUI();
} catch (Exception e) {
Toast.makeText(ActivityInterceptor.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(e.getMessage());
e.printStackTrace();
}
}
Expand Down Expand Up @@ -313,9 +311,7 @@ public void afterTextChanged(Editable s) {

refreshUI();
Uri uri = data == null ? null : data.getData();
Toast.makeText(ActivityInterceptor.this,
String.format("%s: (%s)", getString(R.string.activity_result), uri),
Toast.LENGTH_LONG).show();
UIUtils.displayLongToast("%s: (%s)", getString(R.string.activity_result), uri);
});

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

import static io.github.muntashirakon.AppManager.intercept.IntentCompat.parseExtraValue;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
Expand All @@ -29,6 +26,7 @@
import java.io.Serializable;

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.adapters.SelectedArrayAdapter;
import io.github.muntashirakon.widget.MaterialSpinner;

Expand Down Expand Up @@ -139,11 +137,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle args = requireArguments();
ExtraItem extraItem = (ExtraItem) args.getSerializable(ARG_PREF_ITEM);
@Mode int mode = args.getInt(ARG_MODE, MODE_CREATE);

LayoutInflater inflater = LayoutInflater.from(activity);
if (inflater == null) return super.onCreateDialog(savedInstanceState);
@SuppressLint("InflateParams")
View view = inflater.inflate(R.layout.dialog_edit_pref_item, null);
View view = View.inflate(activity, R.layout.dialog_edit_pref_item, null);
MaterialSpinner spinner = view.findViewById(R.id.type_selector_spinner);
ArrayAdapter<CharSequence> spinnerAdapter = SelectedArrayAdapter.createFromResource(activity,
R.array.extras_types, io.github.muntashirakon.ui.R.layout.auto_complete_dropdown_item);
Expand Down Expand Up @@ -229,7 +223,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
.setPositiveButton(mode == MODE_CREATE ? R.string.add : R.string.done, (dialog, which) -> {
if (mOnSaveListener == null) return;
if (editKeyName.getText() == null) {
Toast.makeText(getActivity(), R.string.key_name_cannot_be_null, Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(R.string.key_name_cannot_be_null);
return;
}
String keyName = editKeyName.getText().toString().trim();
Expand All @@ -241,7 +235,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
}
newExtraItem.type = mCurrentType;
if (TextUtils.isEmpty(newExtraItem.keyName)) {
Toast.makeText(getActivity(), R.string.key_name_cannot_be_null, Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(R.string.key_name_cannot_be_null);
return;
}
try {
Expand All @@ -252,7 +246,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), R.string.error_evaluating_input, Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(R.string.error_evaluating_input);
return;
}
mOnSaveListener.onSave(mode, newExtraItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.contract.ActivityResultContracts;
Expand Down Expand Up @@ -126,7 +125,7 @@ public static void startChooser(@NonNull Context context, @Nullable String subje
context.startActivity(Intent.createChooser(actionSendIntent, context.getResources().getText(R.string.send_log_title))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(e.getMessage());
}
}

Expand Down Expand Up @@ -287,8 +286,7 @@ private void applyFiltersFromIntent(@Nullable Intent intent) {
if (!TextUtils.isEmpty(level)) {
int logLevelLimit = LogLine.convertCharToLogLevel(level.charAt(0));
if (logLevelLimit == -1) {
String invalidLevel = getString(R.string.toast_invalid_level, level);
Toast.makeText(this, invalidLevel, Toast.LENGTH_LONG).show();
UIUtils.displayLongToast(R.string.toast_invalid_level, level);
} else {
mViewModel.setLogLevel(logLevelLimit);
search(mSearchQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -42,6 +39,7 @@
import io.github.muntashirakon.AppManager.utils.CpuUtils;
import io.github.muntashirakon.AppManager.utils.NotificationUtils;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;

/**
* Reads logs.
Expand All @@ -63,7 +61,6 @@ public class LogcatRecordingService extends ForegroundService {
private final Object mLock = new Object();
private LogcatReader mReader;
private boolean mKilled;
private Handler mHandler;
private QueuedProgressHandler mProgressHandler;
private PowerManager.WakeLock mWakeLock;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
Expand All @@ -85,7 +82,6 @@ public void onCreate() {
IntentFilter intentFilter = new IntentFilter(ACTION_STOP_RECORDING);
intentFilter.addDataScheme(URI_SCHEME);
ContextCompat.registerReceiver(this, mReceiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);
mHandler = new Handler(Looper.getMainLooper());
mWakeLock = CpuUtils.getPartialWakeLock("logcat_recorder");
mWakeLock.acquire();
}
Expand All @@ -100,7 +96,7 @@ private void initializeReader(@NonNull LogcatReaderLoader loader) {
// it's ready to record
}
if (!mKilled) {
makeToast(R.string.log_recording_started, Toast.LENGTH_SHORT);
ThreadUtils.postOnMainThread(() -> UIUtils.displayShortToast(R.string.log_recording_started));
}
} catch (IOException e) {
Log.e(TAG, e);
Expand Down Expand Up @@ -220,10 +216,6 @@ private PendingIntent getLogcatActivityToViewSavedFile(String filename) {
}


private void makeToast(final int stringResId, final int toastLength) {
mHandler.post(() -> Toast.makeText(LogcatRecordingService.this, stringResId, toastLength).show());
}

private void killProcess() {
if (!mKilled) {
synchronized (mLock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -28,6 +27,7 @@
import io.github.muntashirakon.AppManager.settings.LogViewerPreferences;
import io.github.muntashirakon.AppManager.settings.Prefs;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.dialog.TextInputDialogBuilder;
import io.github.muntashirakon.dialog.TextInputDropdownDialogBuilder;

Expand Down Expand Up @@ -78,7 +78,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
.setInputText(logFilename)
.setPositiveButton(R.string.ok, (dialog, which, inputText, isChecked) -> {
if (SaveLogHelper.isInvalidFilename(inputText)) {
Toast.makeText(mActivity, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.enter_good_filename);
} else {
//noinspection ConstantConditions
String filename = inputText.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.text.SpannableStringBuilder;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -418,11 +417,6 @@ private void clearData(@NonNull List<String> candidatePackages) {
.show();
}

private void clearAppCache() {
// TODO
Toast.makeText(this, "Not implemented yet.", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -244,7 +243,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.go, (dialog, which, profName, isChecked) -> {
if (TextUtils.isEmpty(profName)) {
Toast.makeText(this, R.string.failed_to_duplicate_profile, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.failed_to_duplicate_profile);
return;
}
progressIndicator.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
Expand Down Expand Up @@ -80,10 +79,10 @@ public class ProfilesActivity extends BaseActivity {
Path profilePath = ProfileManager.findProfilePathById(mProfileId);
AppsProfile profile = AppsProfile.fromPath(profilePath);
profile.write(os);
Toast.makeText(this, R.string.the_export_was_successful, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.the_export_was_successful);
} catch (IOException | JSONException e) {
Log.e(TAG, "Error: ", e);
Toast.makeText(this, R.string.export_failed, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.export_failed);
}
}
});
Expand All @@ -104,12 +103,12 @@ public class ProfilesActivity extends BaseActivity {
try (OutputStream os = innerProfilePath.openOutputStream()) {
newProfile.write(os);
}
Toast.makeText(this, R.string.the_import_was_successful, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.the_import_was_successful);
// Load imported profile
startActivity(AppsProfileActivity.getProfileIntent(this, newProfile.profileId));
} catch (IOException | JSONException e) {
Log.e(TAG, "Error: ", e);
Toast.makeText(this, R.string.import_failed, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.import_failed);
}
});

Expand Down Expand Up @@ -247,15 +246,15 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
.setPositiveButton(R.string.cancel, null)
.setNegativeButton(R.string.ok, (dialog, which) -> {
if (ProfileManager.deleteProfile(profile.profileId)) {
Toast.makeText(mActivity, R.string.deleted_successfully, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.deleted_successfully);
} else {
Toast.makeText(mActivity, R.string.deletion_failed, Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast(R.string.deletion_failed);
}
})
.show();
} else if (id == R.id.action_routine_ops) {
// TODO(7/11/20): Setup routine operations for this profile
Toast.makeText(mActivity, "Not yet implemented", Toast.LENGTH_SHORT).show();
UIUtils.displayShortToast("Not yet implemented");
} else if (id == R.id.action_duplicate) {
new TextInputDialogBuilder(mActivity, R.string.input_profile_name)
.setTitle(R.string.new_profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -30,6 +29,7 @@
import io.github.muntashirakon.AppManager.intercept.IntentCompat;
import io.github.muntashirakon.AppManager.settings.FeatureController;
import io.github.muntashirakon.AppManager.utils.FileUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.io.IoUtils;

// Copyright 2015 Google, Inc.
Expand Down Expand Up @@ -68,7 +68,7 @@ protected void onAuthenticated(Bundle savedInstanceState) {

mApkUri = IntentCompat.getDataUri(intent);
if (mApkUri == null) {
Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show();
UIUtils.displayShortToast(R.string.error);
finish();
return;
}
Expand Down
Loading

0 comments on commit 1a19bc9

Please sign in to comment.