Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Azure Mobile Engagement Android SDK
 * Copyright (c) Microsoft Corporation
 *
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.IOException;
import java.io.InputStream;

import android.app.DownloadManager;

import android.content.ContentResolver;
import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class Main {
    /**
     * Decode downloaded big picture.
     * @param context any application context.
     * @param downloadId downloaded picture identifier.
     * @return decoded bitmap if successful, null on error.
     */
    public static Bitmap getBigPicture(Context context, long downloadId) {
        /* Decode bitmap */
        InputStream stream = null;
        try {
            /*
             * Query download manager to get file. FIXME For an unknown reason, using the file descriptor
             * fails after APK build with ProGuard, we use stream instead.
             */
            DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri uri = downloadManager.getUriForDownloadedFile(downloadId);
            ContentResolver contentResolver = context.getContentResolver();
            stream = contentResolver.openInputStream(uri);

            /*
             * Bitmaps larger than 2048 in any dimension are likely to cause OutOfMemoryError in the
             * NotificationManager or even here. Plus some devices simply don't accept such images: the
             * notification manager can drop the notification without any way to check that via API. To
             * avoid the problem, sub sample the image in an efficient way (not using Bitmap matrix
             * scaling after decoding the bitmap with original size: it could run out of memory when
             * decoding the full image). FIXME There is center cropping applied by the NotificationManager
             * on the bitmap we provide, we can't avoid it, see
             * https://code.google.com/p/android/issues/detail?id=58318.
             */
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 1;
            options.inPreferQualityOverSpeed = true;

            /* Decode dimensions */
            BitmapFactory.decodeStream(stream, null, options);
            int maxDim = Math.max(options.outWidth, options.outHeight);

            /* Compute sub sample size (it must be a power of 2) */
            while (maxDim > 2048) {
                options.inSampleSize <<= 1;
                maxDim >>= 1;
            }

            /* Decode actual bitmap */
            options.inJustDecodeBounds = false;
            stream.close();
            stream = contentResolver.openInputStream(uri);
            return BitmapFactory.decodeStream(stream, null, options);
        } catch (Throwable t) {
            /* Abort, causes are likely FileNotFoundException or OutOfMemoryError */
            return null;
        } finally {
            /* Silently close stream */
            if (stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                }
        }
    }
}