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

Commit dd7d456a authored by Wenyi Wang's avatar Wenyi Wang
Browse files

Catch exception in starting ContactSaveService

This CL follows ag/798285 and adds try/catch blocks for all the places
that start ContactSaveService using static methods in ContactSaveService.

Bug: 23896510
Change-Id: Ie35cf0d213386a81a662777a5b37d6f3a3fc5633
parent d5d6a763
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import android.provider.ContactsContract.RawContactsEntity;
import android.util.Log;
import android.widget.Toast;

import com.android.contacts.activities.ContactEditorBaseActivity;
import com.android.contacts.common.compat.CompatUtils;
import com.android.contacts.common.database.ContactUpdateUtils;
import com.android.contacts.common.model.AccountTypeManager;
@@ -174,6 +175,45 @@ public class ContactSaveService extends IntentService {
        sListeners.remove(listener);
    }

    /**
     * Returns true if the ContactSaveService was started successfully and false if an exception
     * was thrown and a Toast error message was displayed.
     */
    public static boolean startService(Context context, Intent intent, int saveMode) {
        try {
            context.startService(intent);
        } catch (Exception exception) {
            final int resId;
            switch (saveMode) {
                case ContactEditorBaseActivity.ContactEditor.SaveMode.SPLIT:
                    resId = R.string.contactUnlinkErrorToast;
                    break;
                case ContactEditorBaseActivity.ContactEditor.SaveMode.RELOAD:
                    resId = R.string.contactJoinErrorToast;
                    break;
                case ContactEditorBaseActivity.ContactEditor.SaveMode.CLOSE:
                    resId = R.string.contactSavedErrorToast;
                    break;
                default:
                    resId = R.string.contactGenericErrorToast;
            }
            Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    /**
     * Utility method that starts service and handles exception.
     */
    public static void startService(Context context, Intent intent) {
        try {
            context.startService(intent);
        } catch (Exception exception) {
            Toast.makeText(context, R.string.contactGenericErrorToast, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public Object getSystemService(String name) {
        Object service = super.getSystemService(name);
+1 −1
Original line number Diff line number Diff line
@@ -337,7 +337,7 @@ public class AttachPhotoActivity extends ContactsActivity {
                raw.getRawContactId() != null ? raw.getRawContactId() : -1,
                mCroppedPhotoUri
        );
        startService(intent);
        ContactSaveService.startService(this, intent);
        finish();
    }

+1 −22
Original line number Diff line number Diff line
@@ -165,28 +165,7 @@ public class CompactContactEditorFragment extends ContactEditorBaseFragment impl
                ((Activity) mContext).getClass(),
                CompactContactEditorActivity.ACTION_SAVE_COMPLETED, mUpdatedPhotos,
                JOIN_CONTACT_ID_EXTRA_KEY, joinContactId);
        try {
            mContext.startService(intent);
        } catch (Exception exception) {
            final int resId;
            switch (saveMode) {
                case ContactEditorBaseActivity.ContactEditor.SaveMode.SPLIT:
                    resId = R.string.contactUnlinkErrorToast;
                    break;
                case ContactEditorBaseActivity.ContactEditor.SaveMode.RELOAD:
                    resId = R.string.contactJoinErrorToast;
                    break;
                case ContactEditorBaseActivity.ContactEditor.SaveMode.CLOSE:
                    resId = R.string.contactSavedErrorToast;
                    break;
                default:
                    resId = R.string.contactGenericErrorToast;
            }
            Toast.makeText(mContext, resId, Toast.LENGTH_SHORT).show();
            onCancelEditConfirmed();
            return false;
        }
        return true;
        return startSaveService(mContext, intent, saveMode);
    }

    @Override
+9 −0
Original line number Diff line number Diff line
@@ -978,6 +978,15 @@ abstract public class ContactEditorBaseFragment extends Fragment implements
     */
    abstract protected boolean doSaveAction(int saveMode, Long joinContactId);

    protected boolean startSaveService(Context context, Intent intent, int saveMode) {
        final boolean result = ContactSaveService.startService(
                context, intent, saveMode);
        if (!result) {
            onCancelEditConfirmed();
        }
        return result;
    }

    //
    // State accessor methods
    //
+1 −2
Original line number Diff line number Diff line
@@ -426,8 +426,7 @@ public class ContactEditorFragment extends ContactEditorBaseFragment implements
                SAVE_MODE_EXTRA_KEY, saveMode, isEditingUserProfile(),
                ((Activity) mContext).getClass(), ContactEditorActivity.ACTION_SAVE_COMPLETED,
                mUpdatedPhotos, JOIN_CONTACT_ID_EXTRA_KEY, joinContactId);
        mContext.startService(intent);
        return true;
        return startSaveService(mContext, intent, saveMode);
    }

    @Override
Loading