Home | History | Annotate | Download | only in managedprovisioning
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.verifier.managedprovisioning;
     18 
     19 import android.app.Activity;
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.ActivityNotFoundException;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.graphics.Bitmap;
     26 import android.graphics.Canvas;
     27 import android.graphics.Color;
     28 import android.graphics.Paint;
     29 import android.graphics.Rect;
     30 import android.net.Uri;
     31 import android.nfc.NfcAdapter;
     32 import android.os.Bundle;
     33 import android.os.UserManager;
     34 import android.support.v4.content.FileProvider;
     35 import android.util.Log;
     36 import android.view.View;
     37 import android.view.View.OnClickListener;
     38 import android.widget.Toast;
     39 
     40 import com.android.cts.verifier.R;
     41 
     42 import java.io.File;
     43 import java.io.FileNotFoundException;
     44 import java.io.FileOutputStream;
     45 
     46 public class NfcTestActivity extends Activity {
     47     private static final String TAG = "NfcTestActivity";
     48 
     49     /* package */ static final String EXTRA_DISALLOW_BY_POLICY = "disallowByPolicy";
     50 
     51     private static final String NFC_BEAM_PACKAGE = "com.android.nfc";
     52     private static final String NFC_BEAM_ACTIVITY = "com.android.nfc.BeamShareActivity";
     53     private static final String SAMPLE_IMAGE_FILENAME = "image_to_share.jpg";
     54     private static final String SAMPLE_IMAGE_CONTENT = "sample image";
     55     private static final int MARGIN = 80;
     56     private static final int TEXT_SIZE = 200;
     57 
     58     private ComponentName mAdminReceiverComponent;
     59     private DevicePolicyManager mDevicePolicyManager;
     60     private UserManager mUserMangaer;
     61     private NfcAdapter mNfcAdapter;
     62     private boolean mDisallowByPolicy;
     63 
     64     @Override
     65     protected void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67         setContentView(R.layout.byod_nfc_test_activity);
     68 
     69         mAdminReceiverComponent = new ComponentName(this, DeviceAdminTestReceiver.class.getName());
     70         mDevicePolicyManager = (DevicePolicyManager) getSystemService(
     71                 Context.DEVICE_POLICY_SERVICE);
     72         mUserMangaer = (UserManager) getSystemService(Context.USER_SERVICE);
     73         mDisallowByPolicy = getIntent().getBooleanExtra(EXTRA_DISALLOW_BY_POLICY, false);
     74         if (mDisallowByPolicy) {
     75             mDevicePolicyManager.addUserRestriction(mAdminReceiverComponent,
     76                     UserManager.DISALLOW_OUTGOING_BEAM);
     77         }
     78 
     79         final Uri uri = createUriForImage(SAMPLE_IMAGE_FILENAME, SAMPLE_IMAGE_CONTENT);
     80         Uri[] uris = new Uri[] { uri };
     81 
     82         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
     83         mNfcAdapter.setBeamPushUris(uris, this);
     84 
     85         findViewById(R.id.manual_beam_button).setOnClickListener(new OnClickListener() {
     86             @Override
     87             public void onClick(View view) {
     88                 mNfcAdapter.invokeBeam(NfcTestActivity.this);
     89             }
     90         });
     91         findViewById(R.id.intent_share_button).setOnClickListener(new OnClickListener() {
     92             @Override
     93             public void onClick(View view) {
     94                 Intent shareIntent = new Intent(Intent.ACTION_SEND);
     95                 shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
     96                 shareIntent.setType("image/jpg");
     97                 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     98                 // Specify the package name of NfcBeamActivity so that the tester don't need to
     99                 // select the activity manually.
    100                 shareIntent.setClassName(NFC_BEAM_PACKAGE, NFC_BEAM_ACTIVITY);
    101                 try {
    102                     startActivity(shareIntent);
    103                 } catch (ActivityNotFoundException e) {
    104                     Toast.makeText(NfcTestActivity.this,
    105                             R.string.provisioning_byod_cannot_resolve_beam_activity,
    106                             Toast.LENGTH_SHORT).show();
    107                     Log.e(TAG, "Nfc beam activity not found", e);
    108                 }
    109             }
    110         });
    111     }
    112 
    113     @Override
    114     public void finish() {
    115         if (mUserMangaer.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM)) {
    116             mDevicePolicyManager.clearUserRestriction(mAdminReceiverComponent,
    117                     UserManager.DISALLOW_OUTGOING_BEAM);
    118         }
    119         super.finish();
    120     }
    121 
    122     /**
    123      * Creates a Bitmap image that contains red on white text with a specified margin.
    124      * @param text Text to be displayed in the image.
    125      * @return A Bitmap image with the above specification.
    126      */
    127     private Bitmap createSampleImage(String text) {
    128         Paint paint = new Paint();
    129         paint.setStyle(Paint.Style.FILL);
    130         paint.setTextSize(TEXT_SIZE);
    131         Rect rect = new Rect();
    132         paint.getTextBounds(text, 0, text.length(), rect);
    133         int w = 2 * MARGIN + rect.right - rect.left;
    134         int h = 2 * MARGIN + rect.bottom - rect.top;
    135         Bitmap dest = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    136         Canvas canvas = new Canvas();
    137         canvas.setBitmap(dest);
    138         paint.setColor(Color.WHITE);
    139         canvas.drawPaint(paint);
    140         paint.setColor(Color.RED);
    141         canvas.drawText(text, MARGIN - rect.left, MARGIN - rect.top, paint);
    142         return dest;
    143     }
    144 
    145     private Uri createUriForImage(String name, String text) {
    146         final File file = new File(getFilesDir() + File.separator + "images"
    147                 + File.separator + name);
    148         file.getParentFile().mkdirs(); //if the folder doesn't exists it is created
    149         try {
    150             createSampleImage(text).compress(Bitmap.CompressFormat.JPEG, 100,
    151                     new FileOutputStream(file));
    152         } catch (FileNotFoundException e) {
    153             return null;
    154         }
    155         return FileProvider.getUriForFile(this,
    156                 "com.android.cts.verifier.managedprovisioning.fileprovider", file);
    157     }
    158 }
    159