• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Intent;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.OutputStreamWriter;
29 
30 /**
31  * Class to receive intents sent across profile boundaries, and read/write to content uri specified
32  * in these intents to test cross-profile content uris.
33  */
34 public class IntentReceiverActivity extends Activity {
35 
36     private static final String TAG = "IntentReceiverActivity";
37 
38     private static final String ACTION_READ_FROM_URI = "com.android.cts.action.READ_FROM_URI";
39 
40     private static final String ACTION_WRITE_TO_URI = "com.android.cts.action.WRITE_TO_URI";
41 
42     private static final String ACTION_TAKE_PERSISTABLE_URI_PERMISSION =
43             "com.android.cts.action.TAKE_PERSISTABLE_URI_PERMISSION";
44 
45     private static final String EXTRA_CAUGHT_SECURITY_EXCEPTION = "extra_caught_security_exception";
46 
onCreate(Bundle savedInstanceState)47     public void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         Intent received = getIntent();
50         String action = received.getAction();
51         Uri uri = getIntent().getClipData().getItemAt(0).getUri();
52         if (ACTION_READ_FROM_URI.equals(action)) {
53             Intent result = new Intent();
54             String message = null;
55             try {
56                 message = getFirstLineFromUri(uri);
57             } catch (SecurityException e) {
58                 Log.i(TAG, "Caught a SecurityException while trying to read " + uri, e);
59                 result.putExtra(EXTRA_CAUGHT_SECURITY_EXCEPTION, true);
60             } catch (IOException e) {
61                 Log.i(TAG, "Caught a IOException while trying to read " + uri, e);
62             }
63             Log.i(TAG, "Message received in reading test: " + message);
64             result.putExtra("extra_response", message);
65             setResult(Activity.RESULT_OK, result);
66         } else if (ACTION_WRITE_TO_URI.equals(action)) {
67             Intent result = new Intent();
68             String message = received.getStringExtra("extra_message");
69             Log.i(TAG, "Message received in writing test: " + message);
70             try {
71                 writeToUri(uri, message);
72             } catch (SecurityException e) {
73                 Log.i(TAG, "Caught a SecurityException while trying to write to " + uri, e);
74                 result.putExtra(EXTRA_CAUGHT_SECURITY_EXCEPTION, true);
75             } catch (IOException e) {
76                 Log.i(TAG, "Caught a IOException while trying to write to " + uri, e);
77             }
78             setResult(Activity.RESULT_OK, result);
79         } else if (ACTION_TAKE_PERSISTABLE_URI_PERMISSION.equals(action)) {
80             Log.i(TAG, "Taking persistable uri permission to " + uri);
81             getContentResolver().takePersistableUriPermission(uri,
82                     Intent.FLAG_GRANT_READ_URI_PERMISSION);
83             setResult(Activity.RESULT_OK);
84         }
85         finish();
86     }
87 
88     /**
89      * Returns the first line of the file associated with uri.
90      */
getFirstLineFromUri(Uri uri)91     private String getFirstLineFromUri(Uri uri) throws IOException {
92         InputStream is = getContentResolver().openInputStream(uri);
93         BufferedReader r = new BufferedReader(new InputStreamReader(is));
94         return r.readLine();
95     }
96 
writeToUri(Uri uri, String text)97     private void writeToUri(Uri uri, String text) throws IOException {
98         OutputStreamWriter writer = new OutputStreamWriter(
99                 getContentResolver().openOutputStream(uri));
100         writer.write(text);
101         writer.flush();
102         writer.close();
103     }
104 }
105