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