Added option to auto-enable wifi on start.

This commit is contained in:
Felix Ableitner 2014-05-01 21:49:03 +02:00
parent e90039e7e8
commit c4317782e4
7 changed files with 84 additions and 41 deletions

View file

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/dont_show_again"
android:text="@string/dont_show_dialog_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

8
res/values/arrays.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="enable_wifi_values">
<item>yes</item>
<item>no</item>
<item>ask</item>
</array>
</resources>

View file

@ -27,9 +27,8 @@
<string name="exit_renderer">Do you really want to exit the renderer? <string name="exit_renderer">Do you really want to exit the renderer?
Playback will be stopped.</string> Playback will be stopped.</string>
<string name="select_route">Please select a route</string> <string name="select_route">Please select a route</string>
<string name="warning_wifi_not_connected">Wifi needs to be connected for playback</string> <string name="enable_wifi_dialog">Wifi needs to be connected for playback.
<string name="dont_show_dialog_again">"Do not show this dialog again"</string> Do you want to enable it now?</string>
<string name="warning">Warning</string>
<!-- Image alt text --> <!-- Image alt text -->
<string name="album_art">Album Art</string> <string name="album_art">Album Art</string>
@ -43,6 +42,16 @@
<!-- Title for the SettingsActivity --> <!-- Title for the SettingsActivity -->
<string name="settings_title">Preferences</string> <string name="settings_title">Preferences</string>
<!-- Title for preference to enable wifi on start -->
<string name="enable_wifi_title">Automatically enable Wifi on start</string>
<!-- Values for preference to auto-enable wifi on start -->
<string-array name="enable_wifi_value_titles">
<item>yes</item>
<item>no</item>
<item>ask</item>
</string-array>
<!-- Title for the pause playback on call preference --> <!-- Title for the pause playback on call preference -->
<string name="incoming_phone_call_pause">Pause playback on incoming phone call</string> <string name="incoming_phone_call_pause">Pause playback on incoming phone call</string>

View file

@ -1,6 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="enable_wifi_on_start"
android:title="@string/enable_wifi_title"
android:entries="@array/enable_wifi_value_titles"
android:entryValues="@array/enable_wifi_values"
android:defaultValue="ask"/>
<CheckBoxPreference <CheckBoxPreference
android:key="incoming_phone_call_pause" android:key="incoming_phone_call_pause"
android:title="@string/incoming_phone_call_pause" android:title="@string/incoming_phone_call_pause"

View file

@ -32,11 +32,15 @@ import java.util.List;
import org.teleal.cling.support.model.item.Item; import org.teleal.cling.support.model.item.Item;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.app.FragmentStatePagerAdapter;
@ -139,29 +143,28 @@ public class MainActivity extends ActionBarActivity {
.setText(R.string.title_route) .setText(R.string.title_route)
.setTabListener(tabListener)); .setTabListener(tabListener));
ConnectivityManager connManager = (ConnectivityManager) final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
getSystemService(CONNECTIVITY_SERVICE); if (!wifi.isWifiEnabled()){
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); String value = PreferenceManager.getDefaultSharedPreferences(this)
final SharedPreferences prefs = getSharedPreferences("preferences.db", 0); .getString(PreferencesActivity.KEY_ENABLE_WIFI_ON_START, "ask");
if (value.equals("yes")) {
if (!wifi.isConnected() && !prefs.getBoolean("wifi_skip_dialog", false)) { wifi.setWifiEnabled(true);
View checkBoxView = View.inflate(this, R.layout.dialog_wifi_disabled, null);
CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.dont_show_again);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("wifi_skip_dialog", isChecked)
.commit();
} }
}); else if (value.equals("ask")) {
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setView(checkBoxView) .setMessage(R.string.enable_wifi_dialog)
.setTitle(R.string.warning_wifi_not_connected) .setPositiveButton(android.R.string.yes,
.setPositiveButton(android.R.string.ok, null) new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
wifi.setWifiEnabled(true);
}
})
.setNegativeButton(android.R.string.no, null)
.show(); .show();
} }
}
if (savedInstanceState != null) { if (savedInstanceState != null) {
FragmentManager fm = getSupportFragmentManager(); FragmentManager fm = getSupportFragmentManager();

View file

@ -28,24 +28,33 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.github.nutomic.controldlna.gui; package com.github.nutomic.controldlna.gui;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
import android.view.MenuItem; import android.view.MenuItem;
import com.github.nutomic.controldlna.R; import com.github.nutomic.controldlna.R;
public class PreferencesActivity extends PreferenceActivity { public class PreferencesActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String KEY_ENABLE_WIFI_ON_START = "enable_wifi_on_start";
public static final String KEY_INCOMING_PHONE_CALL_PAUSE = "incoming_phone_call_pause"; public static final String KEY_INCOMING_PHONE_CALL_PAUSE = "incoming_phone_call_pause";
private static final String KEY_CONTACT_DEV = "contact_dev"; private static final String KEY_CONTACT_DEV = "contact_dev";
private ListPreference mEnableWifiOnStart;
private Preference mContactDev; private Preference mContactDev;
/**
* Initializes preferences from xml.
*/
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -56,11 +65,18 @@ public class PreferencesActivity extends PreferenceActivity {
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
} }
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
final PreferenceScreen screen = getPreferenceScreen(); final PreferenceScreen screen = getPreferenceScreen();
mEnableWifiOnStart = (ListPreference) screen.findPreference(KEY_ENABLE_WIFI_ON_START);
mEnableWifiOnStart.setSummary(mEnableWifiOnStart.getEntry());
mContactDev = screen.findPreference(KEY_CONTACT_DEV); mContactDev = screen.findPreference(KEY_CONTACT_DEV);
} }
/**
* Navigates up from activity on ActionBar back click.
*/
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -72,6 +88,9 @@ public class PreferencesActivity extends PreferenceActivity {
} }
} }
/**
* Sends mail intent on contact dev preference click.
*/
@Override @Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) { Preference preference) {
@ -84,4 +103,13 @@ public class PreferencesActivity extends PreferenceActivity {
return super.onPreferenceTreeClick(preferenceScreen, preference); return super.onPreferenceTreeClick(preferenceScreen, preference);
} }
/**
* Updates summary of list preference (from current item).
*/
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(KEY_ENABLE_WIFI_ON_START)) {
mEnableWifiOnStart.setSummary(mEnableWifiOnStart.getEntry());
}
}
} }

View file

@ -39,8 +39,9 @@ public class RouteAdapter extends ArrayAdapter<RouteInfo> {
* Replacement for addAll, which is not implemented on lower API levels. * Replacement for addAll, which is not implemented on lower API levels.
*/ */
public void add(List<RouteInfo> routes) { public void add(List<RouteInfo> routes) {
for (RouteInfo r : routes) for (RouteInfo r : routes) {
add(r); add(r);
} }
}
} }