| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.app; |
| 18 |
|
| 19 |
import android.annotation.StyleRes; |
| 20 |
import android.app.Activity; |
| 21 |
import android.app.AlertDialog; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.Intent; |
| 24 |
import android.content.pm.PackageManager; |
| 25 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 26 |
import android.content.res.Configuration; |
| 27 |
import android.os.Build; |
| 28 |
import android.os.Bundle; |
| 29 |
import android.os.Handler; |
| 30 |
import android.os.RemoteException; |
| 31 |
import android.os.UserHandle; |
| 32 |
import android.text.Html; |
| 33 |
import android.text.Spannable; |
| 34 |
import android.text.TextUtils; |
| 35 |
import android.text.method.LinkMovementMethod; |
| 36 |
import android.text.style.TypefaceSpan; |
| 37 |
import android.text.style.URLSpan; |
| 38 |
import android.util.Slog; |
| 39 |
import android.view.ContextThemeWrapper; |
| 40 |
import android.view.InflateException; |
| 41 |
import android.view.LayoutInflater; |
| 42 |
import android.view.View; |
| 43 |
import android.widget.Button; |
| 44 |
import android.widget.TextView; |
| 45 |
|
| 46 |
import com.android.internal.R; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class LogAccessDialogActivity extends Activity implements |
| 52 |
View.OnClickListener { |
| 53 |
private static final String TAG = LogAccessDialogActivity.class.getSimpleName(); |
| 54 |
public static final String EXTRA_CALLBACK = "EXTRA_CALLBACK"; |
| 55 |
|
| 56 |
|
| 57 |
private static final int DIALOG_TIME_OUT = Build.IS_DEBUGGABLE ? 60000 : 300000; |
| 58 |
private static final int MSG_DISMISS_DIALOG = 0; |
| 59 |
|
| 60 |
private String mPackageName; |
| 61 |
private int mUid; |
| 62 |
private ILogAccessDialogCallback mCallback; |
| 63 |
|
| 64 |
private String mAlertTitle; |
| 65 |
private String mAlertBody; |
| 66 |
private String mAlertLearnMore; |
| 67 |
private AlertDialog.Builder mAlertDialog; |
| 68 |
private AlertDialog mAlert; |
| 69 |
private View mAlertView; |
| 70 |
|
| 71 |
@Override |
| 72 |
protected void onCreate(Bundle savedInstanceState) { |
| 73 |
super.onCreate(savedInstanceState); |
| 74 |
|
| 75 |
|
| 76 |
if (!readIntentInfo(getIntent())) { |
| 77 |
Slog.e(TAG, "Invalid Intent extras, finishing"); |
| 78 |
finish(); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
try { |
| 84 |
mAlertTitle = getTitleString(this, mPackageName, mUid); |
| 85 |
} catch (NameNotFoundException e) { |
| 86 |
Slog.e(TAG, "Unable to fetch label of package " + mPackageName, e); |
| 87 |
declineLogAccess(); |
| 88 |
finish(); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
mAlertBody = getResources().getString(R.string.log_access_confirmation_body); |
| 93 |
mAlertLearnMore = getResources().getString(R.string.log_access_confirmation_learn_more); |
| 94 |
|
| 95 |
|
| 96 |
boolean isDarkTheme = (getResources().getConfiguration().uiMode |
| 97 |
& Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; |
| 98 |
int themeId = isDarkTheme ? android.R.style.Theme_DeviceDefault_Dialog_Alert : |
| 99 |
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert; |
| 100 |
mAlertView = createView(themeId); |
| 101 |
|
| 102 |
|
| 103 |
mAlertDialog = new AlertDialog.Builder(this, themeId); |
| 104 |
mAlertDialog.setView(mAlertView); |
| 105 |
mAlertDialog.setOnCancelListener(dialog -> declineLogAccess()); |
| 106 |
mAlertDialog.setOnDismissListener(dialog -> finish()); |
| 107 |
|
| 108 |
|
| 109 |
mAlert = mAlertDialog.create(); |
| 110 |
mAlert.getWindow().setHideOverlayWindows(true); |
| 111 |
mAlert.show(); |
| 112 |
|
| 113 |
|
| 114 |
mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG, DIALOG_TIME_OUT); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
protected void onDestroy() { |
| 119 |
super.onDestroy(); |
| 120 |
if (!isChangingConfigurations() && mAlert != null && mAlert.isShowing()) { |
| 121 |
mAlert.dismiss(); |
| 122 |
} |
| 123 |
mAlert = null; |
| 124 |
} |
| 125 |
|
| 126 |
private boolean readIntentInfo(Intent intent) { |
| 127 |
if (intent == null) { |
| 128 |
Slog.e(TAG, "Intent is null"); |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
mCallback = ILogAccessDialogCallback.Stub.asInterface( |
| 133 |
intent.getExtras().getBinder(EXTRA_CALLBACK)); |
| 134 |
if (mCallback == null) { |
| 135 |
Slog.e(TAG, "Missing callback"); |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
mPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); |
| 140 |
if (mPackageName == null || mPackageName.length() == 0) { |
| 141 |
Slog.e(TAG, "Missing package name extra"); |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
if (!intent.hasExtra(Intent.EXTRA_UID)) { |
| 146 |
Slog.e(TAG, "Missing EXTRA_UID"); |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
mUid = intent.getIntExtra(Intent.EXTRA_UID, 0); |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
private Handler mHandler = new Handler() { |
| 156 |
public void handleMessage(android.os.Message msg) { |
| 157 |
switch (msg.what) { |
| 158 |
case MSG_DISMISS_DIALOG: |
| 159 |
if (mAlert != null) { |
| 160 |
mAlert.dismiss(); |
| 161 |
mAlert = null; |
| 162 |
declineLogAccess(); |
| 163 |
} |
| 164 |
break; |
| 165 |
|
| 166 |
default: |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
private String getTitleString(Context context, String callingPackage, int uid) |
| 173 |
throws NameNotFoundException { |
| 174 |
PackageManager pm = context.getPackageManager(); |
| 175 |
|
| 176 |
CharSequence appLabel = pm.getApplicationInfoAsUser(callingPackage, |
| 177 |
PackageManager.MATCH_DIRECT_BOOT_AUTO, |
| 178 |
UserHandle.getUserId(uid)).loadLabel(pm); |
| 179 |
|
| 180 |
String titleString = context.getString( |
| 181 |
com.android.internal.R.string.log_access_confirmation_title, appLabel); |
| 182 |
|
| 183 |
return titleString; |
| 184 |
} |
| 185 |
|
| 186 |
private Spannable styleFont(String text) { |
| 187 |
Spannable s = (Spannable) Html.fromHtml(text); |
| 188 |
for (URLSpan span : s.getSpans(0, s.length(), URLSpan.class)) { |
| 189 |
TypefaceSpan typefaceSpan = new TypefaceSpan("google-sans"); |
| 190 |
s.setSpan(typefaceSpan, s.getSpanStart(span), s.getSpanEnd(span), 0); |
| 191 |
} |
| 192 |
return s; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
private View createView(@StyleRes int themeId) { |
| 201 |
Context themedContext = new ContextThemeWrapper(this, themeId); |
| 202 |
final View view = LayoutInflater.from(themedContext).inflate( |
| 203 |
R.layout.log_access_user_consent_dialog_permission, null ); |
| 204 |
|
| 205 |
if (view == null) { |
| 206 |
throw new InflateException(); |
| 207 |
} |
| 208 |
|
| 209 |
((TextView) view.findViewById(R.id.log_access_dialog_title)) |
| 210 |
.setText(mAlertTitle); |
| 211 |
|
| 212 |
if (!TextUtils.isEmpty(mAlertLearnMore)) { |
| 213 |
Spannable mSpannableLearnMore = styleFont(mAlertLearnMore); |
| 214 |
|
| 215 |
((TextView) view.findViewById(R.id.log_access_dialog_body)) |
| 216 |
.setText(TextUtils.concat(mAlertBody, "\n\n", mSpannableLearnMore)); |
| 217 |
|
| 218 |
((TextView) view.findViewById(R.id.log_access_dialog_body)) |
| 219 |
.setMovementMethod(LinkMovementMethod.getInstance()); |
| 220 |
} else { |
| 221 |
((TextView) view.findViewById(R.id.log_access_dialog_body)) |
| 222 |
.setText(mAlertBody); |
| 223 |
} |
| 224 |
|
| 225 |
Button button_allow = (Button) view.findViewById(R.id.log_access_dialog_allow_button); |
| 226 |
button_allow.setOnClickListener(this); |
| 227 |
|
| 228 |
Button button_deny = (Button) view.findViewById(R.id.log_access_dialog_deny_button); |
| 229 |
button_deny.setOnClickListener(this); |
| 230 |
|
| 231 |
return view; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public void onClick(View view) { |
| 237 |
try { |
| 238 |
switch (view.getId()) { |
| 239 |
case R.id.log_access_dialog_allow_button: |
| 240 |
mCallback.approveAccessForClient(mUid, mPackageName); |
| 241 |
finish(); |
| 242 |
break; |
| 243 |
case R.id.log_access_dialog_deny_button: |
| 244 |
declineLogAccess(); |
| 245 |
finish(); |
| 246 |
break; |
| 247 |
} |
| 248 |
} catch (RemoteException e) { |
| 249 |
finish(); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private void declineLogAccess() { |
| 254 |
try { |
| 255 |
mCallback.declineAccessForClient(mUid, mPackageName); |
| 256 |
} catch (RemoteException e) { |
| 257 |
finish(); |
| 258 |
} |
| 259 |
} |
| 260 |
} |