• 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 package com.example.android.pushapithirdpartyone;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AccountManagerCallback;
22 import android.accounts.AccountManagerFuture;
23 import android.accounts.AuthenticatorDescription;
24 import android.accounts.OnAccountsUpdateListener;
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.util.Log;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.Button;
37 import android.widget.CompoundButton;
38 import android.widget.TextView;
39 import android.widget.Toast;
40 import android.widget.ToggleButton;
41 
42 public class MainActivity extends Activity {
43 
44     private static final int REQUEST_CODE_PICK_ACCOUNT = 0;
45     private static final String TAG = "PushApiTestAppOne";
46     private static AccountManager am;
47     private static OnAccountsUpdateListener mListener;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.activity_main);
53         am = AccountManager.get(getApplicationContext());
54         mListener = new OnAccountsUpdateListener() {
55             @Override
56             public void onAccountsUpdated(Account[] accounts) {
57                 Log.i(TAG, "onAccountsUpdated is called:");
58                 if (accounts != null) {
59                     for (Account account : accounts) {
60                         Log.i(TAG, "visible account: " + account);
61                     }
62                 }
63             }
64         };
65         am.addOnAccountsUpdatedListener(mListener, null, false,
66                 new String[] {"com.example.android.pushapiauthenticator", "com.google"});
67         final TextView visibleAccounts = (TextView) findViewById(R.id.visibleaccounts);
68         final Button getVisibleAccounts = (Button) findViewById(R.id.getvisibleaccounts);
69         final Toast notifOn =
70                 Toast.makeText(getApplicationContext(), "Notifs Turned On!", Toast.LENGTH_SHORT);
71         final Toast notifOff =
72                 Toast.makeText(getApplicationContext(), "Notifs Turned Off!", Toast.LENGTH_SHORT);
73         AlertDialog.Builder builder = new AlertDialog.Builder(this);
74         builder.setMessage("Welcome to Test App 1.\nPlease make sure you have:\n\n1. Test App 2\n"
75                 + "\n2. Auth App \n\ninstalled for the demo. These applications together provide"
76                 + " tests, use cases, and proof of concept of Account Discovery API!\n")
77                 .setTitle("WELCOME")
78                 .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
79                     @Override
80                     public void onClick(DialogInterface dialogInterface, int i) {
81                         // do nothing
82                     }
83                 });
84         AlertDialog dialog = builder.create();
85         dialog.show();
86         getVisibleAccounts.setOnClickListener(new View.OnClickListener() {
87             @Override
88             public void onClick(View view) {
89                 Account[] accountsAccessedByAuthApp = am.getAccounts();
90                 StringBuilder masterString = new StringBuilder();
91                 for (int i = 0; i < accountsAccessedByAuthApp.length; i++) {
92                     masterString.append(accountsAccessedByAuthApp[i].name + ", "
93                             + accountsAccessedByAuthApp[i].type + "\n");
94                 }
95                 if (masterString.length() > 0) {
96                     visibleAccounts.setText(masterString);
97                 } else {
98                     visibleAccounts.setText("----");
99                 }
100 
101                 Intent intent = AccountManager.newChooseAccountIntent(null, null, null, null, null,
102                         null, null); // Show all accounts
103                 startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
104             }
105         });
106     }
107 
108     @Override
onDestroy()109     protected void onDestroy() {
110         am.removeOnAccountsUpdatedListener(mListener);
111         super.onDestroy();
112     }
113 
114     @Override
onActivityResult(int requestCode, int resultCode, Intent data)115     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
116         if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
117             // Receiving a result from the AccountPicker
118             if (resultCode == RESULT_OK) {
119                 Toast.makeText(this, data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE),
120                         Toast.LENGTH_LONG).show();
121             } else if (resultCode == RESULT_CANCELED) {
122                 Toast.makeText(this, "No account was chosen", Toast.LENGTH_LONG).show();
123             }
124         }
125     }
126 }
127