Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Default transferring mechanism and UI improvements #290

Merged
merged 23 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9a9d5c6
✨ feat: added the list preference
huangyz0918 Aug 2, 2019
f6d9f27
✨ feat: remove the asking dialog.
huangyz0918 Aug 3, 2019
a7cc2ac
🚑 quickfix: fixed the unit tests.
huangyz0918 Aug 3, 2019
dda6460
💄 ui: added a animation while sending by bluetooth.
huangyz0918 Aug 3, 2019
7c37c29
🚑 quickfix: remove the animate lib, replaced with a progress bar.
huangyz0918 Aug 3, 2019
84f7ac1
✨ feat: added sections in settings.
huangyz0918 Aug 4, 2019
d9c3ffc
✨ feat: add the switch option for senders.
huangyz0918 Aug 6, 2019
3d69968
✨ feat: add switch option in the receiver's menu.
huangyz0918 Aug 6, 2019
a42ef74
💄 ui: change color theme for bluetooth specific activities.
huangyz0918 Aug 9, 2019
c5805f1
💄 ui: added icons in the menu of sending/receiving pages.
huangyz0918 Aug 10, 2019
54243a7
🚑 quickfix: fixed unit tests.
huangyz0918 Aug 10, 2019
0877e6d
🐛 fix: resolved conflicts.
huangyz0918 Aug 11, 2019
a0a3445
💄 ui: added icons in the dialog.
huangyz0918 Aug 12, 2019
c52969d
🐛 fix: resolved conflicts.
huangyz0918 Aug 16, 2019
df08543
💄 ui: add icon to bluetooth sender.
huangyz0918 Aug 16, 2019
038e47f
🐛 fix: resolved conflicts and improved the dialog.
huangyz0918 Aug 16, 2019
7f95a1a
🚑 quickfix: add default text for bluetooth name.
huangyz0918 Aug 18, 2019
0bbd4ea
🚑 quickfix: add @NonNull checks for dialog utils.
huangyz0918 Aug 18, 2019
6a0fb00
🐛 fix: fix some issues
huangyz0918 Aug 18, 2019
d827d3a
🚑 quickfix: add comment for dialog utils.
huangyz0918 Aug 18, 2019
9638567
🚑 quickfix: fixed the settings text.
huangyz0918 Aug 18, 2019
c4fb32b
🚑 quickfix:
huangyz0918 Aug 18, 2019
d16605f
✅ test: fix unit tests.
huangyz0918 Aug 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions skunkworks_crow/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".views.ui.bluetooth.BtReceiverActivity"/>
<activity android:name=".views.ui.bluetooth.BtSenderActivity"/>
<activity android:name=".views.ui.bluetooth.BtReceiverActivity" />
<activity android:name=".views.ui.bluetooth.BtSenderActivity" />
<activity android:name=".views.ui.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ private void setupDataStreamsAndReceive(@Share.TransferMethod int method) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(targetMacAddress);
if (bluetoothDevice != null) {
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext());
boolean isSecureMode = prefs.getBoolean(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE, true);
if (isSecureMode) {
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID);
} else {
bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
}

if (!bluetoothSocket.isConnected()) {
bluetoothSocket.connect();
Expand Down
116 changes: 77 additions & 39 deletions skunkworks_crow/src/main/java/org/odk/share/utilities/DialogUtils.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.odk.share.utilities;

import android.app.AlertDialog;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import org.odk.share.R;
import org.odk.share.views.ui.bluetooth.BtReceiverActivity;
import org.odk.share.views.ui.bluetooth.BtSenderActivity;
import org.odk.share.views.ui.hotspot.HpReceiverActivity;
import org.odk.share.views.ui.hotspot.HpSenderActivity;
import org.odk.share.views.ui.settings.PreferenceKeys;

/**
* @author huangyz0918 ([email protected])
Expand All @@ -21,48 +27,80 @@ private DialogUtils() {
}

/**
* Show a {@link AlertDialog} for choosing a sender methods.
* Create a simple {@link AlertDialog} for showing messages, after that, we finish the {@link Activity}.
*/
public static AlertDialog createSimpleDialog(@NonNull Context context,
@NonNull String title,
@NonNull String message) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setCancelable(false);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.ok), (dialog, i) -> ((Activity) context).finish());
return alertDialog;
}

/**
* Create an {@link AlertDialog} for switching receive method.
*/
public static AlertDialog createMethodSwitchDialog(@NonNull Activity targetActivity, DialogInterface.OnClickListener listener) {
AlertDialog alertDialog = new AlertDialog.Builder(targetActivity).create();
alertDialog.setCancelable(false);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, targetActivity.getString(R.string.cancel),
(dialog, i) -> {
dialog.dismiss();
});

alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, targetActivity.getString(R.string.switch_method), listener);

//the icons set in the dialog are for destinations.
if (targetActivity instanceof BtReceiverActivity || targetActivity instanceof BtSenderActivity) {
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
alertDialog.setIcon(R.drawable.ic_wifi_tethering_black_24dp);
alertDialog.setTitle(targetActivity.getString(R.string.switch_to_hotspot_title));
alertDialog.setMessage(targetActivity.getString(R.string.hotspot_switch_method));
} else if (targetActivity instanceof HpReceiverActivity || targetActivity instanceof HpSenderActivity) {
alertDialog.setIcon(R.drawable.ic_bluetooth_black_24dp);
alertDialog.setTitle(targetActivity.getString(R.string.switch_to_bluetooth_title));
alertDialog.setMessage(targetActivity.getString(R.string.bluetooth_switch_method));
} else {
throw new IllegalArgumentException("Not a valid activity parameter");
}

return alertDialog;
}

/**
* Detecting the default set by user, and using that as main sending method.
*/
public static AlertDialog showSenderMethodsDialog(Context context, Intent intent, String title) {
String[] options = {context.getString(R.string.method_bluetooth), context.getString(R.string.method_wifi_hotspot)};
return new AlertDialog.Builder(context)
.setTitle(title)
.setIcon(R.drawable.ic_help_outline)
.setItems(options, (DialogInterface dialog, int which) -> {
if (ActivityUtils.getActivity(context) != null) {
switch (which) {
case 0:
intent.setClass(ActivityUtils.getActivity(context), BtSenderActivity.class);
break;
case 1:
intent.setClass(ActivityUtils.getActivity(context), HpSenderActivity.class);
break;
}
context.startActivity(intent);
}
}).create();
public static void switchToDefaultSendingMethod(@NonNull Context context, @NonNull Intent intent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String defaultMethod = prefs.getString(PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD, context.getString(R.string.default_hotspot_ssid));
if (context.getString(R.string.default_hotspot_ssid).equals(defaultMethod)) {
intent.setClass(ActivityUtils.getActivity(context), HpSenderActivity.class);
} else if (context.getString(R.string.bluetooth).equals(defaultMethod)) {
intent.setClass(ActivityUtils.getActivity(context), BtSenderActivity.class);
} else {
throw new IllegalArgumentException("No such default sending method!");
}

context.startActivity(intent);
}

/**
* Show a {@link AlertDialog} for choosing a sender methods.
* Detecting the default set by user, and using that as main receiving method.
*/
public static AlertDialog showReceiverMethodsDialog(Context context, Intent intent, String title) {
String[] options = {context.getString(R.string.method_bluetooth), context.getString(R.string.method_wifi_hotspot)};
return new AlertDialog.Builder(context)
.setTitle(title)
.setIcon(R.drawable.ic_help_outline)
.setItems(options, (DialogInterface dialog, int which) -> {
if (ActivityUtils.getActivity(context) != null) {
switch (which) {
case 0:
intent.setClass(ActivityUtils.getActivity(context), BtReceiverActivity.class);
break;
case 1:
intent.setClass(ActivityUtils.getActivity(context), HpReceiverActivity.class);
break;
}
context.startActivity(intent);
}
}).create();
public static void switchToDefaultReceivingMethod(@NonNull Context context) {
Intent intent = new Intent();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String defaultMethod = prefs.getString(PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD, context.getString(R.string.default_hotspot_ssid));
if (context.getString(R.string.default_hotspot_ssid).equals(defaultMethod)) {
intent.setClass(ActivityUtils.getActivity(context), HpReceiverActivity.class);
} else if (context.getString(R.string.bluetooth).equals(defaultMethod)) {
intent.setClass(ActivityUtils.getActivity(context), BtReceiverActivity.class);
} else {
throw new IllegalArgumentException("No such default receiving method!");
}

context.startActivity(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Expand All @@ -28,8 +30,11 @@
import org.odk.share.rx.RxEventBus;
import org.odk.share.rx.schedulers.BaseSchedulerProvider;
import org.odk.share.services.ReceiverService;
import org.odk.share.utilities.ActivityUtils;
import org.odk.share.utilities.DialogUtils;
import org.odk.share.utilities.PermissionUtils;
import org.odk.share.views.ui.common.injectable.InjectableActivity;
import org.odk.share.views.ui.hotspot.HpReceiverActivity;

import javax.inject.Inject;

Expand Down Expand Up @@ -91,8 +96,12 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt_receive);
ButterKnife.bind(this);
setTitle(getString(R.string.connect_bluetooth_title));

setTitle(" " + getString(R.string.connect_bluetooth_title));
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setIcon(R.drawable.ic_bluetooth_white_24dp);
}

initEvents();
setupDialogs();
Expand Down Expand Up @@ -317,6 +326,26 @@ public void onItemClick(BluetoothDevice device) {
}
}

/**
* Create the switch method button in the menu.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.switch_method_menu, menu);
final MenuItem switchItem = menu.findItem(R.id.menu_switch);
switchItem.setOnMenuItemClickListener((MenuItem item) -> {
DialogUtils.createMethodSwitchDialog(this, (DialogInterface dialog, int which) -> {
receiverService.cancel();
if (BluetoothUtils.isBluetoothEnabled()) {
BluetoothUtils.disableBluetooth();
}
ActivityUtils.launchActivity(this, HpReceiverActivity.class, true);
}).show();
return true;
});
return super.onCreateOptionsMenu(menu);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -29,8 +32,10 @@
import org.odk.share.rx.RxEventBus;
import org.odk.share.rx.schedulers.BaseSchedulerProvider;
import org.odk.share.services.SenderService;
import org.odk.share.utilities.DialogUtils;
import org.odk.share.utilities.PermissionUtils;
import org.odk.share.views.ui.common.injectable.InjectableActivity;
import org.odk.share.views.ui.hotspot.HpSenderActivity;

import javax.inject.Inject;

Expand Down Expand Up @@ -65,6 +70,9 @@ public class BtSenderActivity extends InjectableActivity {
@BindView(R.id.toolbar)
Toolbar toolbar;

@BindView(R.id.progressBar)
ProgressBar progressBar;

@Inject
RxEventBus rxEventBus;

Expand All @@ -83,9 +91,9 @@ public class BtSenderActivity extends InjectableActivity {
private AlertDialog resultDialog;
private static final int CONNECT_TIMEOUT = 120;
private static final int COUNT_DOWN_INTERVAL = 1000;
private Intent receivedIntent;
private static final int DISCOVERABLE_CODE = 0x121;
private static final int SUCCESS_CODE = 120;
private BtSenderActivity thisActivity = this;

private long[] formIds;
private int mode;
Expand All @@ -97,20 +105,28 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_bt_send);
ButterKnife.bind(this);

setTitle(getString(R.string.send_instance_title));
setTitle(" " + getString(R.string.send_instance_title));
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setIcon(R.drawable.ic_bluetooth_white_24dp);
}

if (!BluetoothUtils.isBluetoothEnabled()) {
BluetoothUtils.enableBluetooth();
}

if (getIntent() != null) {
receivedIntent = getIntent();
} else {
throw new IllegalArgumentException("No received intent");
}

setupDialog();
registerReceiver(bluetoothReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

formIds = getIntent().getLongArrayExtra(INSTANCE_IDS);
mode = getIntent().getIntExtra(MODE, ASK_REVIEW_MODE);
if (formIds == null) {
formIds = getIntent().getLongArrayExtra(FORM_IDS);
formIds = receivedIntent.getLongArrayExtra(FORM_IDS);
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
}

if (BluetoothUtils.isBluetoothEnabled()) {
Expand Down Expand Up @@ -145,14 +161,27 @@ private void setupDialog() {
.create();
}

private void dismissAllDialogs() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
/**
* Create the switch method button in the menu.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.switch_method_menu, menu);
final MenuItem switchItem = menu.findItem(R.id.menu_switch);
switchItem.setOnMenuItemClickListener((MenuItem item) -> {
DialogUtils.createMethodSwitchDialog(this, (DialogInterface dialog, int which) -> {
receivedIntent.setClass(this, HpSenderActivity.class);
senderService.cancel();
if (BluetoothUtils.isBluetoothEnabled()) {
BluetoothUtils.disableBluetooth();
}
startActivity(receivedIntent);
finish();
}).show();
return true;
});

if (resultDialog != null && resultDialog.isShowing()) {
resultDialog.dismiss();
}
return super.onCreateOptionsMenu(menu);
}

/**
Expand Down Expand Up @@ -197,16 +226,23 @@ private Disposable addUploadEventSubscription() {
case FINISHED:
progressDialog.dismiss();
isFinished = true;
progressBar.setVisibility(View.GONE);
String result = uploadEvent.getResult();
resultDialog.setMessage(result);
resultDialog.show();
break;
case ERROR:
dismissAllDialogs();
progressBar.setVisibility(View.GONE);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(this, getString(R.string.error_while_uploading, uploadEvent.getResult()), Toast.LENGTH_SHORT).show();
break;
case CANCELLED:
dismissAllDialogs();
progressBar.setVisibility(View.GONE);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(this, getString(R.string.canceled), Toast.LENGTH_LONG).show();
break;
}
Expand All @@ -233,7 +269,7 @@ public void onReceive(Context context, Intent intent) {
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
== BluetoothAdapter.STATE_ON && !isDiscovering) {
BtSenderActivityPermissionsDispatcher.enableDiscoveryWithPermissionCheck(thisActivity);
BtSenderActivityPermissionsDispatcher.enableDiscoveryWithPermissionCheck(BtSenderActivity.this);
}
}
}
Expand Down Expand Up @@ -314,19 +350,13 @@ public void onTick(long millisUntilFinished) {
@Override
public void onFinish() {
isDiscovering = false;
if (!(thisActivity).isFinishing()) {
if (!(BtSenderActivity.this).isFinishing()) {
alertDialog.show();
}
}
}.start();
}

@Override
protected void onStop() {
super.onStop();
dismissAllDialogs();
}

@Override
protected void onResume() {
super.onResume();
Expand Down
Loading