Use activity instead of dialog
This commit is contained in:
55
mobile/src/com/cradle/iitc_mobile/share/IntentFragment.java
Normal file
55
mobile/src/com/cradle/iitc_mobile/share/IntentFragment.java
Normal file
@ -0,0 +1,55 @@
|
||||
package com.cradle.iitc_mobile.share;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AbsListView;
|
||||
import android.widget.AbsListView.OnScrollListener;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
|
||||
public class IntentFragment extends Fragment implements OnScrollListener, OnItemClickListener {
|
||||
private Intent mIntent;
|
||||
private IntentListView mListView;
|
||||
private int mScrollIndex, mScrollTop;
|
||||
|
||||
public String getTitle() {
|
||||
return getArguments().getString("title");
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
Bundle args = getArguments();
|
||||
|
||||
mIntent = args.getParcelable("intent");
|
||||
mListView = new IntentListView(getActivity());
|
||||
mListView.setIntent(mIntent);
|
||||
if (mScrollIndex != -1 && mScrollTop != -1)
|
||||
mListView.setSelectionFromTop(mScrollIndex, mScrollTop);
|
||||
mListView.setOnScrollListener(this);
|
||||
mListView.setOnItemClickListener(this);
|
||||
|
||||
return mListView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
|
||||
{
|
||||
Intent intent = mListView.getTargetIntent(position);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
||||
mScrollIndex = mListView.getFirstVisiblePosition();
|
||||
View v = mListView.getChildAt(0);
|
||||
mScrollTop = (v == null) ? 0 : v.getTop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScrollStateChanged(AbsListView view, int scrollState) {
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.cradle.iitc_mobile.share;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
|
||||
public class IntentFragmentAdapter extends FragmentPagerAdapter {
|
||||
private List<IntentFragment> mTabs;
|
||||
|
||||
public IntentFragmentAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
|
||||
mTabs = new ArrayList<IntentFragment>();
|
||||
}
|
||||
|
||||
public void add(IntentFragment fragment) {
|
||||
mTabs.add(fragment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mTabs.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
return mTabs.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return mTabs.get(position).getTitle();
|
||||
}
|
||||
}
|
136
mobile/src/com/cradle/iitc_mobile/share/IntentListView.java
Normal file
136
mobile/src/com/cradle/iitc_mobile/share/IntentListView.java
Normal file
@ -0,0 +1,136 @@
|
||||
package com.cradle.iitc_mobile.share;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.cradle.iitc_mobile.R;
|
||||
|
||||
import android.app.Activity;
|
||||
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.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class IntentListView extends ListView {
|
||||
private class IntentAdapter extends ArrayAdapter<ResolveInfo>
|
||||
{
|
||||
private IntentAdapter()
|
||||
{
|
||||
super(IntentListView.this.getContext(), android.R.layout.simple_list_item_1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
|
||||
TextView view = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
|
||||
|
||||
ActivityInfo info = getItem(position).activityInfo;
|
||||
CharSequence label = info.loadLabel(mPackageManager);
|
||||
Drawable icon = info.loadIcon(mPackageManager);
|
||||
|
||||
view.setText(label);
|
||||
view.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.icon_margin));
|
||||
view.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, null, null, null);
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
private IntentAdapter mAdapter;
|
||||
private PackageManager mPackageManager;
|
||||
private Intent mIntent = null;
|
||||
|
||||
public IntentListView(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public IntentListView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public IntentListView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
mPackageManager = getContext().getPackageManager();
|
||||
mAdapter = new IntentAdapter();
|
||||
setAdapter(mAdapter);
|
||||
}
|
||||
|
||||
public ResolveInfo getItem(int position) {
|
||||
return mAdapter.getItem(position);
|
||||
}
|
||||
|
||||
public void setIntent(Intent intent)
|
||||
{
|
||||
mIntent = intent;
|
||||
|
||||
mAdapter.setNotifyOnChange(false);
|
||||
mAdapter.clear();
|
||||
|
||||
String packageName = getContext().getPackageName();
|
||||
// TODO find default, show on top
|
||||
// TODO exclude IITCm
|
||||
|
||||
List<ResolveInfo> activities = mPackageManager.queryIntentActivities(intent, 0);
|
||||
ResolveInfo defaultTarget = mPackageManager.resolveActivity(intent, 0);
|
||||
|
||||
boolean hasCopyIntent = false;
|
||||
for (ResolveInfo resolveInfo : activities) { // search for "Copy to clipboard" target, provided by Drive
|
||||
if (resolveInfo.activityInfo.name.equals("com.google.android.apps.docs.app.SendTextToClipboardActivity")
|
||||
&& resolveInfo.activityInfo.packageName.equals("com.google.android.apps.docs"))
|
||||
hasCopyIntent = true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < activities.size(); i++) { // use traditional loop since List may change during interation
|
||||
ResolveInfo info = activities.get(i);
|
||||
ActivityInfo activity = info.activityInfo;
|
||||
|
||||
// remove all IITCm intents, except for SendToClipboard in case Drive is not installed
|
||||
if (activity.packageName.equals(packageName))
|
||||
{
|
||||
if (hasCopyIntent || !activity.name.equals(SendToClipboard.class.getCanonicalName()))
|
||||
{
|
||||
activities.remove(i);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// move default Intent to top
|
||||
if (info.activityInfo.packageName.equals(defaultTarget.activityInfo.packageName)
|
||||
&& info.activityInfo.name.equals(defaultTarget.activityInfo.name))
|
||||
{
|
||||
activities.remove(i);
|
||||
activities.add(0, info);
|
||||
}
|
||||
}
|
||||
|
||||
mAdapter.addAll(activities);
|
||||
mAdapter.setNotifyOnChange(true);
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public Intent getTargetIntent(int position) {
|
||||
ActivityInfo activity = mAdapter.getItem(position).activityInfo;
|
||||
|
||||
Intent intent = new Intent(mIntent)
|
||||
.setComponent(new ComponentName(activity.packageName, activity.name))
|
||||
.setPackage(activity.packageName);
|
||||
|
||||
return intent;
|
||||
}
|
||||
}
|
28
mobile/src/com/cradle/iitc_mobile/share/SendToClipboard.java
Normal file
28
mobile/src/com/cradle/iitc_mobile/share/SendToClipboard.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.cradle.iitc_mobile.share;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class SendToClipboard extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
|
||||
|
||||
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
|
||||
ClipData clip = ClipData.newPlainText("Copied Text ", text);
|
||||
clipboard.setPrimaryClip(clip);
|
||||
|
||||
Toast.makeText(this, "Copied to clipboard…", Toast.LENGTH_SHORT).show();
|
||||
|
||||
finish();
|
||||
setResult(RESULT_OK);
|
||||
}
|
||||
}
|
120
mobile/src/com/cradle/iitc_mobile/share/ShareActivity.java
Normal file
120
mobile/src/com/cradle/iitc_mobile/share/ShareActivity.java
Normal file
@ -0,0 +1,120 @@
|
||||
package com.cradle.iitc_mobile.share;
|
||||
|
||||
import android.app.ActionBar;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.NavUtils;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.cradle.iitc_mobile.R;
|
||||
|
||||
public class ShareActivity extends FragmentActivity implements ActionBar.TabListener {
|
||||
private boolean mIsPortal;
|
||||
private String mLl;
|
||||
private String mTitle;
|
||||
private int mZoom;
|
||||
IntentFragmentAdapter mFragmentAdapter;
|
||||
ViewPager mViewPager;
|
||||
|
||||
private void addTab(Intent intent, String label)
|
||||
{
|
||||
IntentFragment fragment = new IntentFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelable("intent", intent);
|
||||
args.putString("title", label);
|
||||
fragment.setArguments(args);
|
||||
mFragmentAdapter.add(fragment);
|
||||
}
|
||||
|
||||
private String getUrl() {
|
||||
String url = "http://www.ingress.com/intel?ll=" + mLl + "&z=" + mZoom;
|
||||
if (mIsPortal)
|
||||
url += "&pll=" + mLl;
|
||||
return url;
|
||||
}
|
||||
|
||||
private void setupIntents() {
|
||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
||||
intent.setType("text/plain");
|
||||
intent.putExtra(Intent.EXTRA_TEXT, getUrl());
|
||||
intent.putExtra(Intent.EXTRA_SUBJECT, mTitle);
|
||||
addTab(intent, "Share");
|
||||
|
||||
String geoUri = "geo:" + mLl;
|
||||
intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(geoUri));
|
||||
addTab(intent, "Map");
|
||||
|
||||
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getUrl()));
|
||||
addTab(intent, "Browser");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_share);
|
||||
|
||||
Intent intent = getIntent();
|
||||
mTitle = intent.getStringExtra("title");
|
||||
mLl = intent.getDoubleExtra("lat", 0) + "," + intent.getDoubleExtra("lng", 0);
|
||||
mZoom = intent.getIntExtra("zoom", 0);
|
||||
|
||||
if (mTitle == null) {
|
||||
mTitle = "Intel Map";
|
||||
mIsPortal = false;
|
||||
}
|
||||
|
||||
final ActionBar actionBar = getActionBar();
|
||||
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
mFragmentAdapter = new IntentFragmentAdapter(getSupportFragmentManager());
|
||||
setupIntents();
|
||||
|
||||
mViewPager = (ViewPager) findViewById(R.id.pager);
|
||||
mViewPager.setAdapter(mFragmentAdapter);
|
||||
|
||||
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
actionBar.setSelectedNavigationItem(position);
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
|
||||
IntentFragment fragment = (IntentFragment) mFragmentAdapter.getItem(i);
|
||||
|
||||
actionBar.addTab(actionBar
|
||||
.newTab()
|
||||
.setText(fragment.getTitle())
|
||||
.setTabListener(this));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
NavUtils.navigateUpFromSameTask(this);
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
|
||||
mViewPager.setCurrentItem(tab.getPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user