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 android.accounts.cts.common;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AuthenticatorDescription;
22 import android.content.ContentProvider;
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.Parcelable;
29 
30 import java.util.concurrent.atomic.AtomicReference;
31 
32 public class AuthenticatorContentProvider extends ContentProvider {
33 
34     public static final String AUTHORITY =
35             "android.accounts.cts.unaffiliated.authenticators.provider";
36 
37     public static final int RESULT_SUCCESS = 1;
38     public static final int RESULT_FAIL = 2;
39 
40     public static final String METHOD_GET = "get";
41     public static final String METHOD_SETUP = "setup";
42     public static final String METHOD_TEARDOWN = "setup";
43 
44     public static final int ACTION_GET = 1;
45     public static final int ACTION_SETUP = 2;
46     public static final int ACTION_TEARDOWN = 3;
47 
48     public static final int ARG_UNAFFILIATED = 10;
49     public static final int ARG_AFFILIATED = 11;
50 
51     public static final String KEY_CALLBACK = "callback";
52     public static final String KEY_TX = "tx";
53 
54     public static final AtomicReference<Parcelable> sLastTx = new AtomicReference<>();
55 
setTx(Parcelable tx)56     public static void setTx(Parcelable tx) {
57         sLastTx.set(tx);
58     }
59 
60     @Override
61     // public void handleMessage(Message msg) {
call(String method, String arg, Bundle extras)62     public Bundle call(String method, String arg, Bundle extras) {
63         super.call(method, arg, extras);
64         Bundle result = new Bundle();
65         if (METHOD_GET.equals(method)) {
66             result.putParcelable(KEY_TX, sLastTx.get());
67             return result;
68         } else if (METHOD_SETUP.equals(method)) {
69             setup();
70             return result;
71         } else if (METHOD_TEARDOWN.equals(method)) {
72             teardown();
73             return result;
74         } else {
75             throw new IllegalArgumentException("Unrecognized method!");
76         }
77     }
78 
setup()79     public void setup() {
80         Context context = getContext();
81         AccountManager am = AccountManager.get(context);
82         AuthenticatorDescription[] authenticators = am.getAuthenticatorTypes();
83         for (AuthenticatorDescription a : authenticators) {
84             /*
85              * Populate relevant test information for authenticators in the
86              * same package as the TestAuthenticatorSupportHandler.
87              */
88             if (a.packageName.equals(context.getPackageName())) {
89                 for (String name : Fixtures.getFixtureAccountNames()) {
90                     Account account = new Account(name, a.type);
91                     am.addAccountExplicitly(account, Fixtures.PREFIX_PASSWORD + name, null);
92                 }
93             }
94         }
95     }
96 
teardown()97     public void teardown() {
98         Context context = getContext();
99         AccountManager am = AccountManager.get(context);
100         AuthenticatorDescription[] authenticators = am.getAuthenticatorTypes();
101         for (AuthenticatorDescription a : authenticators) {
102             /*
103              * Populate relevant test information for authenticators in the
104              * same package as the TestAuthenticatorSupportHandler.
105              */
106             if (a.packageName.equals(context.getPackageName())) {
107                 Account[] accountsToRemove = am.getAccountsByType(a.type);
108                 for (Account account : accountsToRemove) {
109                     am.removeAccountExplicitly(account);
110                 }
111             }
112         }
113     }
114 
115     @Override
onCreate()116     public boolean onCreate() {
117         return true;
118     }
119 
120     @Override
query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)121     public Cursor query(
122             Uri uri,
123             String[] projection,
124             String selection,
125             String[] selectionArgs,
126             String sortOrder) {
127         throw new UnsupportedOperationException();
128     }
129 
130     @Override
getType(Uri uri)131     public String getType(Uri uri) {
132         throw new UnsupportedOperationException();
133     }
134 
135     @Override
insert(Uri uri, ContentValues values)136     public Uri insert(Uri uri, ContentValues values) {
137         throw new UnsupportedOperationException();
138     }
139 
140     @Override
delete(Uri uri, String selection, String[] selectionArgs)141     public int delete(Uri uri, String selection, String[] selectionArgs) {
142         throw new UnsupportedOperationException();
143     }
144 
145     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)146     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
147         throw new UnsupportedOperationException();
148     }
149 }
150 
151