Loading app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperImpl.java 0 → 100644 +57 −0 Original line number Diff line number Diff line /* * 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(); } } app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperTask.java 0 → 100644 +57 −0 Original line number Diff line number Diff line /* * 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(); } } app/src/main/java/org/lineageos/backgrounds/ui/ApplyActivity.java +28 −19 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; Loading @@ -34,13 +35,11 @@ import androidx.core.content.ContextCompat; 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"; Loading Loading @@ -134,18 +133,22 @@ public final class ApplyActivity extends AppCompatActivity { } private void applyWallpaper() { final Drawable drawable = mPreviewView.getDrawable(); final WallpaperManager manager = getSystemService(WallpaperManager.class); hideApplyButton(); final Bitmap bm = TypeConverter.drawableToBitmap(drawable); final Drawable drawable = mPreviewView.getDrawable(); hideApplyButtonAndClose(); new ApplyWallpaperTask(new ApplyWallpaperTask.Callback() { @Override public void onCompleted(boolean result) { onWallpaperApplied(result); } 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) { Loading @@ -169,19 +172,25 @@ public final class ApplyActivity extends AppCompatActivity { .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)); Loading app/src/main/java/org/lineageos/backgrounds/ui/MainActivity.java +26 −17 Original line number Diff line number Diff line Loading @@ -42,7 +42,9 @@ import org.lineageos.backgrounds.task.FetchDataTask; 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; Loading @@ -51,7 +53,7 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn private WallsAdapter mAdapter; @Nullable private View holder; private View mHolder; @Override protected void onCreate(@Nullable Bundle savedInstance) { Loading @@ -72,31 +74,28 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn 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 { Loading Loading @@ -159,7 +158,7 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn 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; } Loading @@ -170,10 +169,20 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn } // 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); startActivityForResult(intent, APPLY_WALLPAPER, options.toBundle()); } startActivity(intent, 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); } } app/src/main/res/values/strings.xml +3 −0 Original line number Diff line number Diff line Loading @@ -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 /> Loading Loading
app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperImpl.java 0 → 100644 +57 −0 Original line number Diff line number Diff line /* * 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(); } }
app/src/main/java/org/lineageos/backgrounds/task/ApplyWallpaperTask.java 0 → 100644 +57 −0 Original line number Diff line number Diff line /* * 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(); } }
app/src/main/java/org/lineageos/backgrounds/ui/ApplyActivity.java +28 −19 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; Loading @@ -34,13 +35,11 @@ import androidx.core.content.ContextCompat; 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"; Loading Loading @@ -134,18 +133,22 @@ public final class ApplyActivity extends AppCompatActivity { } private void applyWallpaper() { final Drawable drawable = mPreviewView.getDrawable(); final WallpaperManager manager = getSystemService(WallpaperManager.class); hideApplyButton(); final Bitmap bm = TypeConverter.drawableToBitmap(drawable); final Drawable drawable = mPreviewView.getDrawable(); hideApplyButtonAndClose(); new ApplyWallpaperTask(new ApplyWallpaperTask.Callback() { @Override public void onCompleted(boolean result) { onWallpaperApplied(result); } 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) { Loading @@ -169,19 +172,25 @@ public final class ApplyActivity extends AppCompatActivity { .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)); Loading
app/src/main/java/org/lineageos/backgrounds/ui/MainActivity.java +26 −17 Original line number Diff line number Diff line Loading @@ -42,7 +42,9 @@ import org.lineageos.backgrounds.task.FetchDataTask; 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; Loading @@ -51,7 +53,7 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn private WallsAdapter mAdapter; @Nullable private View holder; private View mHolder; @Override protected void onCreate(@Nullable Bundle savedInstance) { Loading @@ -72,31 +74,28 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn 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 { Loading Loading @@ -159,7 +158,7 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn 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; } Loading @@ -170,10 +169,20 @@ public final class MainActivity extends AppCompatActivity implements SelectionIn } // 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); startActivityForResult(intent, APPLY_WALLPAPER, options.toBundle()); } startActivity(intent, 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); } }
app/src/main/res/values/strings.xml +3 −0 Original line number Diff line number Diff line Loading @@ -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 /> Loading