forked from niqdev/ipcam-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpCamDefaultActivity.java
91 lines (75 loc) · 3.23 KB
/
IpCamDefaultActivity.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
package com.github.niqdev.ipcam;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.github.niqdev.mjpeg.DisplayMode;
import com.github.niqdev.mjpeg.Mjpeg;
import com.github.niqdev.mjpeg.MjpegView;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import static com.github.niqdev.ipcam.settings.SettingsActivity.PREF_AUTH_PASSWORD;
import static com.github.niqdev.ipcam.settings.SettingsActivity.PREF_AUTH_USERNAME;
import static com.github.niqdev.ipcam.settings.SettingsActivity.PREF_FLIP_HORIZONTAL;
import static com.github.niqdev.ipcam.settings.SettingsActivity.PREF_FLIP_VERTICAL;
import static com.github.niqdev.ipcam.settings.SettingsActivity.PREF_IPCAM_URL;
import static com.github.niqdev.ipcam.settings.SettingsActivity.PREF_ROTATE_DEGREES;
public class IpCamDefaultActivity extends AppCompatActivity {
private static final int TIMEOUT = 5;
@BindView(R.id.mjpegViewDefault)
MjpegView mjpegView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ipcam_default);
ButterKnife.bind(this);
}
private SharedPreferences getSharedPreferences() {
return PreferenceManager
.getDefaultSharedPreferences(this);
}
private String getPreference(String key) {
return getSharedPreferences()
.getString(key, "");
}
private Boolean getBooleanPreference(String key) {
return getSharedPreferences()
.getBoolean(key, false);
}
private DisplayMode calculateDisplayMode() {
int orientation = getResources().getConfiguration().orientation;
return orientation == Configuration.ORIENTATION_LANDSCAPE ?
DisplayMode.FULLSCREEN : DisplayMode.BEST_FIT;
}
private void loadIpCam() {
Mjpeg.newInstance()
.credential(getPreference(PREF_AUTH_USERNAME), getPreference(PREF_AUTH_PASSWORD))
.open(getPreference(PREF_IPCAM_URL), TIMEOUT)
.subscribe(
inputStream -> {
mjpegView.setSource(inputStream);
mjpegView.setDisplayMode(calculateDisplayMode());
mjpegView.flipHorizontal(getBooleanPreference(PREF_FLIP_HORIZONTAL));
mjpegView.flipVertical(getBooleanPreference(PREF_FLIP_VERTICAL));
mjpegView.setRotate(Float.parseFloat(getPreference(PREF_ROTATE_DEGREES)));
mjpegView.showFps(true);
},
throwable -> {
Log.e(getClass().getSimpleName(), "mjpeg error", throwable);
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
});
}
@Override
protected void onResume() {
super.onResume();
loadIpCam();
}
@Override
protected void onPause() {
super.onPause();
mjpegView.stopPlayback();
}
}