Skip to content

Commit

Permalink
feat: add sharing image to the Android demo app
Browse files Browse the repository at this point in the history
Signed-off-by: D4ryl00 <d4ryl00@gmail.com>
  • Loading branch information
D4ryl00 committed Dec 15, 2021
1 parent 692ec85 commit 1214be0
Show file tree
Hide file tree
Showing 15 changed files with 512 additions and 71 deletions.
12 changes: 0 additions & 12 deletions android/.idea/runConfigurations.xml

This file was deleted.

2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.journeyapps:zxing-android-embedded:4.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
Expand Down
12 changes: 11 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ipfs.gomobile.example">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
<activity
android:name=".MainActivity"
android:label="Gomobile IPFS Example"
Expand All @@ -24,6 +30,10 @@
android:name=".DisplayImageActivity"
android:parentActivityName=".MainActivity" >
</activity>
<activity
android:name=".ShowQRCode"
android:parentActivityName=".MainActivity" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public class DisplayImageActivity extends AppCompatActivity {
private static final String TAG = "DisplayImageActivity";

public static byte[] fetchedData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -27,8 +29,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

try {
byte[] data = intent.getExtras().getByteArray("ImageData");
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(fetchedData, 0, fetchedData.length);

ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
Expand Down
76 changes: 76 additions & 0 deletions android/app/src/main/java/ipfs/gomobile/example/FetchFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ipfs.gomobile.example;

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

import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.Random;

import ipfs.gomobile.android.IPFS;

final class FetchFile extends AsyncTask<Void, Void, String> {
private static final String TAG = "FetchIPFSFile";

private final WeakReference<MainActivity> activityRef;
private boolean backgroundError;
private byte[] fetchedData;
private String cid;

FetchFile(MainActivity activity, String cid) {
activityRef = new WeakReference<>(activity);
this.cid = cid;
}

@Override
protected void onPreExecute() {
MainActivity activity = activityRef.get();
if (activity == null || activity.isFinishing()) return;

activity.displayStatusProgress(activity.getString(R.string.titleImageFetching));
}

@Override
protected String doInBackground(Void... v) {
MainActivity activity = activityRef.get();
if (activity == null || activity.isFinishing()) {
cancel(true);
return null;
}

IPFS ipfs = activity.getIpfs();

try {
fetchedData = ipfs.newRequest("cat")
.withArgument(cid)
.send();

// Log.d(TAG, "fetched file data=" + MainActivity.bytesToHex(fetchedData));
return activity.getString(R.string.titleFetchedImage);
} catch (Exception err) {
backgroundError = true;
return MainActivity.exceptionToString(err);
}
}

protected void onPostExecute(String result) {
MainActivity activity = activityRef.get();
if (activity == null || activity.isFinishing()) return;

if (backgroundError) {
activity.displayStatusError(activity.getString(R.string.titleImageFetchingErr), result);
Log.e(TAG, "Ipfs image fetch error: " + result);
} else {
activity.displayStatusSuccess();

// Put directly data through this way because of size limit with Intend
DisplayImageActivity.fetchedData = fetchedData;

Intent intent = new Intent(activity, DisplayImageActivity.class);
intent.putExtra("Title", result);
activity.startActivity(intent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected void onPreExecute() {
MainActivity activity = activityRef.get();
if (activity == null || activity.isFinishing()) return;

activity.displayFetchProgress();
activity.displayStatusProgress(activity.getString(R.string.titleXKCDFetching));
}

@Override
Expand Down Expand Up @@ -86,13 +86,15 @@ protected void onPostExecute(String result) {
if (activity == null || activity.isFinishing()) return;

if (backgroundError) {
activity.displayFetchError(result);
activity.displayStatusError(activity.getString(R.string.titleXKCDFetchingErr), result);
Log.e(TAG, "XKCD fetch error: " + result);
} else {
activity.displayFetchSuccess();
activity.displayStatusSuccess();

// Put directly data through this way because of size limit with Intend
DisplayImageActivity.fetchedData = fetchedData;

Intent intent = new Intent(activity, DisplayImageActivity.class);
intent.putExtra("ImageData", fetchedData);
intent.putExtra("Title", result);
activity.startActivity(intent);
}
Expand Down
Loading

0 comments on commit 1214be0

Please sign in to comment.