Skip to content

Commit

Permalink
Use chooser for sharing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
NightlyNexus committed Sep 23, 2015
1 parent f24404f commit 70dd957
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.jakewharton.u2020.ui.logs;

import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.widget.ListView;
import android.widget.Toast;
import com.jakewharton.u2020.data.LumberYard;
Expand Down Expand Up @@ -79,7 +79,7 @@ private void share() {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
Intents.maybeStartActivity(getContext(), sendIntent);
Intents.maybeStartChooser(getContext(), sendIntent);
}
});
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/jakewharton/u2020/util/Intents.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,23 @@ public final class Intents {
* will display a simple message if none are available to handle it.
*/
public static boolean maybeStartActivity(Context context, Intent intent) {
return maybeStartActivity(context, intent, false);
}

/**
* Attempt to launch Android's chooser for the supplied {@link Intent}. Queries on-device
* packages before launching and will display a simple message if none are available to handle
* it.
*/
public static boolean maybeStartChooser(Context context, Intent intent) {
return maybeStartActivity(context, intent, true);
}

private static boolean maybeStartActivity(Context context, Intent intent, boolean chooser) {
if (hasHandler(context, intent)) {
if (chooser) {
intent = Intent.createChooser(intent, null);
}
context.startActivity(intent);
return true;
} else {
Expand All @@ -27,7 +43,7 @@ public static boolean maybeStartActivity(Context context, Intent intent) {
/**
* Queries on-device packages for a handler for the supplied {@link Intent}.
*/
public static boolean hasHandler(Context context, Intent intent) {
private static boolean hasHandler(Context context, Intent intent) {
List<ResolveInfo> handlers = context.getPackageManager().queryIntentActivities(intent, 0);
return !handlers.isEmpty();
}
Expand Down

0 comments on commit 70dd957

Please sign in to comment.