-
Notifications
You must be signed in to change notification settings - Fork 190
/
MainActivity.java
136 lines (119 loc) · 5.25 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.yausername.youtubedl_android_example;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.yausername.youtubedl_android.YoutubeDL;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnStreamingExample;
private Button btnDownloadingExample;
private Button btnCommandExample;
private Button btnUpdate;
private ProgressBar progressBar;
private boolean updating = false;
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
}
@Override
protected void onDestroy() {
compositeDisposable.dispose();
super.onDestroy();
}
private void initListeners() {
btnStreamingExample.setOnClickListener(this);
btnDownloadingExample.setOnClickListener(this);
btnCommandExample.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
}
private void initViews() {
btnStreamingExample = findViewById(R.id.btn_streaming_example);
btnDownloadingExample = findViewById(R.id.btn_downloading_example);
btnCommandExample = findViewById(R.id.btn_command_example);
btnUpdate = findViewById(R.id.btn_update);
progressBar = findViewById(R.id.progress_bar);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_streaming_example: {
Intent i = new Intent(MainActivity.this, StreamingExampleActivity.class);
startActivity(i);
break;
}
case R.id.btn_downloading_example: {
Intent i = new Intent(MainActivity.this, DownloadingExampleActivity.class);
startActivity(i);
break;
}
case R.id.btn_command_example: {
Intent i = new Intent(MainActivity.this, CommandExampleActivity.class);
startActivity(i);
break;
}
case R.id.btn_update: {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Update Channel")
.setItems(new String[]{"Stable Releases", "Nightly Releases", "Master Releases"},
(dialogInterface, which) -> {
if (which == 0)
updateYoutubeDL(YoutubeDL.UpdateChannel._STABLE);
else if (which == 1)
updateYoutubeDL(YoutubeDL.UpdateChannel._NIGHTLY);
else
updateYoutubeDL(YoutubeDL.UpdateChannel._MASTER);
})
.create();
dialog.show();
break;
}
}
}
private void updateYoutubeDL(YoutubeDL.UpdateChannel updateChannel) {
if (updating) {
Toast.makeText(MainActivity.this, "Update is already in progress!", Toast.LENGTH_LONG).show();
return;
}
updating = true;
progressBar.setVisibility(View.VISIBLE);
Disposable disposable = Observable.fromCallable(() -> YoutubeDL.getInstance().updateYoutubeDL(this, updateChannel))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(status -> {
progressBar.setVisibility(View.GONE);
switch (status) {
case DONE:
Toast.makeText(MainActivity.this, "Update successful " + YoutubeDL.getInstance().versionName(this), Toast.LENGTH_LONG).show();
break;
case ALREADY_UP_TO_DATE:
Toast.makeText(MainActivity.this, "Already up to date " + YoutubeDL.getInstance().versionName(this), Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(MainActivity.this, status.toString(), Toast.LENGTH_LONG).show();
break;
}
updating = false;
}, e -> {
if (BuildConfig.DEBUG) Log.e(TAG, "failed to update", e);
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "update failed", Toast.LENGTH_LONG).show();
updating = false;
});
compositeDisposable.add(disposable);
}
}