Skip to content

Commit

Permalink
soarcn#72 introduce a helper to create shareIntent picker
Browse files Browse the repository at this point in the history
  • Loading branch information
soarcn committed Jul 18, 2015
1 parent 0dc7a49 commit 19163a1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.cocosw.bottomsheet.example;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -23,10 +18,9 @@
import android.widget.ArrayAdapter;

import com.cocosw.bottomsheet.BottomSheet;
import com.cocosw.bottomsheet.BottomSheetHelper;
import com.cocosw.query.CocoQuery;

import java.util.List;

/**
* Project: gradle
* Created by LiaoKai(soarcn) on 2014/9/22.
Expand Down Expand Up @@ -139,10 +133,10 @@ public void onClick(DialogInterface dialog, int which) {
}).limit(R.integer.bs_initial_list_row).build();
break;
case 6:
sheet = getShareActions(new BottomSheet.Builder(this).grid().title("Share To " + adapter.getItem(position)), "Hello " + adapter.getItem(position)).build();
sheet = getShareActions("Hello " + adapter.getItem(position)).title("Share To " + adapter.getItem(position)).limit(R.integer.no_limit).build();
break;
case 7:
sheet = getShareActions(new BottomSheet.Builder(this).grid().title("Share To " + adapter.getItem(position)), "Hello " + adapter.getItem(position)).limit(R.integer.bs_initial_grid_row).build();
sheet = getShareActions("Hello " + adapter.getItem(position)).title("Share To " + adapter.getItem(position)).build();
break;
case 8:
sheet = new BottomSheet.Builder(this).icon(getRoundedBitmap(R.drawable.icon)).title("To " + adapter.getItem(position)).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
Expand Down Expand Up @@ -178,32 +172,12 @@ public boolean onMenuItemClick(MenuItem item) {
return sheet;
}

private BottomSheet.Builder getShareActions(BottomSheet.Builder builder, String text) {
PackageManager pm = this.getPackageManager();

private BottomSheet.Builder getShareActions(String text) {
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
final List<ResolveInfo> list = pm.queryIntentActivities(shareIntent, 0);

for (int i = 0; i < list.size(); i++) {
builder.sheet(i, list.get(i).loadIcon(pm), list.get(i).loadLabel(pm));
}

builder.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityInfo activity = list.get(which).activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent newIntent = (Intent) shareIntent.clone();
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
newIntent.setComponent(name);
startActivity(newIntent);
}
});
return builder;
return BottomSheetHelper.shareAction(this, shareIntent);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.cocosw.bottomsheet;

import android.app.Activity;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.annotation.NonNull;

import java.util.List;

/**
* A helper class,
* <p/>
* Project: BottomSheet
* Created by LiaoKai(soarcn) on 2015/7/18.
*/
public class BottomSheetHelper {


/**
* Create a BottomSheet Builder for creating share intent chooser.
* You still need to call show() to display it like:
* <p/>
* Intent sharingIntent = new Intent(Intent.ACTION_SEND);
* shareIntent.setType("text/plain");
* shareIntent.putExtra(Intent.EXTRA_TEXT, "hello");
* BottomSheetHelper.shareAction(activity,sharingIntent).show();
*
* @param activity Activity instance
* @param intent shareIntent
* @return BottomSheet builder
*/
public static BottomSheet.Builder shareAction(@NonNull final Activity activity, @NonNull final Intent intent) {
BottomSheet.Builder builder = new BottomSheet.Builder(activity).grid();
PackageManager pm = activity.getPackageManager();

final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);

for (int i = 0; i < list.size(); i++) {
builder.sheet(i, list.get(i).loadIcon(pm), list.get(i).loadLabel(pm));
}

builder.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
ActivityInfo activityInfo = list.get(which).activityInfo;
ComponentName name = new ComponentName(activityInfo.applicationInfo.packageName,
activityInfo.name);
Intent newIntent = (Intent) intent.clone();
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
newIntent.setComponent(name);
activity.startActivity(newIntent);
}
});
builder.limit(R.integer.bs_initial_grid_row);
return builder;
}

}
2 changes: 1 addition & 1 deletion library/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="bs_grid_left_padding">8dp</dimen>
<dimen name="bs_grid_left_padding">8dp</dimen>
<dimen name="bs_grid_top_padding">0dp</dimen>
<dimen name="bs_grid_right_padding">8dp</dimen>
<dimen name="bs_grid_bottom_padding">8dp</dimen>
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/integer.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="no_limit">0</integer>
<integer name="bs_grid_colum">3</integer>
<integer name="bs_initial_grid_row">3</integer>
<integer name="bs_initial_list_row">5</integer>
Expand Down

0 comments on commit 19163a1

Please sign in to comment.