Home | History | Annotate | Download | only in receiver
      1 /*
      2  * Copyright (C) 2014 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 package com.android.cts.intent.receiver;
     17 
     18 import android.app.Activity;
     19 import android.content.ClipboardManager;
     20 import android.content.ClipData;
     21 import android.content.Intent;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.util.Log;
     25 
     26 import java.io.BufferedReader;
     27 import java.io.IOException;
     28 import java.io.InputStream;
     29 import java.io.InputStreamReader;
     30 import java.io.OutputStreamWriter;
     31 
     32 /**
     33  * Class to receive intents sent across profile boundaries, and read/write to content uri specified
     34  * in these intents to test cross-profile content uris.
     35  */
     36 public class IntentReceiverActivity extends Activity {
     37 
     38     private static final String TAG = "IntentReceiverActivity";
     39 
     40     private static final String ACTION_COPY_TO_CLIPBOARD =
     41             "com.android.cts.action.COPY_TO_CLIPBOARD";
     42 
     43     private static final String ACTION_READ_FROM_URI =
     44             "com.android.cts.action.READ_FROM_URI";
     45 
     46     private static final String ACTION_TAKE_PERSISTABLE_URI_PERMISSION =
     47             "com.android.cts.action.TAKE_PERSISTABLE_URI_PERMISSION";
     48 
     49     private static final String ACTION_WRITE_TO_URI =
     50             "com.android.cts.action.WRITE_TO_URI";
     51 
     52     private static final String ACTION_JUST_CREATE =
     53             "com.android.cts.action.JUST_CREATE";
     54 
     55     public static final String RECEIVING_ACTIVITY_CREATED_ACTION
     56             = "com.android.cts.deviceowner.RECEIVING_ACTIVITY_CREATED_ACTION";
     57 
     58     private static final String EXTRA_CAUGHT_SECURITY_EXCEPTION = "extra_caught_security_exception";
     59 
     60     public void onCreate(Bundle savedInstanceState) {
     61         super.onCreate(savedInstanceState);
     62         final Intent received = getIntent();
     63         final String action = received.getAction();
     64         final ClipData clipData = getIntent().getClipData();
     65         final Uri uri = clipData != null ? clipData.getItemAt(0).getUri() : null;
     66         if (ACTION_COPY_TO_CLIPBOARD.equals(action)) {
     67             String text = received.getStringExtra("extra_text");
     68             Log.i(TAG, "Copying \"" + text + "\" to the clipboard");
     69             ClipData clip = ClipData.newPlainText("", text);
     70             ClipboardManager clipboard =
     71                     (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
     72             clipboard.setPrimaryClip(clip);
     73             setResult(Activity.RESULT_OK);
     74         } else if (ACTION_READ_FROM_URI.equals(action)) {
     75             Intent result = new Intent();
     76             String message = null;
     77             try {
     78                 message = getFirstLineFromUri(uri);
     79             } catch (SecurityException e) {
     80                 Log.i(TAG, "Caught a SecurityException while trying to read " + uri, e);
     81                 result.putExtra(EXTRA_CAUGHT_SECURITY_EXCEPTION, true);
     82             } catch (IOException e) {
     83                 Log.i(TAG, "Caught a IOException while trying to read " + uri, e);
     84             }
     85             Log.i(TAG, "Message received in reading test: " + message);
     86             result.putExtra("extra_response", message);
     87             setResult(Activity.RESULT_OK, result);
     88         } else if (ACTION_TAKE_PERSISTABLE_URI_PERMISSION.equals(action)) {
     89             Log.i(TAG, "Taking persistable uri permission to " + uri);
     90             getContentResolver().takePersistableUriPermission(uri,
     91                     Intent.FLAG_GRANT_READ_URI_PERMISSION);
     92             setResult(Activity.RESULT_OK);
     93         } else if (ACTION_WRITE_TO_URI.equals(action)) {
     94             Intent result = new Intent();
     95             String message = received.getStringExtra("extra_message");
     96             Log.i(TAG, "Message received in writing test: " + message);
     97             try {
     98                 writeToUri(uri, message);
     99             } catch (SecurityException e) {
    100                 Log.i(TAG, "Caught a SecurityException while trying to write to " + uri, e);
    101                 result.putExtra(EXTRA_CAUGHT_SECURITY_EXCEPTION, true);
    102             } catch (IOException e) {
    103                 Log.i(TAG, "Caught a IOException while trying to write to " + uri, e);
    104             }
    105             setResult(Activity.RESULT_OK, result);
    106         } else if (ACTION_JUST_CREATE.equals(action)) {
    107             sendBroadcast(new Intent(RECEIVING_ACTIVITY_CREATED_ACTION));
    108         }
    109         finish();
    110     }
    111 
    112     /**
    113      * Returns the first line of the file associated with uri.
    114      */
    115     private String getFirstLineFromUri(Uri uri) throws IOException {
    116         InputStream is = getContentResolver().openInputStream(uri);
    117         BufferedReader r = new BufferedReader(new InputStreamReader(is));
    118         return r.readLine();
    119     }
    120 
    121     private void writeToUri(Uri uri, String text) throws IOException {
    122         OutputStreamWriter writer = new OutputStreamWriter(
    123                 getContentResolver().openOutputStream(uri));
    124         writer.write(text);
    125         writer.flush();
    126         writer.close();
    127     }
    128 }
    129