Skip to content

Commit

Permalink
app: cumulative update
Browse files Browse the repository at this point in the history
- drop okhttp, use volley
- fix ssl error on android 4.4
- rework main layout
- others
  • Loading branch information
hariimurti committed Apr 9, 2020
1 parent 0921cfa commit dec4232
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 158 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.android.exoplayer:exoplayer:2.11.3'
//noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.2'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.github.ybq:Android-SpinKit:1.4.0'
implementation 'com.android.volley:volley:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
128 changes: 74 additions & 54 deletions app/src/main/java/net/harimurti/tv/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package net.harimurti.tv;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BaseHttpStack;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import net.harimurti.tv.adapter.ViewPagerAdapter;
import net.harimurti.tv.data.Playlist;
import net.harimurti.tv.extra.AsyncSleep;
import net.harimurti.tv.extra.Network;
import net.harimurti.tv.extra.RestClient;
import net.harimurti.tv.extra.RestClient.OnClientResult;
import net.harimurti.tv.extra.TLSSocketFactory;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {
private RestClient client;
private View layoutStatus, layoutSpin, layoutText;
private TextView tvStatus, tvRetry;

private StringRequest request;
private RequestQueue volley;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentActivity fa = this;

// define some view
TabLayout tabLayout = findViewById(R.id.tab_layout);
Expand All @@ -38,71 +51,78 @@ protected void onCreate(Bundle savedInstanceState) {
tvStatus = findViewById(R.id.text_status);
tvRetry = findViewById(R.id.text_retry);

// create client & set listener -> getchannels
client = new RestClient(this);
client.setOnClientResult(new OnClientResult() {
@Override
public void onFailure(String status) {
runOnUiThread(() -> {
ShowLayoutMessage(View.VISIBLE, true);
tvStatus.setText(status);

// try fetching later
RetryGetChannels();
});
}
request = new StringRequest(Request.Method.GET,
getString(R.string.json_playlist),
response -> {
try {
Playlist playlist = new Gson().fromJson(response, Playlist.class);
viewPager.setAdapter(new ViewPagerAdapter(this, playlist));
new TabLayoutMediator(
tabLayout, viewPager, (tab, i) -> tab.setText(playlist.categories.get(i).name)
).attach();
ShowLayoutMessage(View.GONE, false);
} catch (JsonSyntaxException error) {
ShowErrorMessage(error.getMessage(), false);
}
},
error -> ShowErrorMessage(error.getMessage(), true));

@Override
public void onProgress(boolean status) {
runOnUiThread(() -> {
ShowLayoutMessage(status ? View.VISIBLE : View.GONE, false);
});
BaseHttpStack stack = new HurlStack();
if (Build.VERSION.SDK_INT == VERSION_CODES.KITKAT) {
try {
stack = new HurlStack(null, new TLSSocketFactory());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
Log.e("Volley", "Could not create new stack for TLS v1.2");
}
}
volley = Volley.newRequestQueue(this, stack);
volley.add(request);
}

@Override
public void onSuccess(Playlist playlist) {
runOnUiThread(() -> {
viewPager.setAdapter(new ViewPagerAdapter(fa, playlist));
new TabLayoutMediator(
tabLayout, viewPager, (tab, i) -> tab.setText(playlist.categories.get(i).name)
).attach();
});
}
});
client.GetChannels();
private void ShowLayoutMessage(int visibility, boolean isMessage) {
layoutStatus.setVisibility(visibility);
if (!isMessage) {
layoutSpin.setVisibility(View.VISIBLE);
layoutText.setVisibility(View.GONE);
} else {
layoutSpin.setVisibility(View.GONE);
layoutText.setVisibility(View.VISIBLE);
}
}

private void RetryGetChannels() {
private void ShowErrorMessage(String error, boolean retry) {
tvStatus.setText(error);
tvRetry.setText(R.string.text_auto_retry);
ShowLayoutMessage(View.VISIBLE, true);

if (!retry) return;

Network network = new Network(this);
new AsyncSleep(this).task(new AsyncSleep.Task() {
@Override
public void onCountDown(int left) {
if (!network.IsConnected())
tvStatus.setText(getString(R.string.no_network));

tvRetry.setText(String.format(getString(R.string.retry_time), left));
if (!network.IsConnected()) {
tvStatus.setText(R.string.no_network);
}
if (left == 0) {
tvRetry.setText(R.string.text_auto_retry_now);
}
else {
tvRetry.setText(String.format(getString(R.string.text_auto_retry_second), left));
}
}
@Override
public void onFinish() {
if (network.IsConnected())
client.GetChannels();
else
RetryGetChannels();
if (network.IsConnected()) {
volley.add(request);
}
else {
ShowErrorMessage(getString(R.string.no_network), true);
}
}
}).start(5);
}

private void ShowLayoutMessage(int visibility, boolean isMessage) {
layoutStatus.setVisibility(visibility);
if (!isMessage) {
layoutSpin.setVisibility(View.VISIBLE);
layoutText.setVisibility(View.GONE);
} else {
layoutSpin.setVisibility(View.GONE);
layoutText.setVisibility(View.VISIBLE);
}
}

@Override
public void onBackPressed() {
//super.onBackPressed();
Expand Down
25 changes: 15 additions & 10 deletions app/src/main/java/net/harimurti/tv/PlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
Expand Down Expand Up @@ -32,7 +31,6 @@ public class PlayerActivity extends AppCompatActivity {
private View layoutStatus, layoutSpin, layoutText;
private TextView tvStatus, tvRetry;

@SuppressLint("all")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -81,7 +79,7 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED) {
ShowLayoutMessage(View.VISIBLE, true);
tvStatus.setText(R.string.source_offline);
tvRetry.setText(R.string.retry);
tvRetry.setText(R.string.text_auto_retry);
RetryPlaying();
}
}
Expand All @@ -91,7 +89,7 @@ public void onPlayerError(ExoPlaybackException error) {
ShowLayoutMessage(View.VISIBLE, true);
if (error.type == ExoPlaybackException.TYPE_SOURCE) {
tvStatus.setText(R.string.source_offline);
tvRetry.setText(R.string.retry);
tvRetry.setText(R.string.text_auto_retry);
tvRetry.setVisibility(View.VISIBLE);
RetryPlaying();
} else {
Expand Down Expand Up @@ -128,17 +126,24 @@ private void RetryPlaying() {
new AsyncSleep(this).task(new AsyncSleep.Task() {
@Override
public void onCountDown(int left) {
if (!network.IsConnected())
tvStatus.setText(getString(R.string.no_network));

tvRetry.setText(String.format(getString(R.string.retry_time), left));
if (!network.IsConnected()) {
tvStatus.setText(R.string.no_network);
}
if (left == 0) {
tvRetry.setText(R.string.text_auto_retry_now);
}
else {
tvRetry.setText(String.format(getString(R.string.text_auto_retry_second), left));
}
}
@Override
public void onFinish() {
if (network.IsConnected())
if (network.IsConnected()) {
player.prepare(mediaSource);
else
}
else {
RetryPlaying();
}
}
}).start(5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

import java.util.ArrayList;

@SuppressWarnings("all")
public class ViewPagerAdapter extends FragmentStateAdapter {
private static ArrayList<Category> categories;
private static ArrayList<Channel> channels;
private ArrayList<Category> categories;
private ArrayList<Channel> channels;

public ViewPagerAdapter(FragmentActivity activity, Playlist playlist) {
super(activity);
this.categories = playlist.categories;
this.channels = playlist.channels;
}

@SuppressWarnings("all")
@Override
public Fragment createFragment(int position) {
ArrayList<Channel> contents = new ArrayList<>();
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/net/harimurti/tv/extra/AsyncSleep.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.os.Handler;

@SuppressWarnings("all")
public class AsyncSleep {
private Task task = null;
private Context context;
Expand All @@ -28,10 +27,12 @@ public void start(int second) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (left == 0)
runOnUiThread(()->task.onFinish());
else
runOnUiThread(()->task.onCountDown(left));
runOnUiThread(()-> {
task.onCountDown(left);
if (left == 0) {
task.onFinish();
}
});
}
}, i * 1000);
}
Expand Down
73 changes: 0 additions & 73 deletions app/src/main/java/net/harimurti/tv/extra/RestClient.java

This file was deleted.

Loading

0 comments on commit dec4232

Please sign in to comment.