Search in sources :

Example 11 with WallpaperManager

use of android.app.WallpaperManager in project Fairphone by Kwamecorp.

the class UserInitializeReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    final Resources resources = context.getResources();
    // Context.getPackageName() may return the "original" package name,
    // com.android.launcher2; Resources needs the real package name,
    // com.android.launcher. So we ask Resources for what it thinks the
    // package name should be.
    final String packageName = resources.getResourcePackageName(R.array.wallpapers);
    ArrayList<Integer> list = new ArrayList<Integer>();
    addWallpapers(resources, packageName, R.array.wallpapers, list);
    addWallpapers(resources, packageName, R.array.extra_wallpapers, list);
    WallpaperManager wpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
    for (int i = 1; i < list.size(); i++) {
        int resid = list.get(i);
        if (!wpm.hasResourceWallpaper(resid)) {
            try {
                wpm.setResource(resid);
            } catch (IOException e) {
            }
            return;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Resources(android.content.res.Resources) IOException(java.io.IOException) WallpaperManager(android.app.WallpaperManager)

Example 12 with WallpaperManager

use of android.app.WallpaperManager in project Fairphone by Kwamecorp.

the class WallpaperChooserDialogFragment method selectWallpaper.

private void selectWallpaper(int position) {
    try {
        WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(Context.WALLPAPER_SERVICE);
        wpm.setResource(mImages.get(position));
        Activity activity = getActivity();
        activity.setResult(Activity.RESULT_OK);
        activity.finish();
    } catch (IOException e) {
        Log.e(TAG, "Failed to set wallpaper: " + e);
    }
}
Also used : Activity(android.app.Activity) IOException(java.io.IOException) WallpaperManager(android.app.WallpaperManager)

Example 13 with WallpaperManager

use of android.app.WallpaperManager in project AisenWeiBo by wangdan.

the class WallpaperDownloadTask method setWallpaper.

// 设置壁纸
public static void setWallpaper(Context context, File file, final OnProgressCallback callback) throws TaskException {
    long time = System.currentTimeMillis();
    try {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(GlobalContext.getInstance());
        try {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            int navigationBarHeight = SystemUtils.getNavigationBarHeight(context);
            int wallpaperWidth = width;
            int wallpaperHeight = height;
            Options opts = new Options();
            opts.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
            boolean decode = false;
            if (opts.outHeight > height + navigationBarHeight) {
                decode = true;
            }
            Logger.d(TAG, "image height = %d, screen height = %d", opts.outHeight, height + navigationBarHeight);
            if (decode) {
                // docode的时间会稍微长一点,idol3测试在1S内,所以先显示一个99%的进度
                //                    if (progress <= 0) {
                //                        progress = 999l;
                //                        total = 1000l;
                //                        publishProgress(progress, total);
                //                    }
                opts.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
                Matrix matrix = new Matrix();
                float scale = wallpaperHeight * 1.0f / bitmap.getHeight();
                matrix.setScale(scale, scale);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
                wallpaperManager.setStream(in);
                if (callback != null) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {

                        @Override
                        public void run() {
                            callback.onSetWallpaper(true);
                        }
                    });
                }
                Logger.d(TAG, "设置处理后的壁纸耗时 : " + (System.currentTimeMillis() - time));
                return;
            }
        } catch (Throwable e) {
            Logger.printExc(WallpaperDownloadTask.class, e);
        }
        wallpaperManager.setStream(new FileInputStream(file));
        Logger.d(TAG, "设置原壁纸耗时 : " + (System.currentTimeMillis() - time));
        if (callback != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    callback.onSetWallpaper(true);
                }
            });
        }
    } catch (Exception e) {
        Logger.printExc(WallpaperDownloadTask.class, e);
        if (callback != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    callback.onSetWallpaper(false);
                }
            });
        }
        throw new TaskException("", context.getResources().getString(R.string.txt_set_wallpaper_fail));
    }
}
Also used : Options(android.graphics.BitmapFactory.Options) Handler(android.os.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DisplayMetrics(android.util.DisplayMetrics) FileInputStream(java.io.FileInputStream) TaskException(org.aisen.android.network.task.TaskException) IOException(java.io.IOException) WindowManager(android.view.WindowManager) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) TaskException(org.aisen.android.network.task.TaskException) ByteArrayInputStream(java.io.ByteArrayInputStream) WallpaperManager(android.app.WallpaperManager)

Example 14 with WallpaperManager

use of android.app.WallpaperManager in project ABPlayer by winkstu.

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)

Aggregations

WallpaperManager (android.app.WallpaperManager)14 IOException (java.io.IOException)9 Bitmap (android.graphics.Bitmap)5 Matrix (android.graphics.Matrix)3 Activity (android.app.Activity)2 ProgressDialog (android.app.ProgressDialog)2 Intent (android.content.Intent)2 Resources (android.content.res.Resources)2 Canvas (android.graphics.Canvas)2 Path (android.graphics.Path)2 Rect (android.graphics.Rect)2 AsyncTask (android.os.AsyncTask)2 Bundle (android.os.Bundle)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 WallpaperInfo (android.app.WallpaperInfo)1 Options (android.graphics.BitmapFactory.Options)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 DisplayMetrics (android.util.DisplayMetrics)1