1 /* 2 * Copyright (C) 2009 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 package android.accounts; 17 18 import android.app.Activity; 19 import android.os.Bundle; 20 import android.widget.TextView; 21 import android.widget.ArrayAdapter; 22 import android.widget.ListView; 23 import android.view.View; 24 import android.view.LayoutInflater; 25 import android.view.ViewGroup; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.pm.PackageManager; 29 import android.text.TextUtils; 30 import com.android.internal.R; 31 32 /** 33 * @hide 34 */ 35 public class GrantCredentialsPermissionActivity extends Activity implements View.OnClickListener { 36 public static final String EXTRAS_ACCOUNT = "account"; 37 public static final String EXTRAS_AUTH_TOKEN_LABEL = "authTokenLabel"; 38 public static final String EXTRAS_AUTH_TOKEN_TYPE = "authTokenType"; 39 public static final String EXTRAS_RESPONSE = "response"; 40 public static final String EXTRAS_ACCOUNT_TYPE_LABEL = "accountTypeLabel"; 41 public static final String EXTRAS_PACKAGES = "application"; 42 public static final String EXTRAS_REQUESTING_UID = "uid"; 43 private Account mAccount; 44 private String mAuthTokenType; 45 private int mUid; 46 private Bundle mResultBundle = null; 47 onCreate(Bundle savedInstanceState)48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 getWindow().setContentView(R.layout.grant_credentials_permission); 51 mAccount = getIntent().getExtras().getParcelable(EXTRAS_ACCOUNT); 52 mAuthTokenType = getIntent().getExtras().getString(EXTRAS_AUTH_TOKEN_TYPE); 53 mUid = getIntent().getExtras().getInt(EXTRAS_REQUESTING_UID); 54 final String accountTypeLabel = 55 getIntent().getExtras().getString(EXTRAS_ACCOUNT_TYPE_LABEL); 56 final String[] packages = getIntent().getExtras().getStringArray(EXTRAS_PACKAGES); 57 58 findViewById(R.id.allow).setOnClickListener(this); 59 findViewById(R.id.deny).setOnClickListener(this); 60 61 TextView messageView = (TextView) getWindow().findViewById(R.id.message); 62 String authTokenLabel = getIntent().getExtras().getString(EXTRAS_AUTH_TOKEN_LABEL); 63 if (TextUtils.isEmpty(authTokenLabel)) { 64 CharSequence grantCredentialsPermissionFormat = getResources().getText( 65 R.string.grant_credentials_permission_message_desc); 66 messageView.setText(String.format(grantCredentialsPermissionFormat.toString(), 67 mAccount.name, accountTypeLabel)); 68 } else { 69 CharSequence grantCredentialsPermissionFormat = getResources().getText( 70 R.string.grant_credentials_permission_message_with_authtokenlabel_desc); 71 messageView.setText(String.format(grantCredentialsPermissionFormat.toString(), 72 authTokenLabel, mAccount.name, accountTypeLabel)); 73 } 74 75 String[] packageLabels = new String[packages.length]; 76 final PackageManager pm = getPackageManager(); 77 for (int i = 0; i < packages.length; i++) { 78 try { 79 packageLabels[i] = 80 pm.getApplicationLabel(pm.getApplicationInfo(packages[i], 0)).toString(); 81 } catch (PackageManager.NameNotFoundException e) { 82 packageLabels[i] = packages[i]; 83 } 84 } 85 ((ListView) findViewById(R.id.packages_list)).setAdapter( 86 new PackagesArrayAdapter(this, packageLabels)); 87 } 88 onClick(View v)89 public void onClick(View v) { 90 switch (v.getId()) { 91 case R.id.allow: 92 AccountManagerService.getSingleton().grantAppPermission(mAccount, mAuthTokenType, 93 mUid); 94 Intent result = new Intent(); 95 result.putExtra("retry", true); 96 setResult(RESULT_OK, result); 97 setAccountAuthenticatorResult(result.getExtras()); 98 break; 99 100 case R.id.deny: 101 AccountManagerService.getSingleton().revokeAppPermission(mAccount, mAuthTokenType, 102 mUid); 103 setResult(RESULT_CANCELED); 104 break; 105 } 106 finish(); 107 } 108 setAccountAuthenticatorResult(Bundle result)109 public final void setAccountAuthenticatorResult(Bundle result) { 110 mResultBundle = result; 111 } 112 113 /** 114 * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present. 115 */ finish()116 public void finish() { 117 Intent intent = getIntent(); 118 AccountAuthenticatorResponse accountAuthenticatorResponse = 119 intent.getParcelableExtra(EXTRAS_RESPONSE); 120 if (accountAuthenticatorResponse != null) { 121 // send the result bundle back if set, otherwise send an error. 122 if (mResultBundle != null) { 123 accountAuthenticatorResponse.onResult(mResultBundle); 124 } else { 125 accountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); 126 } 127 } 128 super.finish(); 129 } 130 131 private static class PackagesArrayAdapter extends ArrayAdapter<String> { 132 protected LayoutInflater mInflater; 133 private static final int mResource = R.layout.simple_list_item_1; 134 PackagesArrayAdapter(Context context, String[] items)135 public PackagesArrayAdapter(Context context, String[] items) { 136 super(context, mResource, items); 137 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 138 } 139 140 static class ViewHolder { 141 TextView label; 142 } 143 144 @Override getView(int position, View convertView, ViewGroup parent)145 public View getView(int position, View convertView, ViewGroup parent) { 146 // A ViewHolder keeps references to children views to avoid unneccessary calls 147 // to findViewById() on each row. 148 ViewHolder holder; 149 150 // When convertView is not null, we can reuse it directly, there is no need 151 // to reinflate it. We only inflate a new View when the convertView supplied 152 // by ListView is null. 153 if (convertView == null) { 154 convertView = mInflater.inflate(mResource, null); 155 156 // Creates a ViewHolder and store references to the two children views 157 // we want to bind data to. 158 holder = new ViewHolder(); 159 holder.label = (TextView) convertView.findViewById(R.id.text1); 160 161 convertView.setTag(holder); 162 } else { 163 // Get the ViewHolder back to get fast access to the TextView 164 // and the ImageView. 165 holder = (ViewHolder) convertView.getTag(); 166 } 167 168 holder.label.setText(getItem(position)); 169 170 return convertView; 171 } 172 } 173 } 174