If you think the Android project matrix-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package org.matrix.matrixandroidsdk.activity;
/*fromwww.java2s.com*/import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.EditText;
import org.matrix.matrixandroidsdk.Matrix;
import org.matrix.matrixandroidsdk.R;
import org.matrix.matrixandroidsdk.services.EventStreamService;
/**
* Contains useful functions which are called in multiple activities.
*/publicclass CommonActivityUtils {
publicstaticboolean handleMenuItemSelected(Activity activity, int id) {
if (id == R.id.action_logout) {
logout(activity);
return true;
}
elseif (id == R.id.action_disconnect) {
disconnect(activity);
return true;
}
elseif (id == R.id.action_settings) {
activity.startActivity(new Intent(activity, SettingsActivity.class));
return true;
}
return false;
}
publicstaticvoid logout(Activity context) {
stopEventStream(context);
// clear credentials
Matrix.getInstance(context).clearDefaultSessionAndCredentials();
// go to login page
context.startActivity(new Intent(context, LoginActivity.class));
context.finish();
}
publicstaticvoid disconnect(Activity context) {
stopEventStream(context);
// Clear session
Matrix.getInstance(context).clearDefaultSession();
context.finish();
}
publicstaticvoid stopEventStream(Activity context) {
// kill active connections
Intent killStreamService = new Intent(context, EventStreamService.class);
killStreamService.putExtra(EventStreamService.EXTRA_STREAM_ACTION, EventStreamService.StreamAction.STOP.ordinal());
context.startService(killStreamService);
}
publicstaticvoid pauseEventStream(Activity activity) {
Intent streamService = new Intent(activity, EventStreamService.class);
streamService.putExtra(EventStreamService.EXTRA_STREAM_ACTION, EventStreamService.StreamAction.PAUSE.ordinal());
activity.startService(streamService);
}
publicstaticvoid resumeEventStream(Activity activity) {
Intent streamService = new Intent(activity, EventStreamService.class);
streamService.putExtra(EventStreamService.EXTRA_STREAM_ACTION, EventStreamService.StreamAction.RESUME.ordinal());
activity.startService(streamService);
}
publicinterface OnSubmitListener {
publicvoid onSubmit(String text);
publicvoid onCancelled();
}
publicstatic AlertDialog createEditTextAlert(Activity context, String title, String hint, final OnSubmitListener listener) {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
final EditText input = new EditText(context);
if (hint != null) {
input.setHint(hint);
}
alert.setTitle(title);
alert.setView(input);
alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
listener.onSubmit(value);
}
});
alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
listener.onCancelled();
}
}
);
return alert.create();
}
}