OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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.jni; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.app.AlertDialog; |
| 9 import android.content.Context; |
| 10 import android.content.DialogInterface; |
| 11 import android.graphics.Bitmap; |
| 12 import android.text.InputType; |
| 13 import android.util.Log; |
| 14 import android.widget.EditText; |
| 15 import android.widget.Toast; |
| 16 |
| 17 import org.chromium.chromoting.R; |
| 18 |
| 19 import java.nio.ByteBuffer; |
| 20 import java.nio.ByteOrder; |
| 21 |
| 22 /** |
| 23 * Initializes the Chromium remoting library, and provides JNI calls into it. |
| 24 * All interaction with the native code is centralized in this class. |
| 25 */ |
| 26 public class JniInterface { |
| 27 /** The status code indicating successful connection. */ |
| 28 private static final int SUCCESSFUL_CONNECTION = 3; |
| 29 |
| 30 /** The application context. */ |
| 31 private static Activity sContext = null; |
| 32 |
| 33 /* |
| 34 * Library-loading state machine. |
| 35 */ |
| 36 /** Whether we've already loaded the library. */ |
| 37 private static boolean sLoaded = false; |
| 38 |
| 39 /** To be called once from the main Activity. */ |
| 40 public static void loadLibrary(Activity context) { |
| 41 synchronized(JniInterface.class) { |
| 42 if (sLoaded) return; |
| 43 } |
| 44 |
| 45 System.loadLibrary("remoting_client_jni"); |
| 46 loadNative(context); |
| 47 sContext = context; |
| 48 sLoaded = true; |
| 49 } |
| 50 |
| 51 /** Performs the native portion of the initialization. */ |
| 52 private static native void loadNative(Context context); |
| 53 |
| 54 /* |
| 55 * API/OAuth2 keys access. |
| 56 */ |
| 57 public static native String getApiKey(); |
| 58 public static native String getClientId(); |
| 59 public static native String getClientSecret(); |
| 60 |
| 61 /* |
| 62 * Connection-initiating state machine. |
| 63 */ |
| 64 /** Whether the native code is attempting a connection. */ |
| 65 private static boolean sConnected = false; |
| 66 |
| 67 /** The callback to signal upon successful connection. */ |
| 68 private static Runnable sSuccessCallback = null; |
| 69 |
| 70 /** Attempts to form a connection to the user-selected host. */ |
| 71 public static void connectToHost(String username, String authToken, |
| 72 String hostJid, String hostId, String hostPubkey, Runnable successCa
llback) { |
| 73 synchronized(JniInterface.class) { |
| 74 if (!sLoaded) return; |
| 75 if (sConnected) { |
| 76 disconnectFromHost(); |
| 77 } |
| 78 } |
| 79 |
| 80 sSuccessCallback = successCallback; |
| 81 connectNative(username, authToken, hostJid, hostId, hostPubkey); |
| 82 sConnected = true; |
| 83 } |
| 84 |
| 85 /** Severs the connection and cleans up. */ |
| 86 public static void disconnectFromHost() { |
| 87 synchronized(JniInterface.class) { |
| 88 if (!sLoaded || !sConnected) return; |
| 89 } |
| 90 |
| 91 disconnectNative(); |
| 92 sSuccessCallback = null; |
| 93 sConnected = false; |
| 94 } |
| 95 |
| 96 /** Performs the native portion of the connection. */ |
| 97 private static native void connectNative( |
| 98 String username, String authToken, String hostJid, String hostId, St
ring hostPubkey); |
| 99 |
| 100 /** Performs the native portion of the cleanup. */ |
| 101 private static native void disconnectNative(); |
| 102 |
| 103 /* |
| 104 * Entry points *from* the native code. |
| 105 */ |
| 106 /** Screen width of the video feed. */ |
| 107 private static int sWidth = 0; |
| 108 |
| 109 /** Screen height of the video feed. */ |
| 110 private static int sHeight = 0; |
| 111 |
| 112 /** Buffer holding the video feed. */ |
| 113 private static ByteBuffer sBuffer = null; |
| 114 |
| 115 /** Reports whenever the connection status changes. */ |
| 116 private static void reportConnectionStatus(int state, int error) { |
| 117 if (state==SUCCESSFUL_CONNECTION) { |
| 118 sSuccessCallback.run(); |
| 119 } |
| 120 |
| 121 Toast.makeText(sContext, sContext.getResources().getStringArray( |
| 122 R.array.protoc_states)[state] + (error!=0 ? ": " + |
| 123 sContext.getResources().getStringArray(R.array.protoc_er
rors)[error] : ""), |
| 124 Toast.LENGTH_SHORT).show(); |
| 125 } |
| 126 |
| 127 /** Prompts the user to enter a PIN. */ |
| 128 private static void displayAuthenticationPrompt() { |
| 129 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(sContext); |
| 130 pinPrompt.setTitle(sContext.getString(R.string.pin_entry_title)); |
| 131 pinPrompt.setMessage(sContext.getString(R.string.pin_entry_message)); |
| 132 |
| 133 final EditText pinEntry = new EditText(sContext); |
| 134 pinEntry.setInputType( |
| 135 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PA
SSWORD); |
| 136 pinPrompt.setView(pinEntry); |
| 137 |
| 138 pinPrompt.setPositiveButton( |
| 139 R.string.pin_entry_connect, new DialogInterface.OnClickListener(
) { |
| 140 @Override |
| 141 public void onClick(DialogInterface dialog, int which) { |
| 142 Log.i("jniiface", "User provided a PIN code"); |
| 143 authenticationResponse(String.valueOf(pinEntry.getText())); |
| 144 } |
| 145 }); |
| 146 |
| 147 pinPrompt.setNegativeButton( |
| 148 R.string.pin_entry_cancel, new DialogInterface.OnClickListener()
{ |
| 149 @Override |
| 150 public void onClick(DialogInterface dialog, int which) { |
| 151 Log.i("jniiface", "User canceled pin entry prompt"); |
| 152 Toast.makeText(sContext, |
| 153 sContext.getString(R.string.msg_pin_canceled), |
| 154 Toast.LENGTH_LONG).show(); |
| 155 disconnectFromHost(); |
| 156 } |
| 157 }); |
| 158 |
| 159 pinPrompt.show(); |
| 160 } |
| 161 |
| 162 /** Forces the native graphics thread to redraw to the canvas. */ |
| 163 public static boolean redrawGraphics() { |
| 164 synchronized(JniInterface.class) { |
| 165 if (!sConnected) return false; |
| 166 } |
| 167 |
| 168 scheduleRedrawNative(); |
| 169 return true; |
| 170 } |
| 171 |
| 172 /** Performs the redrawing callback. */ |
| 173 private static void redrawGraphicsInternal() { |
| 174 // TODO(solb) Actually draw the image onto some canvas. |
| 175 } |
| 176 |
| 177 /** Performs the native response to the user's PIN. */ |
| 178 private static native void authenticationResponse(String pin); |
| 179 |
| 180 /** Schedules a redraw on the native graphics thread. */ |
| 181 private static native void scheduleRedrawNative(); |
| 182 } |
OLD | NEW |