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