• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 
18 package com.example.android.pushapiauthenticator;
19 
20 import android.accounts.Account;
21 import android.accounts.AccountManager;
22 import android.accounts.AccountManagerCallback;
23 import android.accounts.AccountManagerFuture;
24 import android.accounts.AuthenticatorDescription;
25 import android.app.Activity;
26 import android.app.AlertDialog;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.content.pm.ApplicationInfo;
32 import android.content.pm.PackageManager;
33 import android.os.Bundle;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.Button;
37 import android.widget.EditText;
38 import android.widget.RadioButton;
39 import android.widget.RadioGroup;
40 import android.widget.TextView;
41 import android.widget.Toast;
42 
43 import java.util.HashMap;
44 import java.util.List;
45 
46 public class MainActivity extends Activity {
47 
48     public static final String TYPE = "com.example.android.pushapiauthenticator";
49 
50     private static AccountManager am;
51     private ComponentName authenticatorComponent;
52 
isAccountAdded(Account a)53     public boolean isAccountAdded(Account a) {
54         Account[] accounts = am.getAccountsByType(TYPE);
55         for (Account account : accounts) {
56             if (a.equals(account)) {
57                 return true;
58             }
59         }
60         return false;
61     }
62 
63     @Override
onCreate(Bundle savedInstanceState)64     protected void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66         setContentView(R.layout.activity_main);
67 
68         am = AccountManager.get(getApplicationContext());
69         final Button getAllRequestingApps = (Button) findViewById(R.id.getallrequestingapps);
70         final TextView getAllRequesting3pUids = (TextView) findViewById(R.id.requestingapps);
71 
72         final RadioGroup accountChooser = (RadioGroup) findViewById(R.id.accountGroup);
73         final RadioGroup optionChooser = (RadioGroup) findViewById(R.id.optionsGroup);
74         final RadioGroup packagesChooser = (RadioGroup) findViewById(R.id.packagesChooser);
75         final Button selectOption = (Button) findViewById(R.id.selectoptionbutton);
76         final TextView authStatus = (TextView) findViewById(R.id.authenticatorstatus);
77 
78         final Toast hitGet =
79                 Toast.makeText(getApplicationContext(), "Hit the GET Button!", Toast.LENGTH_SHORT);
80         final Toast enterPackageName = Toast.makeText(getApplicationContext(),
81                 "Choose a packageName!", Toast.LENGTH_SHORT);
82         final Toast chooseAccountWarning =
83                 Toast.makeText(getApplicationContext(), "Choose an Account!", Toast.LENGTH_SHORT);
84         final Toast chooseOptionWarning =
85                 Toast.makeText(getApplicationContext(), "Choose an Option!", Toast.LENGTH_SHORT);
86 
87         final String ACCOUNT_PASSWORD = "some password";
88         final Bundle ACCOUNT_BUNDLE = new Bundle();
89 
90         Account terraAccount = new Account("TERRA", TYPE);
91         Account aquaAccount = new Account("AQUA", TYPE);
92         Account ventusAccount = new Account("VENTUS", TYPE);
93         authenticatorComponent = new ComponentName(
94                 getApplicationContext().getPackageName(),
95                 getApplicationContext().getPackageName()
96                  + ".MyAccountauthenticatorComponent");
97 
98 
99         AlertDialog.Builder builder = new AlertDialog.Builder(this);
100         builder.setMessage("Welcome to Auth App. \nPlease make sure you have: \n\n1. Test App 1\n"
101                 + "\n2. Test App 2 \n\ninstalled for the demo. These applications"
102                 + " provide tests, use cases, and proof of concept of Account Discovery API!\n")
103                 .setTitle("WELCOME")
104                 .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
105                     @Override
106                     public void onClick(DialogInterface dialogInterface, int i) {
107                         // do nothing
108                     }
109                 });
110 
111         AlertDialog dialog = builder.create();
112         dialog.show();
113 
114         getAllRequestingApps.setOnClickListener(new View.OnClickListener() {
115             @Override
116             public void onClick(View view) {
117                 List<ApplicationInfo> list = getPackageManager().getInstalledApplications(
118                         PackageManager.GET_META_DATA);
119                 StringBuilder uidMasterString = new StringBuilder();
120                 StringBuilder packageMasterString = new StringBuilder();
121                 for (ApplicationInfo ai :list) {
122                     String label = (String) ai.processName;
123                     if (label.contains("pushapi")) {
124                         uidMasterString.append(label + "\n");
125                     }
126                 }
127                     if (uidMasterString.length() > 0) {
128                         getAllRequesting3pUids.setText(uidMasterString);
129                     } else {
130                         getAllRequesting3pUids.setText("----");
131                     }
132             }
133         });
134 
135         selectOption.setOnClickListener(new View.OnClickListener() {
136             @Override
137             public void onClick(View view) {
138                 Account currentAccount = terraAccount;
139                 int checkedAccount = accountChooser.getCheckedRadioButtonId();
140                 int checkedOption = optionChooser.getCheckedRadioButtonId();
141                 int checkedApp = packagesChooser.getCheckedRadioButtonId();
142                 if (checkedApp == -1) {
143                     enterPackageName.show();
144                 } else if (checkedAccount == -1) {
145                     chooseAccountWarning.show();
146                 } else if (checkedOption == -1) {
147                     chooseOptionWarning.show();
148                 } else {
149                     // all conditions satisfied
150                     if (checkedAccount == R.id.terrabutton) {
151                         currentAccount = terraAccount;
152                     } else if (checkedAccount == R.id.aquabutton) {
153                         currentAccount = aquaAccount;
154                     } else if (checkedAccount == R.id.ventusbutton) {
155                         currentAccount = ventusAccount;
156                     }
157                     String packageName =
158                             ((RadioButton) findViewById(checkedApp)).getText().toString();
159                     switch (checkedOption) {
160                         case R.id.visibleButton:
161                             am.setAccountVisibility(currentAccount, packageName,
162                                     AccountManager.VISIBILITY_USER_MANAGED_VISIBLE);
163                             Toast.makeText(
164                                     getApplicationContext(), "Set UM_VISIBLE(2) "
165                                             + currentAccount.name + " to " + packageName,
166                                     Toast.LENGTH_SHORT).show();
167                             break;
168                         case R.id.notVisibleButton:
169                             am.setAccountVisibility(currentAccount, packageName,
170                                     AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE);
171                             Toast.makeText(
172                                     getApplicationContext(), "Set UM_NOT_VISIBLE(4) "
173                                             + currentAccount.name + " to " + packageName,
174                                     Toast.LENGTH_SHORT).show();
175                             break;
176                         case R.id.forcedNotVisibleButton:
177                             am.setAccountVisibility(currentAccount, packageName,
178                                     AccountManager.VISIBILITY_NOT_VISIBLE);
179                             Toast.makeText(
180                                     getApplicationContext(), "Removing visibility(3) "
181                                             + currentAccount.name + " of " + packageName,
182                                     Toast.LENGTH_SHORT).show();
183                             break;
184                         case R.id.getButton:
185                             Toast.makeText(getApplicationContext(),
186                                     "Is " + currentAccount.name + " visible to " + packageName
187                                             + "?\n"
188                                             + am.getAccountVisibility(currentAccount, packageName),
189                                     Toast.LENGTH_SHORT).show();
190                             break;
191                         case R.id.addAccountButton:
192                             Toast.makeText(getApplicationContext(),
193                                     "Adding account explicitly!"
194                                             + am.addAccountExplicitly(currentAccount, null, null),
195                                     Toast.LENGTH_SHORT).show();
196                             break;
197                         case R.id.addAccountButtonWithVisibility:
198                             HashMap<String, Integer> packageAndVisibilitys = new HashMap<>();
199                             packageAndVisibilitys.put(packageName,
200                                     AccountManager.VISIBILITY_USER_MANAGED_VISIBLE);
201                             Toast.makeText(getApplicationContext(),
202                                     "Adding account explicitly!"
203                                             + am.addAccountExplicitly(currentAccount, null, null,
204                                                     packageAndVisibilitys)
205                                             + " with visibility for  " + packageName + "!",
206                                     Toast.LENGTH_SHORT).show();
207                             break;
208                         case R.id.removeAccount:
209                             Toast.makeText(getApplicationContext(),
210                                     "Removing account explicitly!"
211                                             + am.removeAccountExplicitly(currentAccount),
212                                     Toast.LENGTH_SHORT).show();
213                             break;
214                         case R.id.renameAccount:
215                             try {
216                                 AccountManagerFuture<Account> accountRenameFuture =
217                                         am.renameAccount(currentAccount, currentAccount.name + "1",
218                                                 null, null);
219                                 Account renamedAccount = accountRenameFuture.getResult();
220                                 Toast.makeText(getApplicationContext(),
221                                         "New account name " + renamedAccount, Toast.LENGTH_SHORT)
222                                         .show();
223                             } catch (Exception e) {
224                                 Toast.makeText(getApplicationContext(), "Exception" + e,
225                                         Toast.LENGTH_SHORT).show();
226                             }
227                             break;
228                         case R.id.enableComponent:
229                             Toast.makeText(getApplicationContext(),
230                                     "Enabling Component", Toast.LENGTH_SHORT).show();
231                             getPackageManager().setComponentEnabledSetting(authenticatorComponent,
232                                     PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
233                                     PackageManager.DONT_KILL_APP);
234                             break;
235                         case R.id.disableComponent:
236                             Toast.makeText(getApplicationContext(),
237                                     "Disabling Component", Toast.LENGTH_SHORT).show();
238                             getPackageManager().setComponentEnabledSetting(authenticatorComponent,
239                                     PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
240                                     PackageManager.DONT_KILL_APP);
241                             break;
242 
243                     }
244                 }
245             }
246         });
247     }
248 }
249