Skip to content

Commit

Permalink
Merge pull request nextcloud#13195 from nextcloud/convert-alert-dialo…
Browse files Browse the repository at this point in the history
…gs-to-m3

Convert Alert Dialogs to M3
  • Loading branch information
AndyScherzinger authored Jul 2, 2024
2 parents 31c7c86 + cdc2c26 commit 5c24ba7
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 202 deletions.
56 changes: 32 additions & 24 deletions app/src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.text.TextUtils;
import android.view.WindowManager;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.nextcloud.appReview.InAppReviewHelper;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
Expand Down Expand Up @@ -109,7 +110,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.util.Pair;
import androidx.lifecycle.Lifecycle;
Expand Down Expand Up @@ -353,8 +353,8 @@ public void onCreate() {
} catch (Exception e) {
Log_OC.d("Debug", "Failed to disable uri exposure");
}

initSyncOperations(preferences,
initSyncOperations(this,
preferences,
uploadsStorageManager,
accountManager,
connectivityService,
Expand All @@ -367,10 +367,11 @@ public void onCreate() {
initContactsBackup(accountManager, backgroundJobManager);
notificationChannels();

backgroundJobManager.scheduleMediaFoldersDetectionJob();
backgroundJobManager.startMediaFoldersDetectionJob();

backgroundJobManager.schedulePeriodicHealthStatus();
if (backgroundJobManager != null) {
backgroundJobManager.scheduleMediaFoldersDetectionJob();
backgroundJobManager.startMediaFoldersDetectionJob();
backgroundJobManager.schedulePeriodicHealthStatus();
}

registerGlobalPassCodeProtection();
}
Expand Down Expand Up @@ -492,11 +493,14 @@ private void initSecurityKeyManager() {

public static void initContactsBackup(UserAccountManager accountManager, BackgroundJobManager backgroundJobManager) {
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl(appContext.get());
if (accountManager == null) {
return;
}

List<User> users = accountManager.getAllUsers();
for (User user : users) {
if (arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CONTACTS_AUTOMATIC_BACKUP)) {
if (backgroundJobManager != null && arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CONTACTS_AUTOMATIC_BACKUP)) {
backgroundJobManager.schedulePeriodicContactsBackup(user);

}
}
}
Expand Down Expand Up @@ -582,6 +586,7 @@ private void enableStrictMode() {
}

public static void initSyncOperations(
final Context context,
final AppPreferences preferences,
final UploadsStorageManager uploadsStorageManager,
final UserAccountManager accountManager,
Expand All @@ -592,7 +597,7 @@ public static void initSyncOperations(
final ViewThemeUtils viewThemeUtils,
final WalledCheckCache walledCheckCache,
final SyncedFolderProvider syncedFolderProvider) {
updateToAutoUpload();
updateToAutoUpload(context);
cleanOldEntries(clock);
updateAutoUploadEntries(clock);

Expand Down Expand Up @@ -798,32 +803,35 @@ private static String getUserAgent(@StringRes int agent) {
return String.format(appString, version, brandedName);
}

private static void updateToAutoUpload() {
Context context = getAppContext();
private static void updateToAutoUpload(Context context) {
AppPreferences preferences = AppPreferencesImpl.fromContext(context);
if (preferences.instantPictureUploadEnabled() || preferences.instantVideoUploadEnabled()) {
preferences.removeLegacyPreferences();

// show info pop-up
try {
new AlertDialog.Builder(context, R.style.Theme_ownCloud_Dialog)
.setTitle(R.string.drawer_synced_folders)
.setMessage(R.string.synced_folders_new_info)
.setPositiveButton(R.string.drawer_open, (dialog, which) -> {
// show Auto Upload
Intent folderSyncIntent = new Intent(context, SyncedFoldersActivity.class);
dialog.dismiss();
context.startActivity(folderSyncIntent);
})
.setNegativeButton(R.string.drawer_close, (dialog, which) -> dialog.dismiss())
.setIcon(R.drawable.nav_synced_folders)
.show();
showAutoUploadAlertDialog(context);
} catch (WindowManager.BadTokenException e) {
Log_OC.i(TAG, "Error showing Auto Upload Update dialog, so skipping it: " + e.getMessage());
}
}
}

private static void showAutoUploadAlertDialog(Context context) {
new MaterialAlertDialogBuilder(context, R.style.Theme_ownCloud_Dialog)
.setTitle(R.string.drawer_synced_folders)
.setMessage(R.string.synced_folders_new_info)
.setPositiveButton(R.string.drawer_open, (dialog, which) -> {
Intent folderSyncIntent = new Intent(context, SyncedFoldersActivity.class);
dialog.dismiss();
context.startActivity(folderSyncIntent);
})
.setNegativeButton(R.string.drawer_close, (dialog, which) -> dialog.dismiss())
.setIcon(R.drawable.nav_synced_folders)
.create()
.show();
}

private static void updateAutoUploadEntries(Clock clock) {
// updates entries to reflect their true paths
Context context = getAppContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public void onReceive(Context context, Intent intent) {
AndroidInjection.inject(this, context);

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
MainApp.initSyncOperations(preferences,
MainApp.initSyncOperations(context,
preferences,
uploadsStorageManager,
accountManager,
connectivityService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
Expand All @@ -45,6 +44,7 @@
import android.view.WindowManager;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.appReview.InAppReviewHelper;
import com.nextcloud.client.account.User;
Expand Down Expand Up @@ -328,10 +328,15 @@ private void checkStoragePath() {
MainApp.setStoragePath(newStorage);

try {
AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.Theme_ownCloud_Dialog).setTitle(R.string.wrong_storage_path).setMessage(R.string.wrong_storage_path_desc).setPositiveButton(R.string.dialog_close, (dialog, which) -> dialog.dismiss()).setIcon(R.drawable.ic_settings).create();
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this, R.style.Theme_ownCloud_Dialog)
.setTitle(R.string.wrong_storage_path)
.setMessage(R.string.wrong_storage_path_desc)
.setPositiveButton(R.string.dialog_close, (dialog, which) -> dialog.dismiss())
.setIcon(R.drawable.ic_settings);

alertDialog.show();
viewThemeUtils.platform.colorTextButtons(alertDialog.getButton(AlertDialog.BUTTON_POSITIVE));
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(getApplicationContext(), builder);

builder.create().show();
} catch (WindowManager.BadTokenException e) {
Log_OC.e(TAG, "Error showing wrong storage info, so skipping it: " + e.getMessage());
}
Expand Down Expand Up @@ -405,18 +410,12 @@ private void upgradeNotificationForInstantUpload() {
if (preferences.instantPictureUploadEnabled() || preferences.instantVideoUploadEnabled()) {
preferences.removeLegacyPreferences();
// show info pop-up
new AlertDialog.Builder(this, R.style.Theme_ownCloud_Dialog).setTitle(R.string.drawer_synced_folders).setMessage(R.string.synced_folders_new_info).setPositiveButton(R.string.drawer_open, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// show instant upload
Intent syncedFoldersIntent = new Intent(getApplicationContext(), SyncedFoldersActivity.class);
dialog.dismiss();
startActivity(syncedFoldersIntent);
}
}).setNegativeButton(R.string.drawer_close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setIcon(R.drawable.nav_synced_folders).show();
new MaterialAlertDialogBuilder(this, R.style.Theme_ownCloud_Dialog).setTitle(R.string.drawer_synced_folders).setMessage(R.string.synced_folders_new_info).setPositiveButton(R.string.drawer_open, (dialog, which) -> {
// show instant upload
Intent syncedFoldersIntent = new Intent(getApplicationContext(), SyncedFoldersActivity.class);
dialog.dismiss();
startActivity(syncedFoldersIntent);
}).setNegativeButton(R.string.drawer_close, (dialog, which) -> dialog.dismiss()).setIcon(R.drawable.nav_synced_folders).show();
}
}

Expand Down Expand Up @@ -2400,12 +2399,15 @@ private void selectUserAndOpenFile(List<User> users, String fileId) {
for (int i = 0; i < userNames.length; i++) {
userNames[i] = users.get(i).getAccountName();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
builder.setTitle(R.string.common_choose_account).setItems(userNames, (dialog, which) -> {
User user = users.get(which);
openFile(user, fileId);
showLoadingDialog(getString(R.string.retrieving_file));
});

viewThemeUtils.dialog.colorMaterialAlertDialogBackground(getApplicationContext(), builder);

final AlertDialog dialog = builder.create();
dismissLoadingDialog();
dialog.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.nextcloud.client.account.User;
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.jobs.upload.FileUploadHelper;
Expand Down Expand Up @@ -104,7 +105,6 @@
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AlertDialog.Builder;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
Expand Down Expand Up @@ -135,6 +135,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
@Inject AppPreferences preferences;
@Inject LocalBroadcastManager localBroadcastManager;
@Inject SyncedFolderProvider syncedFolderProvider;

private AccountManager mAccountManager;
private Stack<String> mParents = new Stack<>();
private List<Parcelable> mStreamsToUpload;
Expand Down Expand Up @@ -205,7 +206,7 @@ protected void setAccount(Account account, boolean savedAccount) {
Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAccountType(this));
if (accounts.length == 0) {
Log_OC.i(TAG, "No ownCloud account is available");
DialogNoAccount dialog = new DialogNoAccount();
DialogNoAccount dialog = new DialogNoAccount(viewThemeUtils);
dialog.show(getSupportFragmentManager(), null);
}

Expand Down Expand Up @@ -308,10 +309,16 @@ public void selectFile(OCFile file) {
}

public static class DialogNoAccount extends DialogFragment {
private final ViewThemeUtils viewThemeUtils;

public DialogNoAccount(ViewThemeUtils viewThemeUtils) {
this.viewThemeUtils = viewThemeUtils;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new Builder(getActivity());
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext());
builder.setIcon(R.drawable.ic_warning);
builder.setTitle(R.string.uploader_wrn_no_account_title);
builder.setMessage(String.format(getString(R.string.uploader_wrn_no_account_text),
Expand All @@ -328,7 +335,8 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
startActivityForResult(intent, REQUEST_CODE__SETUP_ACCOUNT);
});
builder.setNeutralButton(R.string.uploader_wrn_no_account_quit_btn_text,
(dialog, which) -> getActivity().finish());
(dialog, which) -> requireActivity().finish());
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(requireContext(), builder);
return builder.create();
}
}
Expand Down Expand Up @@ -681,7 +689,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAuthTokenType());
if (accounts.length == 0) {
DialogNoAccount dialog = new DialogNoAccount();
DialogNoAccount dialog = new DialogNoAccount(viewThemeUtils);
dialog.show(getSupportFragmentManager(), null);
} else {
// there is no need for checking for is there more then one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.view.ViewGroup;
import android.webkit.URLUtil;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.di.Injectable;
Expand Down Expand Up @@ -82,7 +83,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
Expand Down Expand Up @@ -493,32 +493,34 @@ private void removeE2E(PreferenceCategory preferenceCategoryMore) {
preferenceCategoryMore.removePreference(preference);
} else {
preference.setOnPreferenceClickListener(p -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.FallbackTheming_Dialog);
AlertDialog alertDialog = builder.setTitle(R.string.prefs_e2e_mnemonic)
.setMessage(getString(R.string.remove_e2e_message))
.setCancelable(true)
.setNegativeButton(R.string.common_cancel, ((dialog, i) -> dialog.dismiss()))
.setPositiveButton(R.string.confirm_removal, (dialog, which) -> {
EncryptionUtils.removeE2E(arbitraryDataProvider, user);
preferenceCategoryMore.removePreference(preference);

Preference pMnemonic = findPreference("mnemonic");
if (pMnemonic != null) {
preferenceCategoryMore.removePreference(pMnemonic);
}

dialog.dismiss();
})
.create();

alertDialog.show();

showRemoveE2EAlertDialog(preferenceCategoryMore, preference);
return true;
});
}
}
}

private void showRemoveE2EAlertDialog(PreferenceCategory preferenceCategoryMore, Preference preference) {
new MaterialAlertDialogBuilder(this, R.style.FallbackTheming_Dialog)
.setTitle(R.string.prefs_e2e_mnemonic)
.setMessage(getString(R.string.remove_e2e_message))
.setCancelable(true)
.setNegativeButton(R.string.common_cancel, ((dialog, i) -> dialog.dismiss()))
.setPositiveButton(R.string.confirm_removal, (dialog, which) -> {
EncryptionUtils.removeE2E(arbitraryDataProvider, user);
preferenceCategoryMore.removePreference(preference);

Preference pMnemonic = findPreference("mnemonic");
if (pMnemonic != null) {
preferenceCategoryMore.removePreference(pMnemonic);
}

dialog.dismiss();
})
.create()
.show();
}

private void setupHelpPreference(PreferenceCategory preferenceCategoryMore) {
boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
Preference pHelp = findPreference("help");
Expand Down Expand Up @@ -791,7 +793,7 @@ private void setupGeneralCategory() {
if (storagePath.equals(newPath)) {
return true;
}
StorageMigration storageMigration = new StorageMigration(this, user, storagePath, newPath);
StorageMigration storageMigration = new StorageMigration(this, user, storagePath, newPath, viewThemeUtils);
storageMigration.setStorageMigrationProgressListener(this);
storageMigration.migrate();

Expand Down Expand Up @@ -979,22 +981,23 @@ public void handleMnemonicRequest(Intent data) {

ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl(this);
String mnemonic = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.MNEMONIC).trim();

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.FallbackTheming_Dialog);
AlertDialog alertDialog = builder.setTitle(R.string.prefs_e2e_mnemonic)
.setMessage(mnemonic)
.setNegativeButton(R.string.common_cancel, (dialog, i) -> dialog.dismiss())
.setNeutralButton(R.string.common_copy, (dialog, i) ->
ClipboardUtil.copyToClipboard(this, mnemonic, false))
.setPositiveButton(R.string.common_ok, (dialog, which) -> dialog.dismiss())
.create();

alertDialog.show();
viewThemeUtils.platform.colorTextButtons(alertDialog.getButton(AlertDialog.BUTTON_POSITIVE));
showMnemonicAlertDialogDialog(mnemonic);
}
}
}

private void showMnemonicAlertDialogDialog(String mnemonic) {
new MaterialAlertDialogBuilder(this, R.style.FallbackTheming_Dialog)
.setTitle(R.string.prefs_e2e_mnemonic)
.setMessage(mnemonic)
.setPositiveButton(R.string.common_ok, (dialog, which) -> dialog.dismiss())
.setNegativeButton(R.string.common_cancel, (dialog, i) -> dialog.dismiss())
.setNeutralButton(R.string.common_copy, (dialog, i) ->
ClipboardUtil.copyToClipboard(this, mnemonic, false))
.create()
.show();
}

@Override
@NonNull
public MenuInflater getMenuInflater() {
Expand Down
Loading

0 comments on commit 5c24ba7

Please sign in to comment.