Search in sources :

Example 16 with WallpaperManager

use of android.app.WallpaperManager in project ZI by yixia.

the class CropImage method onSaveClicked.

private void onSaveClicked() {
    // bitmap doesn't have to be read into memory
    if (mSaving)
        return;
    if (mCrop == null) {
        return;
    }
    mSaving = true;
    Rect r = mCrop.getCropRect();
    int width = r.width();
    int height = r.height();
    // If we are circle cropping, we want alpha channel, which is the
    // third param here.
    Bitmap croppedImage = Bitmap.createBitmap(width, height, mCircleCrop ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    {
        Canvas canvas = new Canvas(croppedImage);
        Rect dstRect = new Rect(0, 0, width, height);
        canvas.drawBitmap(mBitmap, r, dstRect, null);
    }
    if (mCircleCrop) {
        // OK, so what's all this about?
        // Bitmaps are inherently rectangular but we want to return
        // something that's basically a circle.  So we fill in the
        // area around the circle with alpha.  Note the all important
        // PortDuff.Mode.CLEAR.
        Canvas c = new Canvas(croppedImage);
        Path p = new Path();
        p.addCircle(width / 2F, height / 2F, width / 2F, Path.Direction.CW);
        c.clipPath(p, Region.Op.DIFFERENCE);
        c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
    }
    /* If the output is required to a specific size then scale or fill */
    if (mOutputX != 0 && mOutputY != 0) {
        if (mScale) {
            /* Scale the image to the required dimensions */
            Bitmap old = croppedImage;
            croppedImage = BitmapUtils.transform(new Matrix(), croppedImage, mOutputX, mOutputY, mScaleUp);
            if (old != croppedImage) {
                old.recycle();
            }
        } else {
            /* Don't scale the image crop it to the size requested.
				 * Create an new image with the cropped image in the center and
				 * the extra space filled.
				 */
            // Don't scale the image but instead fill it so it's the
            // required dimension
            Bitmap b = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(b);
            Rect srcRect = mCrop.getCropRect();
            Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);
            int dx = (srcRect.width() - dstRect.width()) / 2;
            int dy = (srcRect.height() - dstRect.height()) / 2;
            /* If the srcRect is too big, use the center part of it. */
            srcRect.inset(Math.max(0, dx), Math.max(0, dy));
            /* If the dstRect is too big, use the center part of it. */
            dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));
            /* Draw the cropped bitmap in the center */
            canvas.drawBitmap(mBitmap, srcRect, dstRect, null);
            /* Set the cropped bitmap as the new bitmap */
            croppedImage.recycle();
            croppedImage = b;
        }
    }
    // Return the cropped image directly or save it to the specified URI.
    Bundle myExtras = getIntent().getExtras();
    if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean("return-data"))) {
        Bundle extras = new Bundle();
        extras.putParcelable("data", croppedImage);
        setResult(RESULT_OK, (new Intent()).setAction("inline-data").putExtras(extras));
        finish();
    } else if (myExtras != null && myExtras.getBoolean("set-wallpaper")) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage(getString(R.string.cropimage_wallpaper_is_setting));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        new AsyncTask<Bitmap, Void, Boolean>() {

            protected void onPreExecute() {
                dialog.show();
            }

            protected void onPostExecute(Boolean result) {
                reset();
                if (result)
                    showToast(R.string.cropimage_wallpaper_success);
                else
                    showToast(R.string.cropimage_wallpaper_failed);
                CropImage.this.finish();
            }

            protected void onCancelled() {
                reset();
            }

            @Override
            protected Boolean doInBackground(Bitmap... image) {
                WallpaperManager myWallpaperManager = WallpaperManager.getInstance(CropImage.this);
                if (image != null && image[0] != null) {
                    try {
                        myWallpaperManager.setBitmap(image[0]);
                    } catch (IOException e) {
                        return false;
                    }
                } else {
                    return false;
                }
                return true;
            }

            private void reset() {
                dialog.dismiss();
            }

            private void showToast(int resID) {
                Toast.makeText(CropImage.this, getString(resID), Toast.LENGTH_SHORT).show();
            }
        }.execute(croppedImage);
    } else {
        final Bitmap b = croppedImage;
        BitmapUtils.startBackgroundJob(this, null, getString(R.string.cropimage_image_saving), new Runnable() {

            public void run() {
                saveOutput(b);
            }
        }, mHandler);
    }
}
Also used : Path(android.graphics.Path) Rect(android.graphics.Rect) Bundle(android.os.Bundle) Canvas(android.graphics.Canvas) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) IOException(java.io.IOException) ProgressDialog(android.app.ProgressDialog) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) WallpaperManager(android.app.WallpaperManager)

Example 17 with WallpaperManager

use of android.app.WallpaperManager in project xDrip-plus by jamorham.

the class LockScreenWallPaper method setBitmap.

@SuppressLint("WrongConstant")
public static void setBitmap(final Bitmap bitmap) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        try {
            final WallpaperManager wallpaperManager = WallpaperManager.getInstance(xdrip.getAppContext());
            final Bitmap wallpaper = BitmapUtil.getTiled(bitmap, getScreenWidth(), getScreenHeight(), isLockScreenBitmapTiled(), Pref.getString(NumberWallPreview.ViewModel.PREF_numberwall_background, null));
            wallpaperManager.setBitmap(wallpaper, null, false, FLAG_LOCK);
            wallpaper.recycle();
        } catch (Exception e) {
            UserError.Log.e(TAG, "Failed to set wallpaper: " + e);
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) WallpaperManager(android.app.WallpaperManager) SuppressLint(android.annotation.SuppressLint)

Example 18 with WallpaperManager

use of android.app.WallpaperManager in project android_packages_apps_Settings by omnirom.

the class WallpaperSuggestionActivityTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    final Application application = RuntimeEnvironment.application;
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(application);
    ShadowApplication shadowApplication = Shadows.shadowOf(application);
    shadowApplication.setSystemService(Context.WALLPAPER_SERVICE, wallpaperManager);
}
Also used : ShadowApplication(org.robolectric.shadows.ShadowApplication) ShadowApplication(org.robolectric.shadows.ShadowApplication) Application(android.app.Application) WallpaperManager(android.app.WallpaperManager) Before(org.junit.Before)

Example 19 with WallpaperManager

use of android.app.WallpaperManager in project ADWLauncher2 by boombuler.

the class WallpaperChooser method selectWallpaper.

/*
     * When using touch if you tap an image it triggers both the onItemClick and
     * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
     * set the wallpaper once.
     */
private void selectWallpaper(int position) {
    if (mIsWallpaperSet) {
        return;
    }
    mIsWallpaperSet = true;
    try {
        WallpaperManager wpm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
        wpm.setResource(mImages.get(position));
        setResult(RESULT_OK);
        finish();
    } catch (IOException e) {
        Log.e(TAG, "Failed to set wallpaper: " + e);
    }
}
Also used : IOException(java.io.IOException) WallpaperManager(android.app.WallpaperManager)

Example 20 with WallpaperManager

use of android.app.WallpaperManager in project PhoneProfilesPlus by henrichg.

the class ActivateProfileHelper method executeForWallpaper.

private static void executeForWallpaper(final Profile profile, final Context context) {
    if (profile._deviceWallpaperChange == 1) {
        final Context appContext = context.getApplicationContext();
        PPApplication.startHandlerThreadWallpaper();
        final Handler handler = new Handler(PPApplication.handlerThreadWallpaper.getLooper());
        handler.post(new Runnable() {

            @Override
            public void run() {
                PowerManager powerManager = (PowerManager) appContext.getSystemService(POWER_SERVICE);
                PowerManager.WakeLock wakeLock = null;
                if (powerManager != null) {
                    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivateProfileHelper.executeForWallpaper");
                    wakeLock.acquire(10 * 60 * 1000);
                }
                DisplayMetrics displayMetrics = new DisplayMetrics();
                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                if (wm != null) {
                    Display display = wm.getDefaultDisplay();
                    // if (android.os.Build.VERSION.SDK_INT >= 17)
                    display.getRealMetrics(displayMetrics);
                    // else
                    // display.getMetrics(displayMetrics);
                    int height = displayMetrics.heightPixels;
                    int width = displayMetrics.widthPixels;
                    if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                        // noinspection SuspiciousNameCombination
                        height = displayMetrics.widthPixels;
                        // noinspection SuspiciousNameCombination
                        width = displayMetrics.heightPixels;
                    }
                    // for lock screen no double width
                    if ((android.os.Build.VERSION.SDK_INT < 24) || (profile._deviceWallpaperFor != 2))
                        // best wallpaper width is twice screen width
                        width = width << 1;
                    Bitmap decodedSampleBitmap = BitmapManipulator.resampleBitmapUri(profile._deviceWallpaper, width, height, context);
                    if (decodedSampleBitmap != null) {
                        // set wallpaper
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                        try {
                            if (android.os.Build.VERSION.SDK_INT >= 24) {
                                int flags = WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK;
                                Rect visibleCropHint = null;
                                if (profile._deviceWallpaperFor == 1)
                                    flags = WallpaperManager.FLAG_SYSTEM;
                                if (profile._deviceWallpaperFor == 2) {
                                    flags = WallpaperManager.FLAG_LOCK;
                                    int left = 0;
                                    int right = decodedSampleBitmap.getWidth();
                                    if (decodedSampleBitmap.getWidth() > width) {
                                        left = (decodedSampleBitmap.getWidth() / 2) - (width / 2);
                                        right = (decodedSampleBitmap.getWidth() / 2) + (width / 2);
                                    }
                                    visibleCropHint = new Rect(left, 0, right, decodedSampleBitmap.getHeight());
                                }
                                // noinspection WrongConstant
                                wallpaperManager.setBitmap(decodedSampleBitmap, visibleCropHint, true, flags);
                            } else
                                wallpaperManager.setBitmap(decodedSampleBitmap);
                        } catch (IOException e) {
                            Log.e("ActivateProfileHelper.executeForWallpaper", Log.getStackTraceString(e));
                        }
                    }
                }
                if ((wakeLock != null) && wakeLock.isHeld())
                    wakeLock.release();
            }
        });
    }
}
Also used : Context(android.content.Context) Rect(android.graphics.Rect) Handler(android.os.Handler) IOException(java.io.IOException) DisplayMetrics(android.util.DisplayMetrics) WindowManager(android.view.WindowManager) PowerManager(android.os.PowerManager) Bitmap(android.graphics.Bitmap) WallpaperManager(android.app.WallpaperManager) Display(android.view.Display)

Aggregations

WallpaperManager (android.app.WallpaperManager)36 IOException (java.io.IOException)15 Bitmap (android.graphics.Bitmap)11 Intent (android.content.Intent)6 Context (android.content.Context)5 Rect (android.graphics.Rect)5 Bundle (android.os.Bundle)5 WindowManager (android.view.WindowManager)5 SuppressLint (android.annotation.SuppressLint)4 Application (android.app.Application)4 Handler (android.os.Handler)4 DisplayMetrics (android.util.DisplayMetrics)4 Display (android.view.Display)4 Before (org.junit.Before)4 Test (org.junit.Test)4 ShadowApplication (org.robolectric.shadows.ShadowApplication)4 Activity (android.app.Activity)3 ActivityNotFoundException (android.content.ActivityNotFoundException)3 Matrix (android.graphics.Matrix)3 TargetApi (android.annotation.TargetApi)2