Improve UI for applying wallpaper process

Signed-off-by: Joey <joey@lineageos.org>
diff --git a/app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperImpl.java b/app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperImpl.java
new file mode 100644
index 0000000..35f1a02
--- /dev/null
+++ b/app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperImpl.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2019 The LineageOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.lineageos.backgrounds.task;
+
+import android.app.WallpaperManager;
+import android.graphics.Bitmap;
+import android.graphics.drawable.Drawable;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+
+import org.lineageos.backgrounds.util.TypeConverter;
+
+import java.io.IOException;
+
+final class ApplyWallpaperImpl {
+    private static final String TAG = "ApplyWallpaperImpl";
+
+    @NonNull
+    private final Callback mCallback;
+
+    ApplyWallpaperImpl(@NonNull final Callback callback) {
+        mCallback = callback;
+    }
+
+    boolean apply(@NonNull final Drawable drawable) {
+        final Bitmap bm = TypeConverter.drawableToBitmap(drawable);
+        final WallpaperManager manager = mCallback.getWallpaperManager();
+
+        try {
+            manager.setBitmap(bm);
+            return true;
+        } catch (IOException e) {
+            Log.e(TAG, e.getMessage(), e);
+            return false;
+        }
+    }
+
+    interface Callback {
+
+        @NonNull
+        WallpaperManager getWallpaperManager();
+    }
+}
diff --git a/app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperTask.java b/app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperTask.java
new file mode 100644
index 0000000..6944e32
--- /dev/null
+++ b/app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperTask.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2019 The LineageOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.lineageos.backgrounds.task;
+
+import android.app.WallpaperManager;
+import android.graphics.drawable.Drawable;
+import android.os.AsyncTask;
+
+import androidx.annotation.NonNull;
+
+public final class ApplyWallpaperTask extends AsyncTask<Drawable, Void, Boolean> implements
+        ApplyWallpaperImpl.Callback {
+    @NonNull
+    private final Callback mCallbacks;
+
+    public ApplyWallpaperTask(@NonNull final Callback callbacks) {
+        mCallbacks = callbacks;
+    }
+
+    @Override
+    protected Boolean doInBackground(@NonNull Drawable... drawables) {
+        final Drawable drawable = drawables[0];
+        return new ApplyWallpaperImpl(this).apply(drawable);
+    }
+
+    @Override
+    protected void onPostExecute(Boolean result) {
+        super.onPostExecute(result);
+        mCallbacks.onCompleted(result);
+    }
+
+    @NonNull
+    @Override
+    public WallpaperManager getWallpaperManager() {
+        return mCallbacks.getWallpaperManager();
+    }
+
+    public interface Callback {
+        void onCompleted(final boolean result);
+
+        @NonNull
+        WallpaperManager getWallpaperManager();
+    }
+}
diff --git a/app/src/main/java/org/lineageos/backgrounds/ui/ApplyActivity.java b/app/src/main/java/org/lineageos/backgrounds/ui/ApplyActivity.java
index 9f147ad..cc5a069 100644
--- a/app/src/main/java/org/lineageos/backgrounds/ui/ApplyActivity.java
+++ b/app/src/main/java/org/lineageos/backgrounds/ui/ApplyActivity.java
@@ -26,6 +26,7 @@
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -34,13 +35,11 @@
 
 import org.lineageos.backgrounds.R;
 import org.lineageos.backgrounds.bundle.WallpaperBundle;
+import org.lineageos.backgrounds.task.ApplyWallpaperTask;
 import org.lineageos.backgrounds.task.LoadDrawableFromUriTask;
 import org.lineageos.backgrounds.util.ColorUtils;
-import org.lineageos.backgrounds.util.TypeConverter;
 import org.lineageos.backgrounds.util.UiUtils;
 
-import java.io.IOException;
-
 public final class ApplyActivity extends AppCompatActivity {
     public static final String EXTRA_TRANSITION_NAME = "transition_shared_preview";
     static final String EXTRA_WALLPAPER = "apply_extra_wallpaper_parcel";
@@ -134,18 +133,22 @@
     }
 
     private void applyWallpaper() {
+        hideApplyButton();
+
         final Drawable drawable = mPreviewView.getDrawable();
-        final WallpaperManager manager = getSystemService(WallpaperManager.class);
 
-        final Bitmap bm = TypeConverter.drawableToBitmap(drawable);
+        new ApplyWallpaperTask(new ApplyWallpaperTask.Callback() {
+            @Override
+            public void onCompleted(boolean result) {
+                onWallpaperApplied(result);
+            }
 
-        hideApplyButtonAndClose();
-
-        try {
-            manager.setBitmap(bm);
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+            @NonNull
+            @Override
+            public WallpaperManager getWallpaperManager() {
+                return getSystemService(WallpaperManager.class);
+            }
+        }).execute(drawable);
     }
 
     private void displayPreview(@Nullable final Drawable drawable) {
@@ -169,19 +172,25 @@
                 .start();
     }
 
-    private void hideApplyButtonAndClose() {
-        if (mButtonView.getVisibility() == View.GONE) {
-            return;
-        }
-
+    private void hideApplyButton() {
         mButtonView.animate()
                 .scaleX(0f)
                 .scaleY(0f)
-                .setDuration(75)
-                .withEndAction(this::finish)
+                .setDuration(250)
                 .start();
     }
 
+    private void onWallpaperApplied(final boolean success) {
+        if (success) {
+            setResult(MainActivity.RESULT_APPLIED);
+        }
+
+        Toast.makeText(this, success ? R.string.apply_success : R.string.apply_failure,
+                Toast.LENGTH_LONG).show();
+        finish();
+
+    }
+
     private void colorUi() {
         final Drawable previewDrawable = mPreviewView.getDrawable();
         final int color = ColorUtils.extractColor(ColorUtils.extractPalette(previewDrawable));
diff --git a/app/src/main/java/org/lineageos/backgrounds/ui/MainActivity.java b/app/src/main/java/org/lineageos/backgrounds/ui/MainActivity.java
index a5b8de9..f2f44ed 100644
--- a/app/src/main/java/org/lineageos/backgrounds/ui/MainActivity.java
+++ b/app/src/main/java/org/lineageos/backgrounds/ui/MainActivity.java
@@ -42,7 +42,9 @@
 import java.util.List;
 
 public final class MainActivity extends AppCompatActivity implements SelectionInterface {
+    public static final int RESULT_APPLIED = 917;
     private static final int PICK_IMAGE_FROM_EXT = 618;
+    private static final int APPLY_WALLPAPER = 619;
 
     private ProgressBar mLoadingProgressBar;
     private TextView mLoadingTextView;
@@ -51,7 +53,7 @@
     private WallsAdapter mAdapter;
 
     @Nullable
-    private View holder;
+    private View mHolder;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstance) {
@@ -72,31 +74,28 @@
         super.onResume();
 
         // Cleanup
-        if (holder != null) {
-            holder.setTransitionName("");
-            holder = null;
+        if (mHolder != null) {
+            mHolder.setTransitionName("");
+            mHolder = null;
         }
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
-        if (requestCode != PICK_IMAGE_FROM_EXT || data == null) {
+        if (requestCode == PICK_IMAGE_FROM_EXT && data != null) {
+            onPickedFromExt(data.getDataString());
             return;
         }
-        final String uri = data.getDataString();
-        if (uri == null) {
-            return;
+        if (requestCode == APPLY_WALLPAPER && resultCode == RESULT_APPLIED) {
+            // We're done
+            finish();
         }
-        // Pass a fake bundle with name as URI path
-        WallpaperBundle fakeBundle = UserWallpaperFactory.build(uri);
-        //noinspection All: we know holder is not null at this point
-        onWallpaperSelected(holder, fakeBundle);
     }
 
     @Override
     public void onWallpaperSelected(@NonNull View view, @Nullable WallpaperBundle bundle) {
-        holder = view;
+        mHolder = view;
         if (bundle == null) {
             pickWallpaperFromExternalStorage();
         } else {
@@ -159,7 +158,7 @@
     private void openPreview(@NonNull final WallpaperBundle bundle) {
         Intent intent = new Intent(this, ApplyActivity.class)
                 .putExtra(ApplyActivity.EXTRA_WALLPAPER, bundle);
-        if (holder == null) {
+        if (mHolder == null) {
             return;
         }
 
@@ -170,10 +169,20 @@
         }
 
         // Shared element transition
-        holder.setTransitionName(ApplyActivity.EXTRA_TRANSITION_NAME);
+        mHolder.setTransitionName(ApplyActivity.EXTRA_TRANSITION_NAME);
         ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
-                this, holder, ApplyActivity.EXTRA_TRANSITION_NAME);
+                this, mHolder, ApplyActivity.EXTRA_TRANSITION_NAME);
 
-        startActivity(intent, options.toBundle());
+        startActivityForResult(intent, APPLY_WALLPAPER, options.toBundle());
+    }
+
+    private void onPickedFromExt(@Nullable final String uriString) {
+        if (uriString == null) {
+            return;
+        }
+        // Pass a fake bundle with name as URI path
+        WallpaperBundle fakeBundle = UserWallpaperFactory.build(uriString);
+        //noinspection All: we know mHolder is not null at this point
+        onWallpaperSelected(mHolder, fakeBundle);
     }
 }
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 749c457..f44b5b4 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -20,6 +20,9 @@
     <string name="main_wallpaper_pick">Your images</string>
     <string name="apply_action">Apply</string>
 
+    <string name="apply_success">Wallpaper applied</string>
+    <string name="apply_failure">Unable to set this wallpaper</string>
+
     <!-- Mono wallpaper names-->
     <eat-comment />