Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chromoting; | |
| 6 | |
| 7 import android.app.Activity; | |
| 8 import android.app.AlertDialog; | |
| 9 import android.content.DialogInterface; | |
| 10 import android.content.SharedPreferences; | |
| 11 import android.os.Build; | |
| 12 import android.view.KeyEvent; | |
| 13 import android.view.View; | |
| 14 import android.widget.CheckBox; | |
| 15 import android.widget.TextView; | |
| 16 import android.widget.Toast; | |
| 17 | |
| 18 import org.chromium.chromoting.jni.JniInterface; | |
| 19 | |
| 20 /** | |
| 21 * This class performs the user-interaction needed to authenticate the session c onnection. This | |
| 22 * includes showing the PIN prompt and requesting tokens for third-party authent ication. | |
| 23 */ | |
| 24 public class SessionAuthenticator { | |
| 25 /** | |
| 26 * Application context used for getting user preferences, displaying UI, and fetching | |
| 27 * third-party tokens. | |
| 28 */ | |
| 29 private Chromoting mApplicationContext; | |
| 30 | |
| 31 public SessionAuthenticator(Chromoting context) { | |
| 32 mApplicationContext = context; | |
| 33 } | |
| 34 | |
| 35 public void displayAuthenticationPrompt(boolean pairingSupported) { | |
| 36 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont ext); | |
| 37 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti cate)); | |
| 38 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_ android)); | |
| 39 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); | |
| 40 | |
| 41 final View pinEntry = | |
| 42 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia log, null); | |
| 43 pinPrompt.setView(pinEntry); | |
| 44 | |
| 45 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d ialog_text); | |
| 46 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d ialog_check); | |
| 47 | |
| 48 if (!pairingSupported) { | |
| 49 pinCheckBox.setChecked(false); | |
| 50 pinCheckBox.setVisibility(View.GONE); | |
| 51 } | |
| 52 | |
| 53 pinPrompt.setPositiveButton( | |
| 54 R.string.connect_button, new DialogInterface.OnClickListener() { | |
| 55 @Override | |
| 56 public void onClick(DialogInterface dialog, int which) { | |
| 57 if (JniInterface.isConnected()) { | |
| 58 JniInterface.handleAuthenticationResponse( | |
| 59 String.valueOf(pinTextView.getText()), | |
| 60 pinCheckBox.isChecked(), Build.MODEL); | |
| 61 } else { | |
| 62 String message = | |
| 63 mApplicationContext.getString(R.string.error _network_error); | |
| 64 Toast.makeText(mApplicationContext, message, Toast.L ENGTH_LONG).show(); | |
| 65 } | |
| 66 } | |
| 67 }); | |
| 68 | |
| 69 pinPrompt.setNegativeButton( | |
| 70 R.string.cancel, new DialogInterface.OnClickListener() { | |
| 71 @Override | |
| 72 public void onClick(DialogInterface dialog, int which) { | |
| 73 JniInterface.disconnectFromHost(); | |
| 74 } | |
| 75 }); | |
| 76 | |
| 77 final AlertDialog pinDialog = pinPrompt.create(); | |
| 78 | |
| 79 pinTextView.setOnEditorActionListener( | |
| 80 new TextView.OnEditorActionListener() { | |
| 81 @Override | |
| 82 public boolean onEditorAction(TextView v, int actionId, KeyE vent event) { | |
| 83 // The user pressed enter on the keypad (equivalent to t he connect button). | |
| 84 pinDialog.getButton(AlertDialog.BUTTON_POSITIVE).perform Click(); | |
| 85 pinDialog.dismiss(); | |
| 86 return true; | |
| 87 } | |
| 88 }); | |
| 89 | |
| 90 pinDialog.setOnCancelListener( | |
| 91 new DialogInterface.OnCancelListener() { | |
| 92 @Override | |
| 93 public void onCancel(DialogInterface dialog) { | |
| 94 // The user backed out of the dialog (equivalent to the cancel button). | |
| 95 pinDialog.getButton(AlertDialog.BUTTON_NEGATIVE).perform Click(); | |
| 96 } | |
| 97 }); | |
| 98 | |
| 99 pinDialog.show(); | |
| 100 } | |
| 101 | |
| 102 /** Saves newly-received pairing credentials to permanent storage. */ | |
| 103 public void commitPairingCredentials(String host, String id, String secret) { | |
| 104 // Empty |id| indicates that pairing needs to be removed. | |
| 105 if (id.isEmpty()) { | |
| 106 mApplicationContext.getPreferences(Activity.MODE_PRIVATE).edit() | |
| 107 .remove(host + "_id") | |
| 108 .remove(host + "_secret") | |
| 109 .apply(); | |
| 110 } else { | |
| 111 mApplicationContext.getPreferences(Activity.MODE_PRIVATE).edit() | |
| 112 .putString(host + "_id", id) | |
| 113 .putString(host + "_secret", secret) | |
| 114 .apply(); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 public void fetchThirdPartyToken(String tokenUrl, String clientId, String sc ope) { | |
| 119 mApplicationContext.fetchThirdPartyToken(tokenUrl, clientId, scope); | |
|
Sergey Ulanov
2015/03/09 18:14:53
I think you can move implementation of this functi
Lambros
2015/03/14 00:42:18
Done.
| |
| 120 } | |
| 121 | |
| 122 /** Returns the pairing ID for the given host, or an empty string if not set . */ | |
| 123 public String getPairingId(String hostId) { | |
| 124 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE); | |
| 125 return prefs.getString(hostId + "_id", ""); | |
| 126 } | |
| 127 | |
| 128 /** Returns the pairing secret for the given host, or an empty string if not set. */ | |
| 129 public String getPairingSecret(String hostId) { | |
| 130 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE); | |
| 131 return prefs.getString(hostId + "_secret", ""); | |
| 132 } | |
| 133 } | |
| OLD | NEW |