Skip to content

Commit

Permalink
Merge pull request #14 from TaQuangKhoi/9-share-thoughts-from-other-apps
Browse files Browse the repository at this point in the history
Receive Text from Other Apps
  • Loading branch information
TaQuangKhoi committed Sep 4, 2022
2 parents 2111817 + a92e124 commit 6897541
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
19 changes: 18 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
android:exported="false"
android:label="@string/title_activity_settings" />


<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -36,6 +35,24 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
</application>

Expand Down
63 changes: 60 additions & 3 deletions app/src/main/java/com/example/napkin/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
Expand All @@ -13,6 +14,7 @@
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
Expand All @@ -26,8 +28,10 @@ public class MainActivity extends AppCompatActivity {
Button btnSend;
EditText etThought, etSourceUrl;
String email, token;
private final OkHttpClient client = new OkHttpClient();
ImageView ivSetting;
Intent intentReceiver;

private final OkHttpClient client = new OkHttpClient();
SharedPreferences savedSettings;

@Override
Expand All @@ -43,9 +47,62 @@ protected void onCreate(Bundle savedInstanceState) {
savedSettings = getSharedPreferences("Settings", MODE_PRIVATE);
etSourceUrl.setText("");

initBtnSetting();
LoadSetting();
initBtnSend();
initBtnSetting();
initReceiver();
}

private void initReceiver() {
Log.d("Napkin", "initReceiver: Init receiver");
intentReceiver = getIntent();
String action = intentReceiver.getAction();
String type = intentReceiver.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intentReceiver); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intentReceiver); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intentReceiver); // Handle multiple images being sent
}
} else {
Toast.makeText(this, "There's nothing", Toast.LENGTH_SHORT).show();
}
}

void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
String sharedUrl = intent.getStringExtra(Intent.EXTRA_REFERRER);
if (sharedText != null) {
// Update UI to reflect text being shared
Toast.makeText(this, "Text Sending : " + sharedText, Toast.LENGTH_SHORT).show();
etThought.setText(sharedText);
if (sharedUrl != null) {
etSourceUrl.setText(sharedUrl);
}
SendThoughtWithToken(
email,
token,
sharedText.toString(),
""
);
}
}

void handleSendImage(Intent intent) {
//Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
Toast.makeText(this, "Don't allow image", Toast.LENGTH_SHORT).show();
}

void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
Toast.makeText(this, "Don't allow images", Toast.LENGTH_SHORT).show();
}
}

private void LoadSetting() {
Expand Down Expand Up @@ -107,7 +164,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
Toast.makeText(this, "Sent ✅", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error :v", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Error - Details in Log:v", Toast.LENGTH_SHORT).show();
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/settings_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@
android:fontFamily="@font/inter_bold"

android:textSize="30sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"

android:text="Version 1.2.1-beta-15"/>
</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">Napkin</string>
<string name="app_name">Napkin Collect</string>
<string name="title_activity_login">LoginActivity</string>
<string name="prompt_email">Email</string>
<string name="prompt_password">Password</string>
Expand Down

0 comments on commit 6897541

Please sign in to comment.