/packages/VpnDialogs/src/com/android/vpndialogs/ConfirmDialog.java

https://github.com/aizuzi/platform_frameworks_base · Java · 107 lines · 75 code · 17 blank · 15 comment · 4 complexity · 1d0987bd6d1b26d58ac5ed32b200a657 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 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 com.android.vpndialogs;
  17. import android.content.Context;
  18. import android.content.DialogInterface;
  19. import android.content.Intent;
  20. import android.content.pm.ApplicationInfo;
  21. import android.content.pm.PackageManager;
  22. import android.net.IConnectivityManager;
  23. import android.os.ServiceManager;
  24. import android.util.Log;
  25. import android.view.View;
  26. import android.widget.Button;
  27. import android.widget.CompoundButton;
  28. import android.widget.ImageView;
  29. import android.widget.TextView;
  30. import com.android.internal.app.AlertActivity;
  31. public class ConfirmDialog extends AlertActivity implements
  32. CompoundButton.OnCheckedChangeListener, DialogInterface.OnClickListener {
  33. private static final String TAG = "VpnConfirm";
  34. private String mPackage;
  35. private IConnectivityManager mService;
  36. private Button mButton;
  37. @Override
  38. protected void onResume() {
  39. super.onResume();
  40. try {
  41. mPackage = getCallingPackage();
  42. mService = IConnectivityManager.Stub.asInterface(
  43. ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
  44. if (mService.prepareVpn(mPackage, null)) {
  45. setResult(RESULT_OK);
  46. finish();
  47. return;
  48. }
  49. PackageManager pm = getPackageManager();
  50. ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
  51. View view = View.inflate(this, R.layout.confirm, null);
  52. ((ImageView) view.findViewById(R.id.icon)).setImageDrawable(app.loadIcon(pm));
  53. ((TextView) view.findViewById(R.id.prompt)).setText(
  54. getString(R.string.prompt, app.loadLabel(pm)));
  55. ((CompoundButton) view.findViewById(R.id.check)).setOnCheckedChangeListener(this);
  56. mAlertParams.mIconAttrId = android.R.attr.alertDialogIcon;
  57. mAlertParams.mTitle = getText(android.R.string.dialog_alert_title);
  58. mAlertParams.mPositiveButtonText = getText(android.R.string.ok);
  59. mAlertParams.mPositiveButtonListener = this;
  60. mAlertParams.mNegativeButtonText = getText(android.R.string.cancel);
  61. mAlertParams.mNegativeButtonListener = this;
  62. mAlertParams.mView = view;
  63. setupAlert();
  64. getWindow().setCloseOnTouchOutside(false);
  65. mButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
  66. mButton.setEnabled(false);
  67. mButton.setFilterTouchesWhenObscured(true);
  68. } catch (Exception e) {
  69. Log.e(TAG, "onResume", e);
  70. finish();
  71. }
  72. }
  73. @Override
  74. public void onBackPressed() {
  75. }
  76. @Override
  77. public void onCheckedChanged(CompoundButton button, boolean checked) {
  78. mButton.setEnabled(checked);
  79. }
  80. @Override
  81. public void onClick(DialogInterface dialog, int which) {
  82. try {
  83. if (which == DialogInterface.BUTTON_POSITIVE && mService.prepareVpn(null, mPackage)) {
  84. setResult(RESULT_OK);
  85. }
  86. } catch (Exception e) {
  87. Log.e(TAG, "onClick", e);
  88. }
  89. }
  90. }