Skip to content

Commit

Permalink
Merge pull request #242 from NordicSemiconductor/dev
Browse files Browse the repository at this point in the history
nRF Mesh Reelase v2.0.5
  • Loading branch information
roshanrajaratnam authored Sep 4, 2019
2 parents d6fe800 + 1840c9d commit b6d32cb
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 245 deletions.
11 changes: 5 additions & 6 deletions Example/nrf-mesh/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion '29.0.0'
buildToolsVersion '29.0.2'

defaultConfig {
applicationId "no.nordicsemi.android.nrfmeshprovisioner"
minSdkVersion 18
targetSdkVersion 29
versionCode 51
versionName "2.0.3"
versionCode 53
versionName "2.0.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
Expand All @@ -40,7 +40,6 @@ android {
release {
minifyEnabled false
shrinkResources false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand Down Expand Up @@ -75,12 +74,12 @@ dependencies {
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
// Brings the new BluetoothLeScanner API to older platforms
implementation 'no.nordicsemi.android.support.v18:scanner:1.4.2'
implementation 'no.nordicsemi.android.support.v18:scanner:1.4.3'
implementation 'no.nordicsemi.android:ble:1.2.0'
// Log Bluetooth LE events in nRF Logger
implementation 'androidx.multidex:multidex:2.0.1'
// Lifecycle extensions
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha03'

implementation 'com.google.dagger:dagger:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;

import javax.inject.Inject;

Expand Down Expand Up @@ -228,6 +230,18 @@ public void onActivityResult(final int requestCode, final int resultCode, final
} else {
Log.e(TAG, "Error while opening file browser");
}
} else if (requestCode == 2011) {
if (resultCode == RESULT_OK) {
if (data != null) {
final Uri uri = data.getData();
try {
final OutputStream stream = requireContext().getContentResolver().openOutputStream(uri);
mViewModel.exportMeshNetwork(stream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}

Expand Down Expand Up @@ -277,7 +291,6 @@ private void performFileSearch() {
startActivityForResult(intent, READ_FILE_REQUEST_CODE);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void handleNetworkExport() {
if (!Utils.isWriteExternalStoragePermissionsGranted(getContext())
|| Utils.isWriteExternalStoragePermissionDeniedForever(getActivity())) {
Expand All @@ -287,11 +300,16 @@ private void handleNetworkExport() {
getString(R.string.external_storage_permission_required));
fragmentPermissionRationale.show(getChildFragmentManager(), null);
} else {
final File f = new File(EXPORT_PATH);
if (!f.exists()) {
f.mkdirs();
final String networkName = mViewModel.getNetworkLiveData().getNetworkName();
if (Utils.isKitkatOrAbove()) {
final Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_TITLE, networkName);
startActivityForResult(intent, 2011);
} else {
mViewModel.exportMeshNetwork();
}
mViewModel.getMeshManagerApi().exportMeshNetwork(EXPORT_PATH);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package no.nordicsemi.android.nrfmeshprovisioner.utils;

import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;

import androidx.annotation.NonNull;
import no.nordicsemi.android.meshprovisioner.MeshManagerApi;

public class NetworkExportUtils {

public interface NetworkExportCallbacks {

void onNetworkExported();

void onNetworkExportFailed(final String error);
}

/**
* Creates an AsyncTask to import the a m
*
* @param meshManagerApi Mesh manager api
* @param outputStream OutputStream obtained from the content resolver
* @param callbacks {@link NetworkExportCallbacks}
*/
public static void exportMeshNetwork(@NonNull final MeshManagerApi meshManagerApi,
@NonNull final OutputStream outputStream,
@NonNull NetworkExportCallbacks callbacks) {
final NetworkExportAsyncTask task = new NetworkExportAsyncTask(meshManagerApi, outputStream, callbacks);
task.execute();
}

/**
* Creates an AsyncTask to import the a m
*
* @param meshManagerApi Mesh manager api
* @param path OutputStream obtained from the content resolver
* @param callbacks {@link NetworkExportCallbacks}
*/
public static void exportMeshNetwork(@NonNull final MeshManagerApi meshManagerApi,
@NonNull final String path,
@NonNull final String fileName,
@NonNull NetworkExportCallbacks callbacks) {
final NetworkExportAsyncTask2 task = new NetworkExportAsyncTask2(meshManagerApi, path, fileName, callbacks);
task.execute();
}

private static class NetworkExportAsyncTask extends AsyncTask<Void, Void, Boolean> {

private static final String TAG = NetworkExportUtils.NetworkExportAsyncTask.class.getSimpleName();
private final MeshManagerApi meshManagerApi;
private final OutputStream outputStream;
private final NetworkExportCallbacks callbacks;
private String error;

/**
* Creates an AsyncTask to import the a m
*
* @param meshManagerApi Mesh manager api
*/
NetworkExportAsyncTask(@NonNull final MeshManagerApi meshManagerApi,
@NonNull final OutputStream outputStream,
@NonNull NetworkExportCallbacks callbacks) {
this.meshManagerApi = meshManagerApi;
this.outputStream = outputStream;
this.callbacks = callbacks;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Boolean doInBackground(final Void... voids) {
final String network = meshManagerApi.exportMeshNetwork();
if (network == null)
return false;
try {
outputStream.write(network.getBytes());
outputStream.close();
return true;
} catch (IOException e) {
error = e.getMessage();
Log.e(TAG, "Exception ex: " + error);
}
return false;
}

@Override
protected void onPostExecute(final Boolean aVoid) {
super.onPostExecute(aVoid);
if (aVoid) {
callbacks.onNetworkExported();
} else {
callbacks.onNetworkExportFailed(error);
}
}
}

private static class NetworkExportAsyncTask2 extends AsyncTask<Void, Void, Boolean> {

private static final String TAG = NetworkExportUtils.NetworkExportAsyncTask.class.getSimpleName();
private final MeshManagerApi meshManagerApi;
private final String path;
private final String fileName;
private final NetworkExportCallbacks callbacks;
private String error;

/**
* Creates an AsyncTask to import the a m
*
* @param meshManagerApi Mesh manager api
*/
NetworkExportAsyncTask2(@NonNull final MeshManagerApi meshManagerApi,
@NonNull final String path,
@NonNull final String fileName,
@NonNull NetworkExportCallbacks callbacks) {
this.meshManagerApi = meshManagerApi;
this.path = path;
this.fileName = fileName;
this.callbacks = callbacks;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Boolean doInBackground(final Void... voids) {
final String network = meshManagerApi.exportMeshNetwork();
if (network == null)
return false;
try {
final File directory = new File(path);
if (!directory.exists()) {
if (!directory.mkdirs()) {
error = "Unable to create file";
return false;
}
}
final File file = new File(path, fileName);
final BufferedWriter br = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
br.write(network);
br.flush();
br.close();
return true;
} catch (IOException e) {
error = e.getMessage();
Log.e(TAG, "Exception ex: " + error);
}
return false;
}

@Override
protected void onPostExecute(final Boolean aVoid) {
super.onPostExecute(aVoid);
if (aVoid) {
callbacks.onNetworkExported();
} else {
callbacks.onNetworkExportFailed(error);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.ParcelUuid;
Expand Down Expand Up @@ -123,7 +124,6 @@ public class NrfMeshRepository implements MeshProvisioningStatusCallbacks, MeshS
//Contains the MeshNetwork
private MeshNetworkLiveData mMeshNetworkLiveData = new MeshNetworkLiveData();
private SingleLiveEvent<String> mNetworkImportState = new SingleLiveEvent<>();
private SingleLiveEvent<String> mNetworkExportState = new SingleLiveEvent<>();
private SingleLiveEvent<MeshMessage> mMeshMessageLiveData = new SingleLiveEvent<>();

// Contains the provisioned nodes
Expand All @@ -147,6 +147,7 @@ public class NrfMeshRepository implements MeshProvisioningStatusCallbacks, MeshS
private boolean mIsDefaultTtlReceived;
private boolean mIsAppKeyAddCompleted;
private boolean mIsNetworkRetransmitSetCompleted;
private Uri uri;

private final Runnable mReconnectRunnable = this::startScan;

Expand Down Expand Up @@ -241,10 +242,6 @@ LiveData<String> getNetworkLoadState() {
return mNetworkImportState;
}

LiveData<String> getNetworkExportState() {
return mNetworkExportState;
}

ProvisioningStatusLiveData getProvisioningState() {
return mProvisioningStateLiveData;
}
Expand Down Expand Up @@ -628,23 +625,6 @@ public void onNetworkImported(final MeshNetwork meshNetwork) {
"for this address. However if the network does not contain any nodes you do not need to change the address");
}

@Override
public void onNetworkExported(final MeshNetwork meshNetwork) {
mNetworkExportState.postValue(meshNetwork.getMeshName() + " has been successfully exported. " +
"You can find the exported network information in the following path. " + EXPORTED_PATH);
}

@Override
public void onNetworkExportedJson(MeshNetwork meshNetwork, String networkJson) {

}

@Override
public void onNetworkExportFailed(final String error) {
mNetworkExportState.postValue(error);

}

@Override
public void onNetworkImportFailed(final String error) {
mNetworkImportState.postValue(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package no.nordicsemi.android.nrfmeshprovisioner.viewmodels;

import java.io.OutputStream;

import javax.inject.Inject;

import androidx.annotation.NonNull;
Expand All @@ -30,13 +32,15 @@
import no.nordicsemi.android.nrfmeshprovisioner.NetworkFragment;
import no.nordicsemi.android.nrfmeshprovisioner.ProxyFilterFragment;
import no.nordicsemi.android.nrfmeshprovisioner.SettingsFragment;
import no.nordicsemi.android.nrfmeshprovisioner.utils.NetworkExportUtils;

/**
* ViewModel for {@link NetworkFragment}, {@link GroupsFragment}, {@link ProxyFilterFragment}, {@link SettingsFragment}
*/
public class SharedViewModel extends BaseViewModel {
public class SharedViewModel extends BaseViewModel implements NetworkExportUtils.NetworkExportCallbacks {

private final ScannerRepository mScannerRepository;
private SingleLiveEvent<String> networkExportState = new SingleLiveEvent<>();

@Inject
SharedViewModel(@NonNull final NrfMeshRepository nrfMeshRepository, @NonNull final ScannerRepository scannerRepository) {
Expand All @@ -59,11 +63,8 @@ public LiveData<String> getNetworkLoadState() {
return mNrfMeshRepository.getNetworkLoadState();
}

/**
* Returns network export state
*/
public LiveData<String> getNetworkExportState() {
return mNrfMeshRepository.getNetworkExportState();
return networkExportState;
}

/**
Expand All @@ -74,4 +75,23 @@ public LiveData<String> getNetworkExportState() {
public void setSelectedGroup(final int address) {
mNrfMeshRepository.setSelectedGroup(address);
}

public void exportMeshNetwork(@NonNull final OutputStream stream) {
NetworkExportUtils.exportMeshNetwork(getMeshManagerApi(), stream, this);
}

public void exportMeshNetwork() {
final String fileName = getNetworkLiveData().getNetworkName() + ".json";
NetworkExportUtils.exportMeshNetwork(getMeshManagerApi(), NrfMeshRepository.EXPORT_PATH, fileName, this);
}

@Override
public void onNetworkExported() {
networkExportState.postValue(getNetworkLiveData().getMeshNetwork().getMeshName() + " has been successfully exported.");
}

@Override
public void onNetworkExportFailed(@NonNull final String error) {
networkExportState.postValue(error);
}
}
Loading

0 comments on commit b6d32cb

Please sign in to comment.