-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create share button with custom chooser. #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,92 @@ | ||
package assembly.giraff; | ||
|
||
import android.support.v4.app.Fragment; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.util.Log; | ||
import android.content.res.Resources; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import assembly.giraff.andtinder.model.CardModel; | ||
import assembly.giraff.andtinder.view.CardContainer; | ||
|
||
import android.widget.Button; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import assembly.giraff.andtinder.model.CardModel; | ||
import assembly.giraff.andtinder.view.CardContainer; | ||
import assembly.giraff.model.CustomCardModel; | ||
|
||
|
||
public class MainViewFragment extends Fragment { | ||
|
||
private CardContainer mCardContainer; | ||
private Button mShareButton; | ||
private static final String TAG = "MainActivity"; | ||
|
||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
|
||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { | ||
View rootView = inflater.inflate(R.layout.activity_main, container, false); | ||
|
||
mCardContainer = (CardContainer) rootView.findViewById(R.id.layoutview); | ||
mShareButton = (Button) rootView.findViewById(R.id.sharebutton); | ||
return rootView; | ||
} | ||
|
||
Resources r = getResources(); | ||
@Override | ||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | ||
super.onViewCreated(view, savedInstanceState); | ||
|
||
ArrayList<Byte[]> mGifDataList = new ArrayList<>(); | ||
|
||
|
||
CustomAdapter adapter = new CustomAdapter(getActivity(),mGifDataList); | ||
CustomAdapter adapter = new CustomAdapter(getActivity(), mGifDataList); | ||
|
||
adapter.add(new CustomCardModel("Title1", "Descripti0on goes her0e 1", "http://gifs.joelglovier.com/accidents/wheelbarrel-dump.gif")); | ||
adapter.add(new CustomCardModel("Title2", "Descr0iption goes her0e 2","http://gifs.joelglovier.com/fail/cat-fail.gif")); | ||
adapter.add(new CustomCardModel("Title2", "Descr0iption goes her0e 2", "http://gifs.joelglovier.com/fail/cat-fail.gif")); | ||
adapter.add(new CustomCardModel("Title3", "Descripti0on goes here 3", "http://gifs.joelglovier.com/aha/aha.gif")); | ||
|
||
CustomCardModel cardModel = new CustomCardModel("Title3", "Descripti0on goes here 3", "http://gifs.joelglovier.com/big-lebowski/no-huh-uh.gif"); | ||
cardModel.setOnClickListener(new CardModel.OnClickListener() { | ||
@Override | ||
public void OnClickListener() { | ||
Log.i("Swipeable Cards","I am pressing the card"); | ||
Log.i("Swipeable Cards", "I am pressing the card"); | ||
} | ||
}); | ||
|
||
cardModel.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() { | ||
@Override | ||
public void onLike() { | ||
Log.i("Swipeable Cards","I like the card"); | ||
Log.i("Swipeable Cards", "I like the card"); | ||
} | ||
|
||
@Override | ||
public void onDislike() { | ||
Log.i("Swipeable Cards","I dislike the card"); | ||
Log.i("Swipeable Cards", "I dislike the card"); | ||
} | ||
}); | ||
|
||
adapter.add(cardModel); | ||
|
||
mCardContainer.setAdapter(adapter); | ||
|
||
return rootView; | ||
mShareButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
share(mCardContainer.getCurrentCard()); | ||
} | ||
}); | ||
} | ||
|
||
private void share(CardModel cardModel) { | ||
if (cardModel == null) return; | ||
|
||
if (cardModel instanceof CustomCardModel) { | ||
String text = String.format("%s %s", | ||
cardModel.getDescription(), | ||
((CustomCardModel) cardModel).getImg_url()); | ||
|
||
Intent intent = new Intent(getActivity(), ShareActivity.class); | ||
intent.setType("text/plain"); | ||
intent.setAction(Intent.ACTION_SEND); | ||
intent.putExtra(Intent.EXTRA_SUBJECT, cardModel.getTitle()); | ||
intent.putExtra(Intent.EXTRA_TEXT, text); | ||
startActivity(intent); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package assembly.giraff; | ||
|
||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.ActivityInfo; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.ResolveInfo; | ||
import android.graphics.drawable.Drawable; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.AdapterView; | ||
import android.widget.BaseAdapter; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ShareActivity extends BaseActivity implements AdapterView.OnItemClickListener { | ||
|
||
private static final String TAG = "ShareActivity"; | ||
private ListView mListView; | ||
private ShareAdapter mAdapter; | ||
private Intent mBaseIntent; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_share); | ||
mListView = (ListView) findViewById(R.id.list_view); | ||
|
||
mBaseIntent = new Intent(getIntent()); // duplicate | ||
mBaseIntent.setComponent(null); | ||
logIntent(mBaseIntent, "Base Intent"); | ||
|
||
View view = getLayoutInflater().inflate(R.layout.header_share, mListView, false); | ||
mListView.addHeaderView(view); | ||
|
||
PackageManager pm = getPackageManager(); | ||
List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(mBaseIntent, 0); | ||
Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator(pm)); | ||
List<ShareTarget> targets = new ArrayList<>(); | ||
for (ResolveInfo resolveInfo : resolveInfoList) { | ||
targets.add(ShareTarget.from(this, resolveInfo)); | ||
} | ||
|
||
|
||
mAdapter = new ShareAdapter(); | ||
mAdapter.setList(targets); | ||
mListView.setAdapter(mAdapter); | ||
mListView.setOnItemClickListener(this); | ||
} | ||
|
||
@Override | ||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||
ShareTarget target = mAdapter.getItem(position - mListView.getHeaderViewsCount()); | ||
Intent intent = new Intent(mBaseIntent); | ||
intent.setComponent(target.getComponentName()); | ||
logIntent(intent, "Fired intent."); | ||
startActivity(intent); | ||
finish(); | ||
} | ||
|
||
private void logIntent(Intent intent, String label) { | ||
Bundle bundle = intent.getExtras(); | ||
Log.d(TAG, "=== dumping intent: " + label + " ==="); | ||
Log.d(TAG, "action: " + intent.getAction()); | ||
Log.d(TAG, "component: " + intent.getComponent()); | ||
Log.d(TAG, "uri: " + intent.getDataString()); | ||
Log.d(TAG, "extras: "); | ||
for (String key : bundle.keySet()) | ||
Log.d(TAG, "- " + key + ": " + bundle.get(key)); | ||
} | ||
|
||
private static class ShareAdapter extends BaseAdapter { | ||
|
||
List<ShareTarget> list = new ArrayList<>(); | ||
|
||
public void setList(List<ShareTarget> list) { | ||
this.list = list; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return list.size(); | ||
} | ||
|
||
@Override | ||
public ShareTarget getItem(int position) { | ||
return list.get(position); | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return position; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
View view = convertView; | ||
ViewHolder holder; | ||
if (view == null) { | ||
view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.item_share, parent, false); | ||
holder = new ViewHolder(view); | ||
view.setTag(holder); | ||
} else { | ||
holder = (ViewHolder) view.getTag(); | ||
} | ||
|
||
// Data binding | ||
ShareTarget target = getItem(position); | ||
holder.text.setText(target.getName()); | ||
holder.text.setCompoundDrawablesWithIntrinsicBounds(target.getIcon(), null, null, null); | ||
|
||
return view; | ||
} | ||
} | ||
|
||
private static class ViewHolder { | ||
TextView text; | ||
|
||
public ViewHolder(View view) { | ||
text = (TextView) view.findViewById(R.id.text); | ||
} | ||
} | ||
|
||
private static class ShareTarget { | ||
private CharSequence name; | ||
private Drawable icon; | ||
private ComponentName componentName; | ||
|
||
public static ShareTarget from(Context context, ResolveInfo resolveInfo) { | ||
PackageManager pm = context.getPackageManager(); | ||
ActivityInfo actInfo = resolveInfo.activityInfo; | ||
ComponentName componentName = new ComponentName(actInfo.packageName, actInfo.name); | ||
ShareTarget shareTarget = new ShareTarget(); | ||
try { | ||
shareTarget.componentName = componentName; | ||
shareTarget.name = actInfo.loadLabel(pm); | ||
shareTarget.icon = pm.getActivityIcon(componentName); | ||
} catch (PackageManager.NameNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return shareTarget; | ||
} | ||
|
||
public CharSequence getName() { | ||
return name; | ||
} | ||
|
||
public Drawable getIcon() { | ||
return icon; | ||
} | ||
|
||
public ComponentName getComponentName() { | ||
return componentName; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,13 +24,12 @@ | |
import android.widget.AdapterView; | ||
import android.widget.ListAdapter; | ||
|
||
import java.util.Random; | ||
|
||
import assembly.giraff.R; | ||
import assembly.giraff.andtinder.model.CardModel; | ||
import assembly.giraff.andtinder.model.Orientations.Orientation; | ||
|
||
import java.util.Random; | ||
|
||
public class CardContainer extends AdapterView<ListAdapter> { | ||
public static final int INVALID_POINTER_ID = -1; | ||
private int mActivePointerId = INVALID_POINTER_ID; | ||
|
@@ -401,6 +400,14 @@ public void setGravity(int gravity) { | |
mGravity = gravity; | ||
} | ||
|
||
// TODO is it the correct way to get currently shown card? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any idea? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alouanemed I think @akhyrul is not sure how to get the currently shown card (when you tap to share). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aaah yeah we have getItem() inside CustomAdapter class ,you can use that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, sorry I didn't check that. Thanks! |
||
public CardModel getCurrentCard(){ | ||
if (getAdapter() == null || getAdapter().getCount() == 0) | ||
return null; | ||
|
||
return (CardModel)getAdapter().getItem(0); | ||
} | ||
|
||
public static class LayoutParams extends ViewGroup.LayoutParams { | ||
|
||
int viewType; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:state_pressed="true"> | ||
<shape> | ||
<solid android:color="@color/gold_darker" /> | ||
<corners android:radius="8dp" /> | ||
</shape> | ||
</item> | ||
<item> | ||
<shape> | ||
<solid android:color="@color/gold" /> | ||
<corners android:radius="8dp" /> | ||
</shape> | ||
</item> | ||
</selector> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<size | ||
android:height="16dp" | ||
android:width="16dp" /> | ||
<solid android:color="@color/transparent" /> | ||
</shape> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you are using CardModel instead of CustomCardModel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, base CardModel doesn't have image url. We are sharing this, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does