Skip to content

Commit

Permalink
Auto-save settings of: subtitles toggle, ip, port, password
Browse files Browse the repository at this point in the history
  • Loading branch information
elig0n committed Jul 22, 2021
1 parent 8a3f768 commit 044126e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
Expand Down Expand Up @@ -39,6 +40,11 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BackgroundToggleButton subtitles2 = (miccah.mpvremote.BackgroundToggleButton)
findViewById(R.id.subtitles2);
subtitles2.setChecked(PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean("SUBTITLES", true));

/* Setup side menu */
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Settings.mDrawerList = (ListView) findViewById(R.id.left_drawer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.view.ViewGroup;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -35,42 +36,51 @@ public SettingsAdapter(MainActivity a) {
mInflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

String ipAddress = null;
try {
WifiManager wifiManager = (WifiManager)
activity.getSystemService(Activity.WIFI_SERVICE);
ipAddress = Formatter.formatIpAddress(
wifiManager.getConnectionInfo().getIpAddress());
} catch (Exception e) {ipAddress = "Error";}

/* Default Settings */
// ipAddress first 3 octets of IPv4 address
Settings.ipAddress = ipAddress.substring(0, ipAddress.lastIndexOf('.')+1);
Settings.port = new Integer(28899);
Settings.passwd = "";
String prefIP = PreferenceManager.getDefaultSharedPreferences(activity)
.getString("IP", "");
if (prefIP != "") {
Settings.ipAddress = prefIP;
} else {
String ipAddress = null;
try {
WifiManager wifiManager = (WifiManager)
activity.getSystemService(Activity.WIFI_SERVICE);
ipAddress = Formatter.formatIpAddress(
wifiManager.getConnectionInfo().getIpAddress());
} catch (Exception e) {ipAddress = "Error";}

// ipAddress first 3 octets of IPv4 address
Settings.ipAddress = ipAddress.substring(0, ipAddress.lastIndexOf('.')+1);

/* Search through subnet to see if the default port is running a server */
for (int i = 1; i < 255; i++) {
final String ipAddr = Settings.ipAddress + i;
new UDPPacket(ipAddr, Settings.port, Settings.passwd, new Callback() {
public void callback(boolean result, JSONObject obj) {
String addr = Settings.ipAddress;
boolean notFound = (addr.length() > 0) ? addr.charAt(addr.length() - 1) == '.' : false;
if (result && notFound) {
Settings.ipAddress = ipAddr;
myItems.get(1).caption = Settings.ipAddress;
notifyDataSetChanged();
Toast.makeText(activity, "Found server",
Toast.LENGTH_SHORT).show();
}
}
}, false, 10).execute(null, "health");
}
}

Settings.port = PreferenceManager.getDefaultSharedPreferences(activity).
getInt("PORT", 28899);
Settings.passwd = PreferenceManager.getDefaultSharedPreferences(activity).
getString("PASSWORD", "");
Settings.audio = 1;
Settings.subtitle = 1;
Settings.audio_tracks = new ArrayList<String>(); Settings.audio_tracks.add("1");
Settings.subtitle_tracks = new ArrayList<String>(); Settings.subtitle_tracks.add("1");

/* Search through subnet to see if the default port is running a server */
for (int i = 1; i < 255; i++) {
final String ipAddr = Settings.ipAddress + i;
new UDPPacket(ipAddr, Settings.port, Settings.passwd, new Callback() {
public void callback(boolean result, JSONObject obj) {
String addr = Settings.ipAddress;
boolean notFound = addr.charAt(addr.length() - 1) == '.';
if (result && notFound) {
Settings.ipAddress = ipAddr;
myItems.get(1).caption = Settings.ipAddress;
notifyDataSetChanged();
Toast.makeText(activity, "Found server",
Toast.LENGTH_SHORT).show();
}
}
}, false, 10).execute(null, "health");
}

/* Initialize list (Text, Hint, Focusable, InputType) */
myItems.add( new ListItem(ListItem.TYPE.TEXT_VIEW, null, "Settings", InputType.TYPE_NULL) );
myItems.add( new ListItem(ListItem.TYPE.EDIT_TEXT, Settings.ipAddress, "IP Address", InputType.TYPE_CLASS_TEXT) );
Expand Down Expand Up @@ -244,14 +254,17 @@ private void setInput(TextView view) {
if (hint.equals(myItems.get(1).hint)) {
// IP Address
Settings.ipAddress = value;
PreferenceManager.getDefaultSharedPreferences(activity).edit().putString("IP", value).apply();
}
else if (hint.equals(myItems.get(2).hint)) {
// Port
Settings.port = Integer.parseInt(value);
Settings.port = value.matches("\\d+") ? Integer.parseInt(value) : 0;
PreferenceManager.getDefaultSharedPreferences(activity).edit().putInt("PORT", Settings.port).apply();
}
else if (hint.equals(myItems.get(4).hint)) {
// Password
Settings.passwd = value;
PreferenceManager.getDefaultSharedPreferences(activity).edit().putString("PASSWORD", value).apply();
}
}

Expand Down

0 comments on commit 044126e

Please sign in to comment.