VancedMicroG/src/org/microg/gms/auth/AskPermissionActivity.java

233 lines
9.0 KiB
Java
Raw Normal View History

2015-02-07 19:56:49 +00:00
/*
* Copyright 2013-2015 µg Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.microg.gms.auth;
2015-02-09 22:16:37 +00:00
import android.accounts.Account;
2015-02-10 02:33:30 +00:00
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.Context;
2015-02-09 22:16:37 +00:00
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
2015-02-07 19:56:49 +00:00
import android.os.Bundle;
2015-02-09 22:16:37 +00:00
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
2015-02-07 19:56:49 +00:00
import com.google.android.gms.R;
2015-03-06 19:08:47 +00:00
import org.microg.gms.common.PackageUtils;
2015-02-09 22:16:37 +00:00
import org.microg.gms.userinfo.ProfileManager;
import java.io.IOException;
2015-02-10 02:33:30 +00:00
public class AskPermissionActivity extends AccountAuthenticatorActivity {
public static final String EXTRA_FROM_ACCOUNT_MANAGER = "from_account_manager";
2015-02-09 22:16:37 +00:00
private static final String TAG = "GmsAuthAskPermission";
private Account account;
private String packageName;
private String service;
private boolean fromAccountManager = false;
2015-02-09 22:16:37 +00:00
2015-02-07 19:56:49 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_permission);
2015-02-09 22:16:37 +00:00
// This makes the dialog take up the full width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
getWindow().setAttributes(lp);
2015-02-10 02:33:30 +00:00
account = new Account(getIntent().getStringExtra(AccountManager.KEY_ACCOUNT_NAME),
getIntent().getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));
packageName = getIntent().getStringExtra(AccountManager.KEY_ANDROID_PACKAGE_NAME);
service = getIntent().getStringExtra(AccountManager.KEY_AUTHTOKEN);
if (getIntent().hasExtra(EXTRA_FROM_ACCOUNT_MANAGER)) fromAccountManager = true;
2015-02-10 02:33:30 +00:00
int callerUid = getIntent().getIntExtra(AccountManager.KEY_CALLER_UID, 0);
2015-03-06 19:08:47 +00:00
PackageUtils.checkPackageUid(this, packageName, callerUid);
2015-02-10 02:33:30 +00:00
2015-02-09 22:16:37 +00:00
// receive package info
PackageManager packageManager = getPackageManager();
ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, e);
finish();
return;
}
CharSequence appLabel = packageManager.getApplicationLabel(applicationInfo);
Drawable appIcon = packageManager.getApplicationIcon(applicationInfo);
Bitmap profileIcon = ProfileManager.getProfilePicture(this, account, false);
2015-02-10 02:33:30 +00:00
// receive profile icon
2015-02-09 22:16:37 +00:00
if (profileIcon != null) {
((ImageView) findViewById(R.id.account_photo)).setImageBitmap(profileIcon);
} else {
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap profileIcon = ProfileManager.getProfilePicture(AskPermissionActivity.this, account, true);
runOnUiThread(new Runnable() {
@Override
public void run() {
((ImageView) findViewById(R.id.account_photo)).setImageBitmap(profileIcon);
}
});
}
}).start();
}
((ImageView) findViewById(R.id.app_icon)).setImageDrawable(appIcon);
if (isOAuth()) {
((TextView) findViewById(R.id.title)).setText(getString(R.string.ask_scope_permission_title, appLabel));
} else {
((TextView) findViewById(R.id.title)).setText(getString(R.string.ask_service_permission_title, appLabel));
}
2015-02-09 22:16:37 +00:00
findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAllow();
}
});
findViewById(android.R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDeny();
}
});
((ListView) findViewById(R.id.permissions)).setAdapter(new PermissionAdapter());
}
public void onAllow() {
AuthManager.storePermission(this, account, packageName, service);
findViewById(android.R.id.button1).setEnabled(false);
findViewById(android.R.id.button2).setEnabled(false);
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
findViewById(R.id.no_progress_bar).setVisibility(View.GONE);
new Thread(new Runnable() {
@Override
public void run() {
Context context = AskPermissionActivity.this;
2015-03-06 19:08:47 +00:00
String sig = PackageUtils.firstSignatureDigest(context, packageName);
AuthRequest request = new AuthRequest().fromContext(context)
.email(account.name)
.token(AccountManager.get(context).getPassword(account))
.service(service)
.app(packageName, sig)
.hasPermission();
if (fromAccountManager) {
request.callerIsGms().calledFromAccountManager();
} else {
request.callerIsApp();
}
try {
AuthResponse response = request.getResponse();
AuthManager.storeResponse(context, account, packageName, sig, service, response);
Bundle result = new Bundle();
result.putString(AccountManager.KEY_AUTHTOKEN, response.auth);
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME, packageName);
result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
setAccountAuthenticatorResult(result);
} catch (IOException e) {
Log.w(TAG, e);
}
finish();
}
}).start();
2015-02-09 22:16:37 +00:00
}
public void onDeny() {
finish();
}
2015-02-10 02:33:30 +00:00
@Override
public void finish() {
super.finish();
}
private boolean isOAuth() {
return service.startsWith("oauth2:") || service.startsWith("oauth:");
}
2015-02-09 22:16:37 +00:00
private class PermissionAdapter extends BaseAdapter {
2015-02-09 22:16:37 +00:00
@Override
public int getCount() {
if (isOAuth()) {
return service.split(" ").length;
}
return 1;
}
@Override
public String getItem(int position) {
if (isOAuth()) {
String tokens = service.split(":", 2)[1];
return tokens.split(" ")[position];
}
return service;
}
@Override
public long getItemId(int position) {
return getItem(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
String label = "unknown";
String labelResourceId;
if (isOAuth()) {
if (item.startsWith("https://")) {
labelResourceId = "permission_scope_" + item.substring(8).replace("/", "_").replace("-", "_");
} else {
labelResourceId = "permission_scope_" + item.replace("/", "_").replace("-", "_");
2015-02-09 22:16:37 +00:00
}
} else {
labelResourceId = "permission_service_" + item + "_label";
}
int labelResource = getResources().getIdentifier(labelResourceId, "string", getPackageName());
if (labelResource != 0) {
label = getString(labelResource);
2015-02-09 22:16:37 +00:00
}
View view = convertView;
if (view == null) {
view = LayoutInflater.from(AskPermissionActivity.this).inflate(R.layout.ask_permission_list_entry, null);
}
2015-02-10 02:33:30 +00:00
((TextView) view.findViewById(android.R.id.text1)).setText(label);
2015-02-09 22:16:37 +00:00
return view;
}
2015-02-07 19:56:49 +00:00
}
}