Home | History | Annotate | Download | only in normalapp
      1 /*
      2  * Copyright (C) 2017 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.normalapp;
     18 
     19 import android.content.ContentProvider;
     20 import android.content.ContentResolver;
     21 import android.content.ContentValues;
     22 import android.content.UriMatcher;
     23 import android.content.pm.PackageInfo;
     24 import android.content.pm.PackageManager.NameNotFoundException;
     25 import android.database.CharArrayBuffer;
     26 import android.database.ContentObserver;
     27 import android.database.Cursor;
     28 import android.database.DataSetObserver;
     29 import android.database.MatrixCursor;
     30 import android.net.Uri;
     31 import android.os.Bundle;
     32 
     33 import com.android.cts.util.TestResult;
     34 
     35 public class ExposedProvider extends ContentProvider {
     36     private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
     37     static {
     38         sUriMatcher.addURI("com.android.cts.normalapp.exposed.provider", "table", 1);
     39     }
     40     private static final String[] sColumnNames = { "_ID", "name" };
     41     private static final MatrixCursor sCursor = new MatrixCursor(sColumnNames, 1);
     42     static {
     43         sCursor.newRow().add(1).add("ExposedProvider");
     44     }
     45 
     46     @Override
     47     public boolean onCreate() {
     48         return true;
     49     }
     50 
     51     @Override
     52     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
     53             String sortOrder) {
     54         boolean canAccessInstantApp = false;
     55         String exception = null;
     56         try {
     57             canAccessInstantApp = tryAccessingInstantApp();
     58         } catch (Throwable t) {
     59             exception = t.getClass().getName();
     60         }
     61 
     62         TestResult.getBuilder()
     63                 .setPackageName("com.android.cts.normalapp")
     64                 .setComponentName("ExposedProvider")
     65                 .setStatus("PASS")
     66                 .setException(exception)
     67                 .setEphemeralPackageInfoExposed(canAccessInstantApp)
     68                 .build()
     69                 .broadcast(getContext());
     70 
     71         return (sUriMatcher.match(uri) != 1) ? null : sCursor;
     72     }
     73 
     74     private boolean tryAccessingInstantApp() throws NameNotFoundException {
     75         final PackageInfo info = getContext().getPackageManager()
     76                 .getPackageInfo("com.android.cts.ephemeralapp1", 0 /*flags*/);
     77         return (info != null);
     78     }
     79 
     80     @Override
     81     public String getType(Uri uri) {
     82         return null;
     83     }
     84 
     85     @Override
     86     public Uri insert(Uri uri, ContentValues values) {
     87         return null;
     88     }
     89 
     90     @Override
     91     public int delete(Uri uri, String selection, String[] selectionArgs) {
     92         return 0;
     93     }
     94 
     95     @Override
     96     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
     97         return 0;
     98     }
     99 
    100 }
    101