Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.support.annotation.StringRes;
import android.support.v7.preference.Preference;
import android.util.Log;
import android.widget.Toast;

import com.nononsenseapps.filepicker.Utils;

Expand All @@ -34,6 +35,7 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {

private String DOWNLOAD_PATH_VIDEO_PREFERENCE;
private String DOWNLOAD_PATH_AUDIO_PREFERENCE;
private String STORAGE_USE_SAF_PREFERENCE;

private Preference prefPathVideo;
private Preference prefPathAudio;
Expand All @@ -47,6 +49,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {

DOWNLOAD_PATH_VIDEO_PREFERENCE = getString(R.string.download_path_video_key);
DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key);
STORAGE_USE_SAF_PREFERENCE = getString(R.string.storage_use_saf);
final String downloadStorageAsk = getString(R.string.downloads_storage_ask);

prefPathVideo = findPreference(DOWNLOAD_PATH_VIDEO_PREFERENCE);
Expand Down Expand Up @@ -169,7 +172,10 @@ public boolean onPreferenceTreeClick(Preference preference) {
String key = preference.getKey();
int request;

if (key.equals(DOWNLOAD_PATH_VIDEO_PREFERENCE)) {
if (key.equals(STORAGE_USE_SAF_PREFERENCE)) {
Toast.makeText(getContext(), R.string.download_choose_new_path, Toast.LENGTH_LONG).show();
return true;
} else if (key.equals(DOWNLOAD_PATH_VIDEO_PREFERENCE)) {
request = REQUEST_DOWNLOAD_VIDEO_PATH;
} else if (key.equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) {
request = REQUEST_DOWNLOAD_AUDIO_PATH;
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/us/shandian/giga/get/DownloadMission.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package us.shandian.giga.get;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.schabi.newpipe.Downloader;
Expand Down Expand Up @@ -264,11 +263,7 @@ void establishConnection(int threadId, HttpURLConnection conn) throws IOExceptio


private void notify(int what) {
Message m = new Message();
m.what = what;
m.obj = this;

mHandler.sendMessage(m);
mHandler.obtainMessage(what, this).sendToTarget();
}

synchronized void notifyProgress(long deltaLen) {
Expand Down Expand Up @@ -408,6 +403,7 @@ private void notifyPostProcessing(int state) {
}
}


/**
* Start downloading with multiple threads.
*/
Expand All @@ -416,14 +412,20 @@ public void start() {

// ensure that the previous state is completely paused.
joinForThread(init);
if (threads != null)
if (threads != null) {
for (Thread thread : threads) joinForThread(thread);
threads = null;
}

running = true;
errCode = ERROR_NOTHING;

if (hasInvalidStorage()) {
notifyError(ERROR_FILE_CREATION, null);
return;
}

if (current >= urls.length) {
threads = null;
runAsync(1, this::notifyFinished);
return;
}
Expand Down Expand Up @@ -664,7 +666,7 @@ public boolean hasInvalidStorage() {
* @return {@code true} is this mission its "healthy", otherwise, {@code false}
*/
public boolean isCorrupt() {
return (isPsFailed() || errCode == ERROR_POSTPROCESSING_HOLD) || isFinished() || hasInvalidStorage();
return (isPsFailed() || errCode == ERROR_POSTPROCESSING_HOLD) || isFinished();
}

private boolean doPostprocessing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public String getTag() {
public boolean existsAsFile() {
if (source == null) return false;

// WARNING: DocumentFile.exists() and DocumentFile.isFile() methods are slow
boolean exists = docFile == null ? ioFile.exists() : docFile.exists();
boolean isFile = docFile == null ? ioFile.isFile() : docFile.isFile();// ¿docFile.isVirtual() means is no-physical?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,12 @@ boolean runMissions() {

boolean flag = false;
for (DownloadMission mission : mMissionsPending) {
if (mission.running || !mission.enqueued || mission.isFinished() || mission.hasInvalidStorage())
if (mission.running || !mission.enqueued || mission.isFinished())
continue;

resumeMission(mission);
if (mission.errCode != DownloadMission.ERROR_NOTHING) continue;

if (mPrefQueueLimit) return true;
flag = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
Expand All @@ -19,6 +20,7 @@
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
Expand Down Expand Up @@ -88,14 +90,14 @@ public class DownloadManagerService extends Service {
private Builder downloadDoneNotification = null;
private StringBuilder downloadDoneList = null;

private final ArrayList<Handler> mEchoObservers = new ArrayList<>(1);
private final ArrayList<Callback> mEchoObservers = new ArrayList<>(1);

private ConnectivityManager mConnectivityManager;
private BroadcastReceiver mNetworkStateListener = null;
private ConnectivityManager.NetworkCallback mNetworkStateListenerL = null;

private SharedPreferences mPrefs = null;
private final SharedPreferences.OnSharedPreferenceChangeListener mPrefChangeListener = this::handlePreferenceChange;
private final OnSharedPreferenceChangeListener mPrefChangeListener = this::handlePreferenceChange;

private boolean mLockAcquired = false;
private LockManager mLock = null;
Expand Down Expand Up @@ -128,12 +130,7 @@ public void onCreate() {
}

mBinder = new DownloadManagerBinder();
mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(Message msg) {
DownloadManagerService.this.handleMessage(msg);
}
};
mHandler = new Handler(this::handleMessage);

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

Expand Down Expand Up @@ -272,7 +269,7 @@ public IBinder onBind(Intent intent) {
return mBinder;
}

public void handleMessage(Message msg) {
private boolean handleMessage(@NonNull Message msg) {
DownloadMission mission = (DownloadMission) msg.obj;

switch (msg.what) {
Expand Down Expand Up @@ -300,14 +297,12 @@ public void handleMessage(Message msg) {
mFailedDownloads.delete(mFailedDownloads.indexOfValue(mission));

synchronized (mEchoObservers) {
for (Handler handler : mEchoObservers) {
Message echo = new Message();
echo.what = msg.what;
echo.obj = msg.obj;

handler.sendMessage(echo);
for (Callback observer : mEchoObservers) {
observer.handleMessage(msg);
}
}

return true;
}

private void handleConnectivityState(boolean updateOnly) {
Expand Down Expand Up @@ -515,7 +510,7 @@ private PendingIntent makePendingIntent(String action) {
return PendingIntent.getService(this, intent.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void manageObservers(Handler handler, boolean add) {
private void manageObservers(Callback handler, boolean add) {
synchronized (mEchoObservers) {
if (add) {
mEchoObservers.add(handler);
Expand Down Expand Up @@ -596,11 +591,11 @@ public boolean askForSavePath() {
);
}

public void addMissionEventListener(Handler handler) {
public void addMissionEventListener(Callback handler) {
manageObservers(handler, true);
}

public void removeMissionEventListener(Handler handler) {
public void removeMissionEventListener(Callback handler) {
manageObservers(handler, false);
}

Expand Down
63 changes: 31 additions & 32 deletions app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -77,7 +76,7 @@
import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_EXCEPTION;
import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_HOST;

public class MissionAdapter extends Adapter<ViewHolder> {
public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callback {
private static final SparseArray<String> ALGORITHMS = new SparseArray<>();
private static final String TAG = "MissionAdapter";
private static final String UNDEFINED_PROGRESS = "--.-%";
Expand Down Expand Up @@ -111,21 +110,7 @@ public MissionAdapter(Context context, @NonNull DownloadManager downloadManager,
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayout = R.layout.mission_item;

mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DownloadManagerService.MESSAGE_PROGRESS:
case DownloadManagerService.MESSAGE_ERROR:
case DownloadManagerService.MESSAGE_FINISHED:
onServiceMessage(msg);
break;
}

if (mStartButton != null && mPauseButton != null)
checkMasterButtonsVisibility();
}
};
mHandler = new Handler(context.getMainLooper());

mEmptyMessage = emptyMessage;

Expand Down Expand Up @@ -403,29 +388,40 @@ private boolean checkInvalidFile(@NonNull Mission mission) {
return true;
}

public Handler getMessenger() {
return mHandler;
}
@Override
public boolean handleMessage(@NonNull Message msg) {
if (mStartButton != null && mPauseButton != null) {
checkMasterButtonsVisibility();
}

switch (msg.what) {
case DownloadManagerService.MESSAGE_PROGRESS:
case DownloadManagerService.MESSAGE_ERROR:
case DownloadManagerService.MESSAGE_FINISHED:
break;
default:
return false;
}

private void onServiceMessage(@NonNull Message msg) {
if (msg.what == DownloadManagerService.MESSAGE_PROGRESS) {
setAutoRefresh(true);
return;
return true;
}

for (int i = 0; i < mPendingDownloadsItems.size(); i++) {
ViewHolderItem h = mPendingDownloadsItems.get(i);
for (ViewHolderItem h : mPendingDownloadsItems) {
if (h.item.mission != msg.obj) continue;

if (msg.what == DownloadManagerService.MESSAGE_FINISHED) {
// DownloadManager should mark the download as finished
applyChanges();
return;
return true;
}

updateProgress(h);
return;
return true;
}

return false;
}

private void showError(@NonNull DownloadMission mission) {
Expand Down Expand Up @@ -563,16 +559,15 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
updateProgress(h);
return true;
case R.id.retry:
if (mission.hasInvalidStorage()) {
if (mission.isPsRunning()) {
mission.psContinue(true);
} else {
mDownloadManager.tryRecover(mission);
if (mission.storage.isInvalid())
mRecover.tryRecover(mission);
else
recoverMission(mission);

return true;
}
mission.psContinue(true);
return true;
case R.id.cancel:
mission.psContinue(false);
Expand Down Expand Up @@ -659,9 +654,13 @@ private void checkEmptyMessageVisibility() {

public void checkMasterButtonsVisibility() {
boolean[] state = mIterator.hasValidPendingMissions();
setButtonVisible(mPauseButton, state[0]);
setButtonVisible(mStartButton, state[1]);
}

mPauseButton.setVisible(state[0]);
mStartButton.setVisible(state[1]);
private static void setButtonVisible(MenuItem button, boolean visible) {
if (button.isVisible() != visible)
button.setVisible(visible);
}

public void ensurePausedMissions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void onServiceConnected(ComponentName name, IBinder binder) {

setAdapterButtons();

mBinder.addMissionEventListener(mAdapter.getMessenger());
mBinder.addMissionEventListener(mAdapter);
mBinder.enableNotifications(false);

updateList();
Expand Down Expand Up @@ -159,7 +159,7 @@ public void onDestroy() {
super.onDestroy();
if (mBinder == null || mAdapter == null) return;

mBinder.removeMissionEventListener(mAdapter.getMessenger());
mBinder.removeMissionEventListener(mAdapter);
mBinder.enableNotifications(true);
mContext.unbindService(mConnection);
mAdapter.deleterDispose(true);
Expand Down Expand Up @@ -197,12 +197,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
case R.id.start_downloads:
item.setVisible(false);
mPause.setVisible(true);
mBinder.getDownloadManager().startAllMissions();
return true;
case R.id.pause_downloads:
item.setVisible(false);
mStart.setVisible(true);
mBinder.getDownloadManager().pauseAllMissions(false);
mAdapter.ensurePausedMissions();// update items view
default:
Expand Down Expand Up @@ -280,7 +278,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
if (mAdapter != null) {
mAdapter.deleterDispose(false);
mForceUpdate = true;
mBinder.removeMissionEventListener(mAdapter.getMessenger());
mBinder.removeMissionEventListener(mAdapter);
}
}

Expand All @@ -296,7 +294,7 @@ public void onResume() {
mAdapter.forceUpdate();
}

mBinder.addMissionEventListener(mAdapter.getMessenger());
mBinder.addMissionEventListener(mAdapter);
mAdapter.checkMasterButtonsVisibility();
}
if (mBinder != null) mBinder.enableNotifications(false);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<string name="download_path_title">Carpeta de descarga de vídeo</string>
<string name="download_path_summary">Ruta para almacenar los vídeos descargados</string>
<string name="download_path_dialog_title">Introducir directorio de descargas para vídeos</string>
<string name="download_choose_new_path">Cambie las carpetas de descarga para que tenga efecto</string>
<string name="default_resolution_title">Resolución por defecto de vídeo</string>
<string name="play_with_kodi_title">Reproducir con Kodi</string>
<string name="kore_not_found">Aplicación Kore no encontrada. ¿Instalarla?</string>
Expand Down
Loading