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