Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c0e2b59 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

enh: Refactor exception handling

parent 4259b642
Loading
Loading
Loading
Loading
+30 −26
Original line number Original line Diff line number Diff line
@@ -63,6 +63,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.List;


import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;


public class AccountImporter {
public class AccountImporter {


@@ -91,7 +92,7 @@ public class AccountImporter {
                    true, null, AUTH_TOKEN_SSO, null, null);
                    true, null, AUTH_TOKEN_SSO, null, null);
            activity.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
            activity.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
        } else {
        } else {
            throw new NextcloudFilesAppNotInstalledException();
            throw new NextcloudFilesAppNotInstalledException(activity);
        }
        }
    }
    }


@@ -104,7 +105,7 @@ public class AccountImporter {
                    true, null, AUTH_TOKEN_SSO, null, null);
                    true, null, AUTH_TOKEN_SSO, null, null);
            fragment.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
            fragment.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
        } else {
        } else {
            throw new NextcloudFilesAppNotInstalledException();
            throw new NextcloudFilesAppNotInstalledException(fragment.requireContext());
        }
        }
    }
    }


@@ -122,7 +123,7 @@ public class AccountImporter {
            // Do something for lollipop and above versions
            // Do something for lollipop and above versions
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
                Log.e(TAG, "Permission not granted yet!");
                Log.e(TAG, "Permission not granted yet!");
                throw new AndroidGetAccountsPermissionNotGranted();
                throw new AndroidGetAccountsPermissionNotGranted(context);
            } else {
            } else {
                Log.d(TAG, "Permission granted!");
                Log.d(TAG, "Permission granted!");
            }
            }
@@ -191,7 +192,7 @@ public class AccountImporter {
                Log.e(TAG, "[getSingleSignOnAccount]", e);
                Log.e(TAG, "[getSingleSignOnAccount]", e);
            }
            }
        }
        }
        throw new NextcloudFilesAppAccountNotFoundException(accountName);
        throw new NextcloudFilesAppAccountNotFoundException(context, accountName);
    }
    }


    public static SingleSignOnAccount extractSingleSignOnAccountFromResponse(Intent intent, Context context) {
    public static SingleSignOnAccount extractSingleSignOnAccountFromResponse(Intent intent, Context context) {
@@ -247,7 +248,8 @@ public class AccountImporter {
                        } else {
                        } else {
                            requestAuthToken(fragment, data);
                            requestAuthToken(fragment, data);
                        }
                        }
                    } catch (NextcloudFilesAppNotSupportedException | NextcloudFilesAppAccountPermissionNotGrantedException e) {
                    } catch (NextcloudFilesAppNotSupportedException |
                             NextcloudFilesAppAccountPermissionNotGrantedException e) {
                        UiExceptionManager.showDialogForException(context, e);
                        UiExceptionManager.showDialogForException(context, e);
                    }
                    }
                    break;
                    break;
@@ -262,7 +264,8 @@ public class AccountImporter {
                        } else {
                        } else {
                            pickNewAccount(fragment);
                            pickNewAccount(fragment);
                        }
                        }
                    } catch (NextcloudFilesAppNotInstalledException  | AndroidGetAccountsPermissionNotGranted e) {
                    } catch (NextcloudFilesAppNotInstalledException |
                             AndroidGetAccountsPermissionNotGranted e) {
                        UiExceptionManager.showDialogForException(context, e);
                        UiExceptionManager.showDialogForException(context, e);
                    }
                    }
                    break;
                    break;
@@ -273,10 +276,10 @@ public class AccountImporter {
            switch (requestCode) {
            switch (requestCode) {
                case CHOOSE_ACCOUNT_SSO:
                case CHOOSE_ACCOUNT_SSO:
                    // nothing to do here
                    // nothing to do here
                    throw new AccountImportCancelledException();
                    throw new AccountImportCancelledException(context);
                case REQUEST_AUTH_TOKEN_SSO:
                case REQUEST_AUTH_TOKEN_SSO:
                    try {
                    try {
                        handleFailedAuthRequest(data);
                        handleFailedAuthRequest(context, data);
                    } catch (SSOException e) {
                    } catch (SSOException e) {
                        UiExceptionManager.showDialogForException(context, e);
                        UiExceptionManager.showDialogForException(context, e);
                    } catch (Exception e) {
                    } catch (Exception e) {
@@ -286,7 +289,7 @@ public class AccountImporter {
                    }
                    }
                    break;
                    break;
                case REQUEST_GET_ACCOUNTS_PERMISSION:
                case REQUEST_GET_ACCOUNTS_PERMISSION:
                    UiExceptionManager.showDialogForException(context, new AndroidGetAccountsPermissionNotGranted());
                    UiExceptionManager.showDialogForException(context, new AndroidGetAccountsPermissionNotGranted(context));
                    break;
                    break;
                default:
                default:
                    break;
                    break;
@@ -315,12 +318,13 @@ public class AccountImporter {
                        } else {
                        } else {
                            pickNewAccount(fragment);
                            pickNewAccount(fragment);
                        }
                        }
                    } catch (NextcloudFilesAppNotInstalledException | AndroidGetAccountsPermissionNotGranted e) {
                    } catch (NextcloudFilesAppNotInstalledException |
                             AndroidGetAccountsPermissionNotGranted e) {
                        UiExceptionManager.showDialogForException(context, e);
                        UiExceptionManager.showDialogForException(context, e);
                    }
                    }
                } else {
                } else {
                    // user declined the permission request..
                    // user declined the permission request..
                    UiExceptionManager.showDialogForException(context, new AndroidGetAccountsPermissionNotGranted());
                    UiExceptionManager.showDialogForException(context, new AndroidGetAccountsPermissionNotGranted(context));
                }
                }
                break;
                break;
            default:
            default:
@@ -329,10 +333,10 @@ public class AccountImporter {


    }
    }


    public static void handleFailedAuthRequest(Intent data) throws SSOException {
    public static void handleFailedAuthRequest(@NonNull Context context, @Nullable Intent data) throws SSOException {
        if (data != null) {
        if (data != null) {
            String exception = data.getStringExtra(NEXTCLOUD_SSO_EXCEPTION);
            String exception = data.getStringExtra(NEXTCLOUD_SSO_EXCEPTION);
            throw SSOException.parseNextcloudCustomException(new Exception(exception));
            throw SSOException.parseNextcloudCustomException(context, new Exception(exception));
        } else {
        } else {
            Log.e(TAG, "handleFailedAuthRequest failed - data is null");
            Log.e(TAG, "handleFailedAuthRequest failed - data is null");
            throw new UnknownErrorException("Authentication request failed - no details available");
            throw new UnknownErrorException("Authentication request failed - no details available");
@@ -356,7 +360,7 @@ public class AccountImporter {
        try {
        try {
            fragment.startActivityForResult(authIntent, REQUEST_AUTH_TOKEN_SSO);
            fragment.startActivityForResult(authIntent, REQUEST_AUTH_TOKEN_SSO);
        } catch (ActivityNotFoundException e) {
        } catch (ActivityNotFoundException e) {
            throw new NextcloudFilesAppNotSupportedException();
            throw new NextcloudFilesAppNotSupportedException(fragment.requireContext());
        }
        }
    }
    }


@@ -365,7 +369,7 @@ public class AccountImporter {
        try {
        try {
            activity.startActivityForResult(authIntent, REQUEST_AUTH_TOKEN_SSO);
            activity.startActivityForResult(authIntent, REQUEST_AUTH_TOKEN_SSO);
        } catch (ActivityNotFoundException e) {
        } catch (ActivityNotFoundException e) {
            throw new NextcloudFilesAppNotSupportedException();
            throw new NextcloudFilesAppNotSupportedException(activity);
        }
        }
    }
    }


@@ -373,7 +377,7 @@ public class AccountImporter {
        String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        Account account = AccountImporter.getAccountForName(context, accountName);
        Account account = AccountImporter.getAccountForName(context, accountName);
        if (account == null) {
        if (account == null) {
            throw new NextcloudFilesAppAccountPermissionNotGrantedException();
            throw new NextcloudFilesAppAccountPermissionNotGrantedException(context);
        }
        }


        String componentName = FilesAppType.findByAccountType(account.type).packageId;
        String componentName = FilesAppType.findByAccountType(account.type).packageId;
+3 −3
Original line number Original line Diff line number Diff line
@@ -164,11 +164,11 @@ public class AidlNetworkRequest extends NetworkRequest {
            if (!mBound.get()) {
            if (!mBound.get()) {
                Log.v(TAG, "[waitForApi] - api not ready yet.. waiting [" + Thread.currentThread().getName() + "]");
                Log.v(TAG, "[waitForApi] - api not ready yet.. waiting [" + Thread.currentThread().getName() + "]");
                try {
                try {
                    mBound.wait(10000); // wait up to 10 seconds
                    mBound.wait(10_000); // wait up to 10 seconds


                    // If api is still not bound after 10 seconds.. try reconnecting
                    // If api is still not bound after 10 seconds.. try reconnecting
                    if (!mBound.get()) {
                    if (!mBound.get()) {
                        throw new NextcloudApiNotRespondingException();
                        throw new NextcloudApiNotRespondingException(mContext);
                    }
                    }
                } catch (InterruptedException ex) {
                } catch (InterruptedException ex) {
                    Log.e(TAG, "WaitForAPI failed", ex);
                    Log.e(TAG, "WaitForAPI failed", ex);
@@ -194,7 +194,7 @@ public class AidlNetworkRequest extends NetworkRequest {
            // Handle Remote Exceptions
            // Handle Remote Exceptions
            if (response.getException() != null) {
            if (response.getException() != null) {
                if (response.getException().getMessage() != null) {
                if (response.getException().getMessage() != null) {
                    throw parseNextcloudCustomException(response.getException());
                    throw parseNextcloudCustomException(mContext, response.getException());
                }
                }
                throw response.getException();
                throw response.getException();
            }
            }
+9 −11
Original line number Original line Diff line number Diff line
@@ -2,17 +2,17 @@
 * Nextcloud SingleSignOn
 * Nextcloud SingleSignOn
 *
 *
 * @author Tobias Kaminsky
 * @author Tobias Kaminsky
 *
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * (at your option) any later version.
 *
 * <p>
 * This program is distributed in the hope that it will be useful,
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * GNU General Public License for more details.
 *
 * <p>
 * You should have received a copy of the GNU General Public License
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 */
@@ -24,15 +24,13 @@ import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.NonNull;


import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.model.ExceptionMessage;


public class AccountImportCancelledException extends SSOException {
public class AccountImportCancelledException extends SSOException {


    @Override
    public AccountImportCancelledException(@NonNull Context context) {
    public void loadExceptionMessage(@NonNull Context context) {
        super(
        this.em = new ExceptionMessage(
                context.getString(R.string.sso_canceled_message),
                context.getString(R.string.sso_canceled),
                R.string.sso_canceled
                context.getString(R.string.sso_canceled_message)
        );
        );
    }
    }
}
}
+4 −6
Original line number Original line Diff line number Diff line
@@ -24,15 +24,13 @@ import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.NonNull;


import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.model.ExceptionMessage;


public class AndroidGetAccountsPermissionNotGranted extends SSOException {
public class AndroidGetAccountsPermissionNotGranted extends SSOException {


    @Override
    public AndroidGetAccountsPermissionNotGranted(@NonNull Context context) {
    public void loadExceptionMessage(@NonNull Context context) {
        super(
        this.em = new ExceptionMessage(
                context.getString(R.string.android_get_accounts_permission_not_granted_exception_message),
                context.getString(R.string.android_get_accounts_permission_not_granted_exception_title),
                R.string.android_get_accounts_permission_not_granted_exception_title
                context.getString(R.string.android_get_accounts_permission_not_granted_exception_message)
        );
        );
    }
    }
}
}
+11 −16
Original line number Original line Diff line number Diff line
@@ -27,25 +27,20 @@ import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.NonNull;


import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.R;
import com.nextcloud.android.sso.model.ExceptionMessage;


public class NextcloudApiNotRespondingException extends SSOException {
public class NextcloudApiNotRespondingException extends SSOException {


    @Override
    public NextcloudApiNotRespondingException(@NonNull Context context) {
    public void loadExceptionMessage(@NonNull Context context) {
        super(
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.em = new ExceptionMessage(
                    context.getString(R.string.nextcloud_files_api_not_responding_title),
                context.getString(R.string.nextcloud_files_api_not_responding_message),
                context.getString(R.string.nextcloud_files_api_not_responding_message),
                    R.string.nextcloud_files_api_not_responding_action,
                R.string.nextcloud_files_api_not_responding_title,
                    new Intent().setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                        ? R.string.nextcloud_files_api_not_responding_action
                        : null,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                        ? new Intent().setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
                        : null
        );
        );
        } else {
            this.em = new ExceptionMessage(
                    context.getString(R.string.nextcloud_files_api_not_responding_title),
                    context.getString(R.string.nextcloud_files_api_not_responding_message)
            );
        }
    }
    }


}
}
Loading