Merged PR 2873: Updating Android sample for 0.6.2 release.

Updating Android sample for 0.6.2 release.
AppServiceClientConnection is now AppServiceConnection
App no longer needs to tell Platform to suspend/resume.
This commit is contained in:
Darren Korman 2017-08-25 00:42:42 +00:00
Родитель 2608e42254
Коммит 2a90a97766
4 изменённых файлов: 188 добавлений и 109 удалений

Просмотреть файл

@ -51,6 +51,6 @@ dependencies {
compile 'com.android.support:design:23.2.0' compile 'com.android.support:design:23.2.0'
compile 'com.android.support:cardview-v7:23.2.0' compile 'com.android.support:cardview-v7:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.0' compile 'com.android.support:recyclerview-v7:23.2.0'
compile(group: 'com.microsoft.connecteddevices', name: 'connecteddevices-sdk-armv7', version: '0.4.0', ext: 'aar', classifier: 'release') compile(group: 'com.microsoft.connecteddevices', name: 'connecteddevices-sdk-armeabi-v7a', version: '0.6.2', ext: 'aar', classifier: 'release')
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
} }

Просмотреть файл

@ -8,11 +8,17 @@ import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@ -22,16 +28,18 @@ import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.microsoft.connecteddevices.AppServiceClientConnectionClosedStatus; import com.microsoft.connecteddevices.AppServiceConnection;
import com.microsoft.connecteddevices.AppServiceClientConnection; import com.microsoft.connecteddevices.AppServiceConnectionClosedStatus;
import com.microsoft.connecteddevices.AppServiceClientConnectionStatus; import com.microsoft.connecteddevices.AppServiceConnectionStatus;
import com.microsoft.connecteddevices.AppServiceClientResponse; import com.microsoft.connecteddevices.AppServiceResponse;
import com.microsoft.connecteddevices.AppServiceRequest;
import com.microsoft.connecteddevices.AppServiceResponseStatus; import com.microsoft.connecteddevices.AppServiceResponseStatus;
import com.microsoft.connecteddevices.ConnectedDevicesException; import com.microsoft.connecteddevices.ConnectedDevicesException;
import com.microsoft.connecteddevices.IAppServiceClientConnectionListener; import com.microsoft.connecteddevices.IAppServiceConnectionListener;
import com.microsoft.connecteddevices.IAppServiceResponseListener; import com.microsoft.connecteddevices.IAppServiceResponseListener;
import com.microsoft.connecteddevices.IAppServiceResponseStatusListener;
import com.microsoft.connecteddevices.IAppServiceRequestListener;
import com.microsoft.connecteddevices.IRemoteLauncherListener; import com.microsoft.connecteddevices.IRemoteLauncherListener;
import com.microsoft.connecteddevices.Platform;
import com.microsoft.connecteddevices.RemoteLaunchUriStatus; import com.microsoft.connecteddevices.RemoteLaunchUriStatus;
import com.microsoft.connecteddevices.RemoteLauncher; import com.microsoft.connecteddevices.RemoteLauncher;
import com.microsoft.connecteddevices.RemoteSystemConnectionRequest; import com.microsoft.connecteddevices.RemoteSystemConnectionRequest;
@ -42,9 +50,16 @@ import static com.microsoft.connecteddevices.RemoteLaunchUriStatus.SUCCESS;
public class DeviceActivity extends Activity implements AdapterView.OnItemSelectedListener { public class DeviceActivity extends Activity implements AdapterView.OnItemSelectedListener {
private static final String TAG = DeviceActivity.class.getName(); private static final String TAG = DeviceActivity.class.getName();
private static final String APP_SERVICE = ""; // Fill in your app service name // Event log colors
private static final String APP_IDENTIFIER = ""; // Fill in your app identifier private static final int APP_SERVICE_CONNECTION_COLOR = (0xff) << 24 | (0x52) << 16 | (0x6d) << 8 | (0x9b);
private static final int APP_SERVICE_REQUEST_COLOR = (0xff) << 24 | (0x01) << 16 | (0x00) << 8 | (0x72);
private static final int APP_SERVICE_RESPONSE_COLOR = (0xff) << 24 | (0x61) << 16 | (0x00) << 8 | (0x8e);
private static final int LAUNCH_URI_COLOR = (0xff) << 24 | (0x72) << 16 | (0x5d) << 8 | (0x00);
private static final int SUCCESS_COLOR = (0xff) << 24 | (0x00) << 16 | (0x8e) << 8 | (0x17);
private static final int FAILURE_COLOR = (0xff) << 24 | (0x8e) << 16 | (0x00) << 8 | (0x00);
private static final String APP_SERVICE = "com.microsoft.test.cdppingpongservice";
private static final String APP_IDENTIFIER = "5085ShawnHenry.RomanTestApp_jsjw7knzsgcce";
private static final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss"; private static final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";
private static final String TIME_STAMP_FORMAT = "HH:mm:ss.SSS"; private static final String TIME_STAMP_FORMAT = "HH:mm:ss.SSS";
@ -52,12 +67,12 @@ public class DeviceActivity extends Activity implements AdapterView.OnItemSelect
private Device device; private Device device;
private TextView _launchLog; private TextView _launchLog;
private EditText _launchUriEditText; private EditText _launchUriEditText;
private Button _sendPingButton; private Button _sendPingButton;
private Button _connectButton; private Button _connectButton;
private String _id = null; private String _id;
private AppServiceClientConnection _appServiceClientConnection; private long _messageId = 0;
private AppServiceConnection _appServiceConnection;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -112,45 +127,75 @@ public class DeviceActivity extends Activity implements AdapterView.OnItemSelect
}); });
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
public void onConnectClick() { public void onConnectClick() {
if (device.getSystem() != null) { if (device.getSystem() != null) {
_connectButton.setEnabled(false); _connectButton.setEnabled(false);
logMessage("Waiting for connection response"); LogMessage(LogLevel.Info, "Waiting for connection response", APP_SERVICE_CONNECTION_COLOR);
connectAppService(new RemoteSystemConnectionRequest(device.getSystem())); connectAppService(new RemoteSystemConnectionRequest(device.getSystem()));
} }
} }
public void onSendPingClick() { public void onSendPingClick() {
logMessage("Waiting for ping response"); final long messageId = ++_messageId;
Bundle message = new Bundle(); LogMessage(LogLevel.Verbose, "Waiting for ping response for message [" + Long.toString(messageId) + "]", APP_SERVICE_REQUEST_COLOR);
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
message.putString("Type", "ping"); Bundle message = CreatePingMessage();
message.putString("CreationDate", df.format(new Date()));
message.putString("TargetId", _id);
try { try {
_appServiceClientConnection.sendMessageAsync(message); _appServiceConnection.sendMessageAsync(message, new IAppServiceResponseListener() {
@Override
public void responseReceived(AppServiceResponse response) {
AppServiceResponseStatus status = response.getStatus();
if (status == AppServiceResponseStatus.SUCCESS)
{
Bundle bundle = response.getMessage();
LogMessage(LogLevel.Info, "Received successful AppService response to message [" + Long.toString(messageId) + "]", SUCCESS_COLOR);
String dateStr = bundle.getString("CreationDate");
DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
try {
Date startDate = df.parse(dateStr);
Date nowDate = new Date();
long diff = nowDate.getTime() - startDate.getTime();
runOnUiThread(new SetPingText(Long.toString(diff)));
} catch (ParseException e) {
e.printStackTrace();
}
}
else
{
LogMessage(LogLevel.Error, "Did not receive successful AppService response", FAILURE_COLOR);
}
}
});
} catch (ConnectedDevicesException e) { } catch (ConnectedDevicesException e) {
LogMessage(LogLevel.Error, "Failed to send Ping request through AppServices", FAILURE_COLOR);
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override private Bundle CreatePingMessage() {
public void onResume() { Bundle bundle = new Bundle();
super.onResume(); DateFormat df = new SimpleDateFormat(DATE_FORMAT);
Platform.resume(); bundle.putString("Type", "ping");
bundle.putString("CreationDate", df.format(new Date()));
bundle.putString("TargetId", _id);
return bundle;
} }
@Override private Bundle CreatePongMessage(Bundle bundle) {
public void onPause() { bundle.putString("Type", "pong");
Platform.suspend(); return bundle;
super.onPause();
} }
public void onLaunchClick() { public void onLaunchClick() {
if (device.getSystem() != null) { if (device.getSystem() != null) {
logMessage("Launching Uri");
launchUri(new RemoteSystemConnectionRequest(device.getSystem())); launchUri(new RemoteSystemConnectionRequest(device.getSystem()));
} }
} }
@ -165,43 +210,31 @@ public class DeviceActivity extends Activity implements AdapterView.OnItemSelect
} }
private void connectAppService(RemoteSystemConnectionRequest connectionRequest) { private void connectAppService(RemoteSystemConnectionRequest connectionRequest) {
_appServiceClientConnection = new AppServiceClientConnection(APP_SERVICE, APP_IDENTIFIER, connectionRequest, _appServiceConnection = new AppServiceConnection(APP_SERVICE, APP_IDENTIFIER, connectionRequest,
new AppServiceClientConnectionListener(), new AppServiceConnectionListener(),
new IAppServiceResponseListener() { new IAppServiceRequestListener() {
@Override @Override
public void responseReceived(AppServiceClientResponse response) { public void requestReceived(AppServiceRequest request) {
AppServiceResponseStatus status = response.getStatus(); LogMessage(LogLevel.Info, "Received AppService request. Sending response.", APP_SERVICE_RESPONSE_COLOR);
if (status == AppServiceResponseStatus.SUCCESS) Bundle message = CreatePongMessage(request.getMessage());
{ request.sendResponseAsync(message, new IAppServiceResponseStatusListener() {
Bundle bundle = response.getMessage(); @Override
Log.i(TAG, "Received successful AppService response"); public void statusReceived(AppServiceResponseStatus status) {
logMessage("Received successful AppService response"); if (status == AppServiceResponseStatus.SUCCESS) {
LogMessage(LogLevel.Info, "Successfully sent response.", SUCCESS_COLOR);
String dateStr = bundle.getString("CreationDate"); } else {
LogMessage(LogLevel.Info, "Failed to send response.", FAILURE_COLOR);
DateFormat df = new SimpleDateFormat(DATE_FORMAT); }
try {
Date startDate = df.parse(dateStr);
Date nowDate = new Date();
long diff = nowDate.getTime() - startDate.getTime();
runOnUiThread(new SetPingText(Long.toString(diff)));
} catch (ParseException e) {
e.printStackTrace();
} }
} });
else
{
Log.e(TAG, "IAppServiceResponseListener.responseReceived status != SUCCESS");
logMessage("Did not receive successful AppService response");
}
} }
}); });
_id = connectionRequest.getRemoteSystem().getId(); _id = connectionRequest.getRemoteSystem().getId();
try { try {
_appServiceClientConnection.openRemoteAsync(); _appServiceConnection.openRemoteAsync();
} catch (ConnectedDevicesException e) { } catch (ConnectedDevicesException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -217,9 +250,9 @@ public class DeviceActivity extends Activity implements AdapterView.OnItemSelect
private void launchUri(RemoteSystemConnectionRequest connectionRequest) { private void launchUri(RemoteSystemConnectionRequest connectionRequest) {
try { try {
String url = _launchUriEditText.getText().toString(); Uri uri = Uri.parse(_launchUriEditText.getText().toString());
logMessage("Launching URI: " + url + " on " + connectionRequest.getRemoteSystem().getDisplayName()); LogMessage(LogLevel.Info, "Launching URI on " + connectionRequest.getRemoteSystem().getDisplayName(), LAUNCH_URI_COLOR);
new RemoteLauncher().LaunchUriAsync(connectionRequest, url, RemoteLauncher.LaunchUriAsync(connectionRequest, uri,
new IRemoteLauncherListener() { new IRemoteLauncherListener() {
@Override @Override
public void onCompleted(RemoteLaunchUriStatus status) { public void onCompleted(RemoteLaunchUriStatus status) {
@ -227,32 +260,72 @@ public class DeviceActivity extends Activity implements AdapterView.OnItemSelect
if (status == SUCCESS) if (status == SUCCESS)
{ {
message = "Launch succeeded"; message = "Launch succeeded";
Log.i(TAG, message); LogMessage(LogLevel.Info, message, SUCCESS_COLOR);
} }
else else
{ {
message = "Launch failed with status " + status.toString(); message = "Launch failed with status " + status.toString();
Log.e(TAG, message); LogMessage(LogLevel.Error, message, FAILURE_COLOR);
} }
logMessage(message);
DeviceActivity.this.runOnUiThread(new PrintToast(message)); DeviceActivity.this.runOnUiThread(new PrintToast(message));
} }
}); });
} catch (ConnectedDevicesException e) { } catch (ConnectedDevicesException e) {
e.printStackTrace(); LogMessage(LogLevel.Error, "Failed to launch URI. Error: " + e.getMessage(), FAILURE_COLOR);
} catch (android.net.ParseException e) {
LogMessage(LogLevel.Error, "Failed to parse provided URI. Error: " + e.getMessage(), FAILURE_COLOR);
} }
} }
private void logMessage(final String message) { private enum LogLevel {
Error(0),
Warning(1),
Info(2),
Verbose(3);
public final int id;
LogLevel(int id) {
this.id = id;
}
}
private void LogMessage(final LogLevel level, final String message, final int color) {
final String levelStr;
switch (level) {
case Error:
Log.e(TAG, message);
levelStr = "Error";
break;
case Warning:
Log.w(TAG, message);
levelStr = "Warning";
break;
case Info:
Log.i(TAG, message);
levelStr = "Info";
break;
case Verbose:
Log.v(TAG, message);
levelStr = "Verbose";
break;
default:
levelStr = "Unknown";
}
DeviceActivity.this.runOnUiThread(new Runnable() { DeviceActivity.this.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
if (_launchLog == null) { if (_launchLog == null) {
return; return;
} }
CharSequence prevText = _launchLog.getText();
String newText = prevText + "\n" + message + " ["+getTimeStamp()+"]"; String newText = "\n" + "[" + getTimeStamp() + "] " + message;
_launchLog.setText(newText);
Spannable insertText = new SpannableString(newText);
insertText.setSpan(new ForegroundColorSpan(color), 0, newText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
_launchLog.append(insertText);
} }
}); });
} }
@ -265,38 +338,68 @@ public class DeviceActivity extends Activity implements AdapterView.OnItemSelect
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }
}; }
} }
private String getTimeStamp() { private String getTimeStamp() {
DateFormat df = new SimpleDateFormat(TIME_STAMP_FORMAT); DateFormat df = new SimpleDateFormat(TIME_STAMP_FORMAT, Locale.getDefault());
return df.format(new Date()); return df.format(new Date());
} }
private class AppServiceClientConnectionListener implements IAppServiceClientConnectionListener { private class AppServiceConnectionListener implements IAppServiceConnectionListener {
@Override @Override
public void onSuccess() { public void onSuccess() {
Log.i(TAG, "AppServiceClientConnectionListener onSuccess"); LogMessage(LogLevel.Info, "AppService connection opened", SUCCESS_COLOR);
logMessage("AppService connection opened");
_sendPingButton.setEnabled(true); DeviceActivity.this.runOnUiThread(new Runnable() {
_connectButton.setEnabled(true); @Override
public void run() {
_sendPingButton.setEnabled(true);
_connectButton.setEnabled(true);
}
});
} }
@Override @Override
public void onError(AppServiceClientConnectionStatus status) { public void onError(AppServiceConnectionStatus status) {
Log.e(TAG, "AppServiceClientConnectionListener onError status [" + status.toString()+"]"); if (status == AppServiceConnectionStatus.APPSERVICE_UNAVAILABLE)
logMessage("AppService connection error"); {
_sendPingButton.setEnabled(false); LogMessage(LogLevel.Warning, "AppService connection was lost. Attempting to reconnect", FAILURE_COLOR);
_connectButton.setEnabled(true); // Since we tried to connect before sending a message, we lost the appservice connection and so need to reconnect
try {
_appServiceConnection.openRemoteAsync();
} catch (ConnectedDevicesException e) {
e.printStackTrace();
}
}
else if (status == AppServiceConnectionStatus.APP_NOT_INSTALLED)
{
LogMessage(LogLevel.Warning, "RomanApp is not installed on the target machine.", FAILURE_COLOR);
}
else
{
LogMessage(LogLevel.Error, "AppService connection error [" + status.toString() + "]", FAILURE_COLOR);
}
DeviceActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
_sendPingButton.setEnabled(false);
_connectButton.setEnabled(true);
}
});
} }
@Override @Override
public void onClosed(AppServiceClientConnectionClosedStatus status) { public void onClosed(AppServiceConnectionClosedStatus status) {
Log.i(TAG, "AppServiceClientConnectionListener onClosed status [" + status.toString()+"]"); LogMessage(LogLevel.Error, "AppService connection closed [" + status.toString() + "]", FAILURE_COLOR);
logMessage("AppService connection closed"); DeviceActivity.this.runOnUiThread(new Runnable() {
_sendPingButton.setEnabled(false); @Override
_connectButton.setEnabled(true); public void run() {
_sendPingButton.setEnabled(false);
_connectButton.setEnabled(true);
}
});
} }
} }
} }

Просмотреть файл

@ -160,18 +160,6 @@ public class DeviceRecyclerActivity extends AppCompatActivity {
} }
} }
@Override
public void onResume() {
super.onResume();
Platform.resume();
}
@Override
public void onPause() {
Platform.suspend();
super.onPause();
}
public void onDiscoverClicked(View view) { public void onDiscoverClicked(View view) {
initializeData(); initializeData();
} }

Просмотреть файл

@ -77,18 +77,6 @@ public class MainActivity extends FragmentActivity implements ActivityCompat.OnR
super.onPostCreate(savedInstanceState); super.onPostCreate(savedInstanceState);
} }
@Override
public void onResume() {
super.onResume();
Platform.resume();
}
@Override
public void onPause() {
Platform.suspend();
super.onPause();
}
public void onLoginClick(View view) { public void onLoginClick(View view) {
_signInButton.setEnabled(false); _signInButton.setEnabled(false);
_web.loadUrl(_oauthUrl); _web.loadUrl(_oauthUrl);