IntentIntegrator.java
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009 ZXing authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package com.google.zxing;
00018 
00019 import android.app.AlertDialog;
00020 import android.app.Activity;
00021 import android.content.ActivityNotFoundException;
00022 import android.content.DialogInterface;
00023 import android.content.Intent;
00024 import android.net.Uri;
00078 public final class IntentIntegrator {
00079   public static final int REQUEST_CODE = 0x0ba7c0de; // get it?
00080   public static final String DEFAULT_TITLE = "Install Barcode Scanner?";
00081   public static final String DEFAULT_MESSAGE =
00082       "This application requires Barcode Scanner. Would you like to install it?";
00083   public static final String DEFAULT_YES = "Yes";
00084   public static final String DEFAULT_NO = "No";
00085   private static final String PACKAGE = "com.google.zxing.client.android";
00086   // supported barcode formats
00087   public static final String PRODUCT_CODE_TYPES = "UPC_A,UPC_E,EAN_8,EAN_13";
00088   public static final String ONE_D_CODE_TYPES = PRODUCT_CODE_TYPES + ",CODE_39,CODE_93,CODE_128";
00089   public static final String QR_CODE_TYPES = "QR_CODE";
00090   public static final String ALL_CODE_TYPES = null;
00091   private IntentIntegrator() {
00092   }
00097   public static AlertDialog initiateScan(Activity activity) {
00098     return initiateScan(activity, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO);
00099   }
00105   public static AlertDialog initiateScan(Activity activity,
00106                                          int stringTitle,
00107                                          int stringMessage,
00108                                          int stringButtonYes,
00109                                          int stringButtonNo) {
00110     return initiateScan(activity,
00111                         activity.getString(stringTitle),
00112                         activity.getString(stringMessage),
00113                         activity.getString(stringButtonYes),
00114                         activity.getString(stringButtonNo));
00115   }
00128   public static AlertDialog initiateScan(Activity activity,
00129                                          CharSequence stringTitle,
00130                                          CharSequence stringMessage,
00131                                          CharSequence stringButtonYes,
00132                                          CharSequence stringButtonNo) {
00133     return initiateScan(activity,
00134                         stringTitle,
00135                         stringMessage,
00136                         stringButtonYes,
00137                         stringButtonNo,
00138                         ALL_CODE_TYPES);
00139   }
00155   public static AlertDialog initiateScan(Activity activity,
00156                                          CharSequence stringTitle,
00157                                          CharSequence stringMessage,
00158                                          CharSequence stringButtonYes,
00159                                          CharSequence stringButtonNo,
00160                                          CharSequence stringDesiredBarcodeFormats) {
00161     Intent intentScan = new Intent(PACKAGE + ".SCAN");
00162     intentScan.setPackage(PACKAGE);
00163     intentScan.addCategory(Intent.CATEGORY_DEFAULT);
00164     // check which types of codes to scan for
00165     if (stringDesiredBarcodeFormats != null) {
00166       // set the desired barcode types
00167       intentScan.putExtra("SCAN_FORMATS", stringDesiredBarcodeFormats);
00168     }
00169     try {
00170       activity.startActivityForResult(intentScan, REQUEST_CODE);
00171       return null;
00172     } catch (ActivityNotFoundException e) {
00173       return showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes, stringButtonNo);
00174     }
00175   }
00176   private static AlertDialog showDownloadDialog(final Activity activity,
00177                                                 CharSequence stringTitle,
00178                                                 CharSequence stringMessage,
00179                                                 CharSequence stringButtonYes,
00180                                                 CharSequence stringButtonNo) {
00181     AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
00182     downloadDialog.setTitle(stringTitle);
00183     downloadDialog.setMessage(stringMessage);
00184     downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
00185       @Override
00186       public void onClick(DialogInterface dialogInterface, int i) {
00187         Uri uri = Uri.parse("market://details?id=" + PACKAGE);
00188         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
00189         activity.startActivity(intent);
00190       }
00191     });
00192     downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
00193       @Override
00194       public void onClick(DialogInterface dialogInterface, int i) {}
00195     });
00196     return downloadDialog.show();
00197   }
00206   public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
00207     if (requestCode == REQUEST_CODE) {
00208       if (resultCode == Activity.RESULT_OK) {
00209         String contents = intent.getStringExtra("SCAN_RESULT");
00210         String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
00211         return new IntentResult(contents, formatName);
00212       } else {
00213         return new IntentResult(null, null);
00214       }
00215     }
00216     return null;
00217   }
00222   public static void shareText(Activity activity, CharSequence text) {
00223     shareText(activity, text, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO);
00224   }
00229   public static void shareText(Activity activity,
00230                                CharSequence text,
00231                                int stringTitle,
00232                                int stringMessage,
00233                                int stringButtonYes,
00234                                int stringButtonNo) {
00235     shareText(activity,
00236               text,
00237               activity.getString(stringTitle),
00238               activity.getString(stringMessage),
00239               activity.getString(stringButtonYes),
00240               activity.getString(stringButtonNo));
00241   }
00254   public static void shareText(Activity activity,
00255                                CharSequence text,
00256                                CharSequence stringTitle,
00257                                CharSequence stringMessage,
00258                                CharSequence stringButtonYes,
00259                                CharSequence stringButtonNo) {
00260     Intent intent = new Intent();
00261     intent.setAction(PACKAGE + ".ENCODE");
00262     intent.setPackage(PACKAGE);
00263     intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
00264     intent.putExtra("ENCODE_DATA", text);
00265     try {
00266       activity.startActivity(intent);
00267     } catch (ActivityNotFoundException e) {
00268       showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes, stringButtonNo);
00269     }
00270   }
00271 }


android_extras
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 20:13:19