Android Open Source - flash-your-qr Qr App Widget Provider






From Project

Back to project page flash-your-qr.

License

The source code is released under:

Apache License

If you think the Android project flash-your-qr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011 Mattias Svala/*from   w  w  w  .  j  a v  a  2 s .  c  o  m*/
 *
 * 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 com.github.qrgen;

import com.google.zxing.WriterException;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.encoder.ByteMatrix;
import com.google.zxing.qrcode.encoder.Encoder;
import com.google.zxing.qrcode.encoder.QRCode;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
import android.widget.RemoteViews;

public class QrAppWidgetProvider extends AppWidgetProvider {

    private static final String TAG = "qrgen";

    public static final String FLASH_QR = "com.github.qrgen.FLASH_QR";

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that
        // belongs to this provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            Log.i(TAG, "widget onUpdate: " + appWidgetId);

            QrData qrData = WidgetConfigure.loadConf(context, appWidgetId);
            updateAppWidget(context, appWidgetManager, appWidgetId, qrData);
        }
    }

    private static void drawIcon(Context context, RemoteViews views,
            ByteMatrix qrCode) {
        Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        QrDraw.draw(canvas, qrCode, 0, 0, 0, 0);
        views.setImageViewBitmap(R.id.button, bitmap);
    }

    static void updateAppWidget(Context context,
                                AppWidgetManager appWidgetManager,
                                int appWidgetId, QrData qrData) {
        if (qrData.data == null) {
            qrData.data = "notsetnotset";
            qrData.label = "foo";
        }

        Log.i(TAG, "widget updateAppWidget: " + appWidgetId + " " +
                qrData.data);

        // Create the intent that the widget will launch.
        Intent intent = new Intent(FLASH_QR, null, context, QrGen.class);
        intent.putExtra("source", qrData.source);
        intent.putExtra("qrdata", qrData.data);
        intent.putExtra("widgetId", appWidgetId);
        PendingIntent pendingIntent = 
        PendingIntent.getActivity(context, appWidgetId, intent, 
                                  PendingIntent.FLAG_UPDATE_CURRENT);

        // Create the view.
        RemoteViews views = new RemoteViews(context.getPackageName(),
                                            R.layout.qr_appwidget);
        if (qrData.label != null) {
            views.setTextViewText(R.id.label, qrData.label);
        }
        views.setOnClickPendingIntent(R.id.button, pendingIntent);

        try {
            Log.i(TAG, "Draw QR code.");
            QRCode code = new QRCode();
            if (qrData.data != null) {
                Encoder.encode(qrData.data, ErrorCorrectionLevel.L, code);
                drawIcon(context, views, code.getMatrix());
            }
        } catch (WriterException e) {
            Log.e(TAG, "Failed to generate QR.", e);
        }

        // Tell the widget manager
        Log.i(TAG, "Done updating.");
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}




Java Source Code List

com.github.qrgen.Contact.java
com.github.qrgen.QrAppWidgetProvider.java
com.github.qrgen.QrData.java
com.github.qrgen.QrDraw.java
com.github.qrgen.QrGen.java
com.github.qrgen.QrView.java
com.github.qrgen.WidgetConfigure.java
com.google.zxing.ChecksumException.java
com.google.zxing.EncodeHintType.java
com.google.zxing.FormatException.java
com.google.zxing.ReaderException.java
com.google.zxing.WriterException.java
com.google.zxing.common.BitArray.java
com.google.zxing.common.BitMatrix.java
com.google.zxing.common.CharacterSetECI.java
com.google.zxing.common.ECI.java
com.google.zxing.common.reedsolomon.GF256Poly.java
com.google.zxing.common.reedsolomon.GF256.java
com.google.zxing.common.reedsolomon.ReedSolomonEncoder.java
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.java
com.google.zxing.qrcode.decoder.FormatInformation.java
com.google.zxing.qrcode.decoder.Mode.java
com.google.zxing.qrcode.decoder.Version.java
com.google.zxing.qrcode.encoder.BlockPair.java
com.google.zxing.qrcode.encoder.ByteMatrix.java
com.google.zxing.qrcode.encoder.Encoder.java
com.google.zxing.qrcode.encoder.MaskUtil.java
com.google.zxing.qrcode.encoder.MatrixUtil.java
com.google.zxing.qrcode.encoder.QRCode.java