Skip to content

Commit

Permalink
Add storage permissions & make presenter calls on UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Apr 30, 2019
1 parent 93e3e1c commit e27980b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ POM_SETTING_DEVELOPER_ID=opensrp
POM_SETTING_DEVELOPER_NAME=OpenSRP Onadev
POM_SETTING_NAME=Android Peer-to-Peer Sync
POM_SETTING_ARTIFACT_ID=android-p2p-sync
POM_SETTING_PACKAGING=aar
POM_SETTING_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.smartregister.p2p.sync;

import android.os.Handler;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -55,12 +57,15 @@ public class SyncSenderHandler {
private PayloadRetry payloadRetry;
private SyncPackageManifest syncPackageManifest;

private Handler uiHandler;

public SyncSenderHandler(@NonNull P2pModeSelectContract.SenderPresenter presenter, @NonNull TreeSet<DataType> dataSyncOrder
, @Nullable List<P2pReceivedHistory> receivedHistory) {
this.presenter = presenter;
this.dataSyncOrder = dataSyncOrder;
this.receivedHistory = receivedHistory;
this.batchSize = P2PLibrary.getInstance().getBatchSize();
this.uiHandler = new Handler(Looper.getMainLooper());
}

private void generateRecordsToSend() {
Expand Down Expand Up @@ -261,12 +266,23 @@ public void run() {
outputStream.flush();
outputStream.close();

} catch (IOException e) {
} catch (final IOException e) {
Timber.e(e, "Error occurred trying to read bytes into payload pipe");
presenter.errorOccurredSync(e);

uiHandler.post(new Runnable() {
@Override
public void run() {
presenter.errorOccurredSync(e);
}
});
}
} else {
presenter.errorOccurredSync(new Exception("Could not find the payload pipe!"));
uiHandler.post(new Runnable() {
@Override
public void run() {
presenter.errorOccurredSync(new Exception("Could not find the payload pipe!"));
}
});
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.smartregister.p2p.sample">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
package org.smartregister.p2p.sample;

import android.Manifest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import org.smartregister.p2p.activity.P2pModeSelectActivity;

public class MainActivity extends AppCompatActivity {

private String[] permissions = new String[] {Manifest.permission.READ_EXTERNAL_STORAGE
, Manifest.permission.WRITE_EXTERNAL_STORAGE};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onResume() {
super.onResume();

requestPermissions();
}

private void requestPermissions() {
if (!arePermissionsGranted()) {
ActivityCompat.requestPermissions(this, permissions, 8348);
} else {
startActivity(new Intent(this, P2pModeSelectActivity.class));
}
}

private boolean arePermissionsGranted() {
for (String permission: permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}

startActivity(new Intent(this, P2pModeSelectActivity.class));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SampleSenderDao() {
nameRecords.add("Chris Wamaitha");

for (int i = 0; i < 200; i++) {
personalDetailsRecords.add(generateRandomString(50000));
personalDetailsRecords.add(generateRandomString(10000));
}
}

Expand Down

0 comments on commit e27980b

Please sign in to comment.