Add Nearby to share (if applicable)
This commit is contained in:
parent
46308dcbde
commit
5a0b625dc0
@ -3,6 +3,7 @@ package org.fdroid.fdroid;
|
|||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@ -35,6 +36,7 @@ import android.view.Menu;
|
|||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.view.Window;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
@ -68,13 +70,15 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import static android.support.v7.widget.RecyclerView.NO_POSITION;
|
import static android.support.v7.widget.RecyclerView.NO_POSITION;
|
||||||
|
|
||||||
public class AppDetails2 extends AppCompatActivity {
|
public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog.ShareChooserDialogListener {
|
||||||
|
|
||||||
private static final String TAG = "AppDetails2";
|
private static final String TAG = "AppDetails2";
|
||||||
|
|
||||||
|
private static final int REQUEST_ENABLE_BLUETOOTH = 2;
|
||||||
private static final int REQUEST_PERMISSION_DIALOG = 3;
|
private static final int REQUEST_PERMISSION_DIALOG = 3;
|
||||||
private static final int REQUEST_UNINSTALL_DIALOG = 4;
|
private static final int REQUEST_UNINSTALL_DIALOG = 4;
|
||||||
|
|
||||||
|
private FDroidApp mFDroidApp;
|
||||||
private App mApp;
|
private App mApp;
|
||||||
private RecyclerView mRecyclerView;
|
private RecyclerView mRecyclerView;
|
||||||
private AppDetailsRecyclerViewAdapter mAdapter;
|
private AppDetailsRecyclerViewAdapter mAdapter;
|
||||||
@ -83,6 +87,8 @@ public class AppDetails2 extends AppCompatActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
mFDroidApp = (FDroidApp) getApplication();
|
||||||
|
//mFDroidApp.applyTheme(this);
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.app_details2);
|
setContentView(R.layout.app_details2);
|
||||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||||
@ -160,12 +166,31 @@ public class AppDetails2 extends AppCompatActivity {
|
|||||||
shareIntent.setType("text/plain");
|
shareIntent.setType("text/plain");
|
||||||
shareIntent.putExtra(Intent.EXTRA_SUBJECT, mApp.name);
|
shareIntent.putExtra(Intent.EXTRA_SUBJECT, mApp.name);
|
||||||
shareIntent.putExtra(Intent.EXTRA_TEXT, mApp.name + " (" + mApp.summary + ") - https://f-droid.org/app/" + mApp.packageName);
|
shareIntent.putExtra(Intent.EXTRA_TEXT, mApp.name + " (" + mApp.summary + ") - https://f-droid.org/app/" + mApp.packageName);
|
||||||
ShareChooserDialog.createChooser((CoordinatorLayout) findViewById(R.id.rootCoordinator), this, shareIntent);
|
|
||||||
|
boolean showNearbyItem = mApp.isInstalled() && mFDroidApp.bluetoothAdapter != null;
|
||||||
|
ShareChooserDialog.createChooser((CoordinatorLayout) findViewById(R.id.rootCoordinator), this, this, shareIntent, showNearbyItem);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNearby() {
|
||||||
|
/*
|
||||||
|
* If Bluetooth has not been enabled/turned on, then
|
||||||
|
* enabling device discoverability will automatically enable Bluetooth
|
||||||
|
*/
|
||||||
|
Intent discoverBt = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||||
|
discoverBt.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 121);
|
||||||
|
startActivityForResult(discoverBt, REQUEST_ENABLE_BLUETOOTH);
|
||||||
|
// if this is successful, the Bluetooth transfer is started
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResolvedShareIntent(Intent shareIntent) {
|
||||||
|
startActivity(shareIntent);
|
||||||
|
}
|
||||||
|
|
||||||
public class AppDetailsRecyclerViewAdapter
|
public class AppDetailsRecyclerViewAdapter
|
||||||
extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||||
|
|
||||||
@ -715,9 +740,9 @@ public class AppDetails2 extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
//case REQUEST_ENABLE_BLUETOOTH:
|
case REQUEST_ENABLE_BLUETOOTH:
|
||||||
// fdroidApp.sendViaBluetooth(this, resultCode, app.packageName);
|
mFDroidApp.sendViaBluetooth(this, resultCode, mApp.packageName);
|
||||||
// break;
|
break;
|
||||||
case REQUEST_PERMISSION_DIALOG:
|
case REQUEST_PERMISSION_DIALOG:
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
Uri uri = data.getData();
|
Uri uri = data.getData();
|
||||||
|
@ -15,6 +15,7 @@ import android.support.v7.app.AppCompatActivity;
|
|||||||
import android.support.v7.widget.GridLayoutManager;
|
import android.support.v7.widget.GridLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.Menu;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
@ -31,9 +32,12 @@ public class ShareChooserDialog extends BottomSheetDialogFragment {
|
|||||||
private RecyclerView mRecyclerView;
|
private RecyclerView mRecyclerView;
|
||||||
private ArrayList<ResolveInfo> mTargets;
|
private ArrayList<ResolveInfo> mTargets;
|
||||||
private int mParentWidth;
|
private int mParentWidth;
|
||||||
|
private Intent mShareIntent;
|
||||||
|
private boolean mShowNearby;
|
||||||
|
|
||||||
public interface ShareChooserDialogListener {
|
public interface ShareChooserDialogListener {
|
||||||
void onItemSelected(ResolveInfo ri);
|
void onNearby();
|
||||||
|
void onResolvedShareIntent(Intent shareIntent);
|
||||||
}
|
}
|
||||||
private ShareChooserDialogListener mListener;
|
private ShareChooserDialogListener mListener;
|
||||||
|
|
||||||
@ -49,9 +53,10 @@ public class ShareChooserDialog extends BottomSheetDialogFragment {
|
|||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
mParentWidth = getArguments().getInt("width", 640);
|
mParentWidth = getArguments().getInt("width", 640);
|
||||||
Intent intent = getArguments().getParcelable("intent");
|
mShareIntent = getArguments().getParcelable("intent");
|
||||||
|
mShowNearby = getArguments().getBoolean("showNearby", false);
|
||||||
mTargets = new ArrayList<>();
|
mTargets = new ArrayList<>();
|
||||||
List<ResolveInfo> resInfo = getContext().getPackageManager().queryIntentActivities(intent, 0);
|
List<ResolveInfo> resInfo = getContext().getPackageManager().queryIntentActivities(mShareIntent, 0);
|
||||||
if (resInfo != null && resInfo.size() > 0) {
|
if (resInfo != null && resInfo.size() > 0) {
|
||||||
for (ResolveInfo resolveInfo : resInfo) {
|
for (ResolveInfo resolveInfo : resInfo) {
|
||||||
String packageName = resolveInfo.activityInfo.packageName;
|
String packageName = resolveInfo.activityInfo.packageName;
|
||||||
@ -123,7 +128,9 @@ public class ShareChooserDialog extends BottomSheetDialogFragment {
|
|||||||
|
|
||||||
RecyclerView.Adapter init(List<ResolveInfo> targetedShareIntents) {
|
RecyclerView.Adapter init(List<ResolveInfo> targetedShareIntents) {
|
||||||
mIntents = new ArrayList<>();
|
mIntents = new ArrayList<>();
|
||||||
mIntents.add("Nearby");
|
if (mShowNearby) {
|
||||||
|
mIntents.add("Nearby (string contents do not matter!)");
|
||||||
|
}
|
||||||
for (ResolveInfo ri : targetedShareIntents) {
|
for (ResolveInfo ri : targetedShareIntents) {
|
||||||
mIntents.add(ri);
|
mIntents.add(ri);
|
||||||
}
|
}
|
||||||
@ -146,7 +153,15 @@ public class ShareChooserDialog extends BottomSheetDialogFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(VH holder, int position) {
|
public void onBindViewHolder(VH holder, int position) {
|
||||||
if (getItemViewType(position) == 1) {
|
if (getItemViewType(position) == 1) {
|
||||||
//holder.label.setText((CharSequence)mIntents.get(position));
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
if (mListener != null) {
|
||||||
|
mListener.onNearby();
|
||||||
|
}
|
||||||
|
dismiss();
|
||||||
|
}
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final ResolveInfo ri = (ResolveInfo) mIntents.get(position);
|
final ResolveInfo ri = (ResolveInfo) mIntents.get(position);
|
||||||
@ -155,8 +170,13 @@ public class ShareChooserDialog extends BottomSheetDialogFragment {
|
|||||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
if (mListener != null)
|
if (mListener != null) {
|
||||||
mListener.onItemSelected(ri);
|
Intent intent = new Intent(mShareIntent);
|
||||||
|
ComponentName name = new ComponentName(ri.activityInfo.applicationInfo.packageName,
|
||||||
|
ri.activityInfo.name);
|
||||||
|
intent.setComponent(name);
|
||||||
|
mListener.onResolvedShareIntent(intent);
|
||||||
|
}
|
||||||
dismiss();
|
dismiss();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -170,25 +190,13 @@ public class ShareChooserDialog extends BottomSheetDialogFragment {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createChooser(CoordinatorLayout rootView, final AppCompatActivity parent, final Intent shareIntent) {
|
public static void createChooser(CoordinatorLayout rootView, ShareChooserDialog.ShareChooserDialogListener listener, final AppCompatActivity parent, final Intent shareIntent, boolean showNearbyItem) {
|
||||||
ShareChooserDialog d = new ShareChooserDialog();
|
ShareChooserDialog d = new ShareChooserDialog();
|
||||||
d.setListener(new ShareChooserDialog.ShareChooserDialogListener() {
|
d.setListener(listener);
|
||||||
@Override
|
|
||||||
public void onItemSelected(final ResolveInfo ri) {
|
|
||||||
startActivityForResolveInfo(ri);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startActivityForResolveInfo(ResolveInfo ri) {
|
|
||||||
Intent intent = new Intent(shareIntent);
|
|
||||||
ComponentName name = new ComponentName(ri.activityInfo.applicationInfo.packageName,
|
|
||||||
ri.activityInfo.name);
|
|
||||||
intent.setComponent(name);
|
|
||||||
parent.startActivity(intent);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putInt("width", rootView.getWidth());
|
args.putInt("width", rootView.getWidth());
|
||||||
args.putParcelable("intent", shareIntent);
|
args.putParcelable("intent", shareIntent);
|
||||||
|
args.putBoolean("showNearby", showNearbyItem);
|
||||||
d.setArguments(args);
|
d.setArguments(args);
|
||||||
d.show(parent.getSupportFragmentManager(), "Share");
|
d.show(parent.getSupportFragmentManager(), "Share");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user