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.nfc.NdefMessage;
     26 import android.nfc.NdefRecord;
     27 import android.nfc.NfcAdapter;
     28 import android.os.Bundle;
     29 import android.os.UserManager;
     30 import android.util.Log;
     31 import android.view.View;
     32 import android.view.View.OnClickListener;
     33 import android.widget.Toast;
     34 
     35 import com.android.cts.verifier.R;
     36 
     37 import java.nio.charset.Charset;
     38 
     39 public class NfcTestActivity extends Activity {
     40     private static final String TAG = "NfcTestActivity";
     41 
     42     /* package */ static final String EXTRA_DISALLOW_BY_POLICY = "disallowByPolicy";
     43 
     44     private static final String NFC_BEAM_PACKAGE = "com.android.nfc";
     45     private static final String NFC_BEAM_ACTIVITY = "com.android.nfc.BeamShareActivity";
     46     private static final String SAMPLE_TEXT = "sample text";
     47 
     48     private ComponentName mAdminReceiverComponent;
     49     private DevicePolicyManager mDevicePolicyManager;
     50     private UserManager mUserMangaer;
     51     private NfcAdapter mNfcAdapter;
     52     private boolean mDisallowByPolicy;
     53     private boolean mAddUserRestrictionOnFinish;
     54 
     55     @Override
     56     protected void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58         setContentView(R.layout.byod_nfc_test_activity);
     59 
     60         mAdminReceiverComponent = new ComponentName(this, DeviceAdminTestReceiver.class.getName());
     61         mDevicePolicyManager = (DevicePolicyManager) getSystemService(
     62                 Context.DEVICE_POLICY_SERVICE);
     63         mUserMangaer = (UserManager) getSystemService(Context.USER_SERVICE);
     64         mAddUserRestrictionOnFinish = mUserMangaer.hasUserRestriction(
     65                 UserManager.DISALLOW_OUTGOING_BEAM);
     66         mDisallowByPolicy = getIntent().getBooleanExtra(EXTRA_DISALLOW_BY_POLICY, false);
     67         if (mDisallowByPolicy) {
     68             mDevicePolicyManager.addUserRestriction(mAdminReceiverComponent,
     69                     UserManager.DISALLOW_OUTGOING_BEAM);
     70         } else {
     71             mDevicePolicyManager.clearUserRestriction(mAdminReceiverComponent,
     72                     UserManager.DISALLOW_OUTGOING_BEAM);
     73         }
     74 
     75         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
     76         mNfcAdapter.setNdefPushMessage(getTestMessage(), this);
     77 
     78         final Intent shareIntent = new Intent(Intent.ACTION_SEND);
     79         shareIntent.putExtra(Intent.EXTRA_TEXT, SAMPLE_TEXT);
     80 
     81         findViewById(R.id.manual_beam_button).setOnClickListener(new OnClickListener() {
     82             @Override
     83             public void onClick(View view) {
     84                 mNfcAdapter.invokeBeam(NfcTestActivity.this);
     85             }
     86         });
     87         findViewById(R.id.intent_share_button).setOnClickListener(new OnClickListener() {
     88             @Override
     89             public void onClick(View view) {
     90                 // Specify the package name of NfcBeamActivity so that the tester don't need to
     91                 // select the activity manually.
     92                 shareIntent.setClassName(NFC_BEAM_PACKAGE, NFC_BEAM_ACTIVITY);
     93                 try {
     94                     startActivity(shareIntent);
     95                 } catch (ActivityNotFoundException e) {
     96                     Toast.makeText(NfcTestActivity.this,
     97                             R.string.provisioning_byod_cannot_resolve_beam_activity,
     98                             Toast.LENGTH_SHORT).show();
     99                     Log.e(TAG, "Nfc beam activity not found", e);
    100                 }
    101             }
    102         });
    103     }
    104 
    105     @Override
    106     public void finish() {
    107         if (mAddUserRestrictionOnFinish) {
    108             mDevicePolicyManager.addUserRestriction(mAdminReceiverComponent,
    109                     UserManager.DISALLOW_OUTGOING_BEAM);
    110         } else {
    111             mDevicePolicyManager.clearUserRestriction(mAdminReceiverComponent,
    112                     UserManager.DISALLOW_OUTGOING_BEAM);
    113         }
    114         super.finish();
    115     }
    116 
    117     private NdefMessage getTestMessage() {
    118         byte[] mimeBytes = "application/com.android.cts.verifier.managedprovisioning"
    119                 .getBytes(Charset.forName("US-ASCII"));
    120         byte[] id = new byte[] {1, 3, 3, 7};
    121         byte[] payload = SAMPLE_TEXT.getBytes(Charset.forName("US-ASCII"));
    122         return new NdefMessage(new NdefRecord[] {
    123                 new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, payload)
    124         });
    125     }
    126 }
    127