2016-04-22 01:28:01 +02:00
|
|
|
package us.shandian.giga.ui.fragment;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Fragment;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.ServiceConnection;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.IBinder;
|
2016-09-14 11:29:22 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2018-06-11 14:44:00 +02:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-05-23 16:53:50 +02:00
|
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2016-04-22 01:28:01 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
2018-06-15 16:15:55 +02:00
|
|
|
import org.schabi.newpipe.download.DeleteDownloadManager;
|
2017-05-23 16:53:50 +02:00
|
|
|
|
2018-06-11 14:44:00 +02:00
|
|
|
import io.reactivex.disposables.Disposable;
|
2016-04-22 01:28:01 +02:00
|
|
|
import us.shandian.giga.get.DownloadManager;
|
|
|
|
import us.shandian.giga.service.DownloadManagerService;
|
|
|
|
import us.shandian.giga.ui.adapter.MissionAdapter;
|
2017-06-28 07:27:32 +02:00
|
|
|
|
|
|
|
public abstract class MissionsFragment extends Fragment {
|
2018-06-11 14:44:00 +02:00
|
|
|
private DownloadManager mDownloadManager;
|
2017-06-28 07:27:32 +02:00
|
|
|
private DownloadManagerService.DMBinder mBinder;
|
|
|
|
|
|
|
|
private SharedPreferences mPrefs;
|
|
|
|
private boolean mLinear;
|
|
|
|
private MenuItem mSwitch;
|
|
|
|
|
|
|
|
private RecyclerView mList;
|
|
|
|
private MissionAdapter mAdapter;
|
|
|
|
private GridLayoutManager mGridManager;
|
|
|
|
private LinearLayoutManager mLinearManager;
|
|
|
|
private Context mActivity;
|
2018-06-15 16:15:55 +02:00
|
|
|
private DeleteDownloadManager mDeleteDownloadManager;
|
2018-06-11 14:44:00 +02:00
|
|
|
private Disposable mDeleteDisposable;
|
2017-06-28 07:27:32 +02:00
|
|
|
|
|
|
|
private ServiceConnection mConnection = new ServiceConnection() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder binder) {
|
|
|
|
mBinder = (DownloadManagerService.DMBinder) binder;
|
2018-06-11 14:44:00 +02:00
|
|
|
mDownloadManager = setupDownloadManager(mBinder);
|
2018-06-15 16:15:55 +02:00
|
|
|
if (mDeleteDownloadManager != null) {
|
|
|
|
mDeleteDownloadManager.setDownloadManager(mDownloadManager);
|
2018-06-11 14:44:00 +02:00
|
|
|
updateList();
|
|
|
|
}
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServiceDisconnected(ComponentName name) {
|
|
|
|
// What to do?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-06-15 16:15:55 +02:00
|
|
|
public void setDeleteManager(@NonNull DeleteDownloadManager deleteDownloadManager) {
|
|
|
|
mDeleteDownloadManager = deleteDownloadManager;
|
2018-06-11 14:44:00 +02:00
|
|
|
if (mDownloadManager != null) {
|
2018-06-15 16:15:55 +02:00
|
|
|
mDeleteDownloadManager.setDownloadManager(mDownloadManager);
|
2018-06-11 14:44:00 +02:00
|
|
|
updateList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 07:27:32 +02:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View v = inflater.inflate(R.layout.missions, container, false);
|
|
|
|
|
|
|
|
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
mLinear = mPrefs.getBoolean("linear", false);
|
|
|
|
|
|
|
|
// Bind the service
|
|
|
|
Intent i = new Intent();
|
|
|
|
i.setClass(getActivity(), DownloadManagerService.class);
|
|
|
|
getActivity().bindService(i, mConnection, Context.BIND_AUTO_CREATE);
|
|
|
|
|
|
|
|
// Views
|
2017-09-03 08:04:18 +02:00
|
|
|
mList = v.findViewById(R.id.mission_recycler);
|
2017-06-28 07:27:32 +02:00
|
|
|
|
|
|
|
// Init
|
|
|
|
mGridManager = new GridLayoutManager(getActivity(), 2);
|
|
|
|
mLinearManager = new LinearLayoutManager(getActivity());
|
|
|
|
mList.setLayoutManager(mGridManager);
|
|
|
|
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Added in API level 23.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onAttach(Context activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
|
|
|
// Bug: in api< 23 this is never called
|
|
|
|
// so mActivity=null
|
|
|
|
// so app crashes with nullpointer exception
|
|
|
|
mActivity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deprecated in API level 23,
|
|
|
|
* but must remain to allow compatibility with api<23
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
|
|
|
mActivity = activity;
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:44:00 +02:00
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
2018-06-15 16:15:55 +02:00
|
|
|
if (mDeleteDownloadManager != null) {
|
|
|
|
mDeleteDisposable = mDeleteDownloadManager.getUndoObservable().subscribe(mission -> {
|
2018-06-11 14:44:00 +02:00
|
|
|
if (mAdapter != null) {
|
|
|
|
mAdapter.updateItemList();
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 07:27:32 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
getActivity().unbindService(mConnection);
|
2018-06-11 14:44:00 +02:00
|
|
|
if (mDeleteDisposable != null) {
|
|
|
|
mDeleteDisposable.dispose();
|
|
|
|
}
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
|
2017-02-02 01:58:47 +01:00
|
|
|
@Override
|
2017-06-28 07:27:32 +02:00
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
return super.onOptionsItemSelected(item);
|
2017-05-23 16:53:50 +02:00
|
|
|
|
|
|
|
/*switch (item.getItemId()) {
|
2017-06-28 07:27:32 +02:00
|
|
|
case R.id.switch_mode:
|
2016-04-22 01:28:01 +02:00
|
|
|
mLinear = !mLinear;
|
|
|
|
updateList();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
2017-05-23 16:53:50 +02:00
|
|
|
}*/
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyChange() {
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateList() {
|
2018-06-15 16:15:55 +02:00
|
|
|
mAdapter = new MissionAdapter((Activity) mActivity, mBinder, mDownloadManager, mDeleteDownloadManager, mLinear);
|
2017-06-28 07:27:32 +02:00
|
|
|
|
|
|
|
if (mLinear) {
|
|
|
|
mList.setLayoutManager(mLinearManager);
|
|
|
|
} else {
|
|
|
|
mList.setLayoutManager(mGridManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
mList.setAdapter(mAdapter);
|
|
|
|
|
|
|
|
if (mSwitch != null) {
|
|
|
|
mSwitch.setIcon(mLinear ? R.drawable.grid : R.drawable.list);
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:44:00 +02:00
|
|
|
mPrefs.edit().putBoolean("linear", mLinear).apply();
|
2017-06-28 07:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract DownloadManager setupDownloadManager(DownloadManagerService.DMBinder binder);
|
2016-04-22 01:28:01 +02:00
|
|
|
}
|