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

Add support for Android 9 #227

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions skunkworks_crow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

// For android 9 support using evernote's android-job
implementation 'com.google.android.gms:play-services-gcm:16.1.0'

// butterknife
implementation "com.jakewharton:butterknife:${rootProject.butterknifeVersion}"
annotationProcessor "com.jakewharton:butterknife-compiler:${rootProject.butterknifeVersion}"
Expand Down
6 changes: 5 additions & 1 deletion skunkworks_crow/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</intent-filter>
</activity>

<activity android:name=".activities.WifiActivity"
<activity android:name=".activities.ReceiverActivity"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
Expand Down Expand Up @@ -85,6 +85,10 @@
android:value=".activities.MainActivity"/>
</activity>

<service
android:name="com.evernote.android.job.gcm.PlatformGcmService"
android:enabled="true"
tools:replace="android:enabled"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.odk.share.activities;

import android.Manifest;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
Expand All @@ -24,6 +25,7 @@
import org.odk.share.application.Share;
import org.odk.share.dao.TransferDao;
import org.odk.share.preferences.SettingsPreference;
import org.odk.share.preferences.SharedPreferencesHelper;

import javax.inject.Inject;

Expand Down Expand Up @@ -70,6 +72,9 @@ public class MainActivity extends FormListActivity implements LoaderManager.Load
@Inject
TransferDao transferDao;

@Inject
SharedPreferencesHelper sharedPreferencesHelper;

private FormsAdapter formAdapter;


Expand Down Expand Up @@ -102,14 +107,41 @@ private void setupAdapter() {

@OnClick(R.id.bViewWifi)
public void viewWifiNetworks() {
Intent intent = new Intent(this, WifiActivity.class);
Intent intent = new Intent(this, ReceiverActivity.class);
startActivity(intent);
}

@OnClick(R.id.bSendForms)
public void selectForms() {
Intent intent = new Intent(this, SendFormsActivity.class);
startActivity(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (sharedPreferencesHelper.getHotspotName().isEmpty() ||
(sharedPreferencesHelper.isHotspotPasswordProtected() && sharedPreferencesHelper.getHotspotPassword().isEmpty())) {
new AlertDialog.Builder(this)
.setTitle("Configuration missing...")
.setMessage("Please set hotspot name and password as declared in setting")
.setPositiveButton("Open Hotspot Settings", (dialog, which) -> {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
})
.setNegativeButton("Configure", (dialog, which) -> {
startActivity(new Intent(getApplicationContext(), SettingsPreference.class));
dialog.dismiss();
})
.setNeutralButton("Cancel", (dialog, which) -> dialog.dismiss())
.create()
.show();
} else {
Intent intent = new Intent(this, SendFormsActivity.class);
startActivity(intent);
}
} else {
Intent intent = new Intent(this, SendFormsActivity.class);
startActivity(intent);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.odk.share.services.ReceiverService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
Expand All @@ -58,12 +57,13 @@
import timber.log.Timber;

import static com.google.zxing.integration.android.IntentIntegrator.QR_CODE_TYPES;
import static org.odk.share.utilities.QRCodeUtils.IP;
import static org.odk.share.utilities.QRCodeUtils.PASSWORD;
import static org.odk.share.utilities.QRCodeUtils.PORT;
import static org.odk.share.utilities.QRCodeUtils.PROTECTED;
import static org.odk.share.utilities.QRCodeUtils.SSID;

public class WifiActivity extends InjectableActivity implements OnItemClickListener, WifiStateListener {
public class ReceiverActivity extends InjectableActivity implements OnItemClickListener, WifiStateListener {

private static final int DIALOG_DOWNLOAD_PROGRESS = 1;
private static final int DIALOG_CONNECTING = 2;
Expand Down Expand Up @@ -101,7 +101,7 @@ public class WifiActivity extends InjectableActivity implements OnItemClickListe
private int port;

private boolean isQRCodeScanned;
private String ssidScanned;
private String ip;
private boolean isProtected;
private String passwordScanned;

Expand All @@ -121,10 +121,7 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

isQRCodeScanned = false;
ssidScanned = null;
isProtected = false;
passwordScanned = null;
lastConnectedWifiInfo = null;
port = -1;

scanResultList = new ArrayList<>();
Expand All @@ -141,18 +138,19 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
lastConnectedWifiInfo = wifiConnector.getActiveConnection();
}

startScan();
}

private boolean isPossibleHotspot(String ssid) {
/*private boolean isPossibleHotspot(String ssid) {
return ssid.contains(getString(R.string.hotspot_name_suffix)) ||
ssid.contains(getString(R.string.hotspot_name_prefix_oreo));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code is useless, please remove them or making comments to clarify the reason for keeping them.

ssid.startsWith(getString(R.string.hotspot_name_prefix_oreo));
}*/

@Override
protected void onResume() {
super.onResume();
wifiStateBroadcastReceiver.register();
startScan();
compositeDisposable.add(addDownloadEventSubscription());
}

Expand Down Expand Up @@ -313,8 +311,12 @@ private void setEmptyViewVisibility(String text) {

private void startReceiveTask() {
showDialog(DIALOG_DOWNLOAD_PROGRESS);
String dstAddress = wifiConnector.getAccessPointIpAddress();
receiverService.startDownloading(dstAddress, port);

if (ip == null || ip.isEmpty()) {
ip = wifiConnector.getAccessPointIpAddress();
}

receiverService.startDownloading(ip, port);
}

@OnClick(R.id.bScan)
Expand Down Expand Up @@ -397,19 +399,25 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
// request was canceled...
Timber.i("QR code scanning cancelled");
} else {
Timber.d("RESULT " + result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above.

Timber.d("Result: %s", result);
try {
JSONObject obj = new JSONObject(result.getContents());
ssidScanned = (String) obj.get(SSID);
ip = (String) obj.get(IP);
wifiNetworkSSID = (String) obj.get(SSID);
port = (Integer) obj.get(PORT);
isProtected = (boolean) obj.get(PROTECTED);
if (isProtected) {
passwordScanned = (String) obj.get(PASSWORD);
}

Timber.d("Scanned results " + ssidScanned + " " + port + " " + isProtected + " " + passwordScanned);
Timber.d("Scanned results " + wifiNetworkSSID + " " + port + " " + isProtected + " " + passwordScanned);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest removing all the test log if they are only used for debugging, or just comment them.

isQRCodeScanned = true;
startScan();

if (wifiConnector.getAccessPointIpAddress() != null && wifiConnector.getAccessPointIpAddress().equals(ip)) {
onConnected();
} else {
startScan();
}
} catch (JSONException e) {
Timber.e(e);
}
Expand All @@ -419,7 +427,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

@Override
public void onStateUpdate(NetworkInfo.DetailedState detailedState) {

String connectedSsid = wifiConnector.getWifiSSID();

Timber.d(wifiNetworkSSID + " " + connectedSsid + " " + detailedState.toString());
Expand All @@ -430,7 +437,6 @@ public void onStateUpdate(NetworkInfo.DetailedState detailedState) {
wifiResultAdapter.notifyItemChanged(scanResultList.indexOf(wifiNetworkInfo));

if (!isConnected) {
isConnected = true;
onConnected();
}

Expand All @@ -440,6 +446,8 @@ public void onStateUpdate(NetworkInfo.DetailedState detailedState) {
}

private void onConnected() {
isConnected = true;

Toast.makeText(getApplicationContext(), "Connected to " + wifiNetworkSSID, Toast.LENGTH_LONG).show();

removeDialog(DIALOG_CONNECTING);
Expand All @@ -462,17 +470,13 @@ public void onScanResultsAvailable() {
ArrayList<WifiNetworkInfo> list = new ArrayList<>();

for (ScanResult scanResult : scanResults) {
if (isPossibleHotspot(scanResult.SSID)) {
WifiNetworkInfo wifiNetworkInfo = new WifiNetworkInfo();
wifiNetworkInfo.setSsid(scanResult.SSID);
wifiNetworkInfo.setRssi(WifiManager.calculateSignalLevel(scanResult.level, 100));
wifiNetworkInfo.setSecurityType(wifiConnector.getScanResultSecurity(scanResult));
list.add(wifiNetworkInfo);
}
WifiNetworkInfo wifiNetworkInfo = new WifiNetworkInfo();
wifiNetworkInfo.setSsid(scanResult.SSID);
wifiNetworkInfo.setRssi(WifiManager.calculateSignalLevel(scanResult.level, 100));
wifiNetworkInfo.setSecurityType(wifiConnector.getScanResultSecurity(scanResult));
list.add(wifiNetworkInfo);
}

Timber.d(Arrays.toString(list.toArray()));

wifiListAvailable(list);
}

Expand All @@ -491,12 +495,12 @@ private void wifiListAvailable(ArrayList<WifiNetworkInfo> list) {
if (isQRCodeScanned) {
isQRCodeScanned = false;
for (WifiNetworkInfo info : list) {
if (info.getSsid().equals(ssidScanned)) {
if (info.getSsid().equals(wifiNetworkSSID)) {
if (info.getState() != NetworkInfo.DetailedState.CONNECTED) {
Toast.makeText(this, "attempting connection", Toast.LENGTH_SHORT).show();
connectToNetwork(info.getSecurityType(), ssidScanned, passwordScanned);
connectToNetwork(info.getSecurityType(), wifiNetworkSSID, passwordScanned);
} else {
Toast.makeText(this, "already connected to " + ssidScanned, Toast.LENGTH_SHORT).show();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract the toasts into something like ToastUtils.java? You can refer to Collect's. Something for showing dialogs can be refactor as well.

Toast.makeText(this, "already connected to " + wifiNetworkSSID, Toast.LENGTH_SHORT).show();
}
return;
}
Expand Down
Loading