Settings.java 8.31 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.musicpd;

22 23
import java.util.LinkedList;

24
import android.Manifest;
25
import android.app.Activity;
26 27 28
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
29
import android.content.pm.PackageManager;
30 31 32 33
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
34 35
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
36 37
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
38
import android.widget.ListView;
39 40 41 42 43 44
import android.widget.TextView;
import android.widget.ToggleButton;

public class Settings extends Activity {
	private static final String TAG = "Settings";
	private Main.Client mClient;
45 46 47 48 49 50 51 52
	private TextView mTextStatus;
	private ToggleButton mRunButton;
	private boolean mFirstRun;
	private LinkedList<String> mLogListArray = new LinkedList<String>();
	private ListView mLogListView;
	private ArrayAdapter<String> mLogListAdapter;

	private static final int MAX_LOGS = 500;
53 54 55 56

	private static final int MSG_ERROR = 0;
	private static final int MSG_STOPPED = 1;
	private static final int MSG_STARTED = 2;
57
	private static final int MSG_LOG = 3;
58

59 60 61 62 63 64 65 66 67 68
	public static class Preferences {
		public static final String KEY_RUN_ON_BOOT ="run_on_boot";
		public static final String KEY_WAKELOCK ="wakelock";

		public static SharedPreferences get(Context context) {
			return context.getSharedPreferences(TAG, MODE_PRIVATE);
		}

		public static void putBoolean(Context context, String key, boolean value) {
			final SharedPreferences prefs = get(context);
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
			if (prefs == null)
				return;
			final Editor editor = prefs.edit();
			editor.putBoolean(key, value);
			editor.apply();
		}

		public static boolean getBoolean(Context context, String key, boolean defValue) {
			final SharedPreferences prefs = get(context);

			return prefs != null ? prefs.getBoolean(key, defValue) : defValue;
		}
	}

	private Handler mHandler = new Handler(new Handler.Callback() {
85 86 87 88 89
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_ERROR:
				Log.d(TAG, "onError");
90 91 92 93 94 95 96 97 98

				mClient.release();
				connectClient();

				mRunButton.setEnabled(false);
				mRunButton.setChecked(false);

				mTextStatus.setText((String)msg.obj);
				mFirstRun = true;
99 100 101
				break;
			case MSG_STOPPED:
				Log.d(TAG, "onStopped");
102 103 104 105 106 107
				mRunButton.setEnabled(true);
				if (!mFirstRun && Preferences.getBoolean(Settings.this, Preferences.KEY_RUN_ON_BOOT, false))
					mRunButton.setChecked(true);
				else
					mRunButton.setChecked(false);
				mFirstRun = true;
108 109 110
				break;
			case MSG_STARTED:
				Log.d(TAG, "onStarted");
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 137 138 139 140
				mRunButton.setChecked(true);
				mFirstRun = true;
				mTextStatus.setText("CAUTION: this version is EXPERIMENTAL!"); // XXX
				break;
			case MSG_LOG:
				if (mLogListArray.size() > MAX_LOGS)
					mLogListArray.remove(0);
				String priority;
				switch (msg.arg1) {
				case Log.DEBUG:
					priority = "D";
					break;
				case Log.ERROR:
					priority = "E";
					break;
				case Log.INFO:
					priority = "I";
					break;
				case Log.VERBOSE:
					priority = "V";
					break;
				case Log.WARN:
					priority = "W";
					break;
				default:
					priority = "";
				}
				mLogListArray.add(priority + "/ " + (String)msg.obj);
				mLogListAdapter.notifyDataSetChanged();

141 142 143 144 145 146
				break;
			}
			return true;
		}
	});

147
	private final OnCheckedChangeListener mOnRunChangeListener = new OnCheckedChangeListener() {
148 149 150
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (mClient != null) {
151
				if (isChecked) {
152
					mClient.start();
153 154 155 156
					if (Preferences.getBoolean(Settings.this,
							Preferences.KEY_WAKELOCK, false))
						mClient.setWakelockEnabled(true);
				} else {
157
					mClient.stop();
158
				}
159 160 161 162
			}
		}
	};

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	private final OnCheckedChangeListener mOnRunOnBootChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			Preferences.putBoolean(Settings.this, Preferences.KEY_RUN_ON_BOOT, isChecked);
			if (isChecked && mClient != null && !mRunButton.isChecked())
				mRunButton.setChecked(true);
		}
	};

	private final OnCheckedChangeListener mOnWakelockChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			Preferences.putBoolean(Settings.this, Preferences.KEY_WAKELOCK, isChecked);
			if (mClient != null && mClient.isRunning())
				mClient.setWakelockEnabled(isChecked);
		}
	};

181 182
	@Override
	protected void onCreate(Bundle savedInstanceState) {
183 184 185 186 187 188 189 190
		/* TODO: this sure is the wrong place to request
		   permissions - it will cause MPD to quit
		   immediately; we should request permissions when we
		   need them, but implementing that is complicated, so
		   for now, we do it here to give users a quick
		   solution for the problem */
		requestAllPermissions();

191 192 193 194 195
		setContentView(R.layout.settings);
		mRunButton = (ToggleButton) findViewById(R.id.run);
		mRunButton.setOnCheckedChangeListener(mOnRunChangeListener);

		mTextStatus = (TextView) findViewById(R.id.status);
196

197
		mLogListAdapter = new ArrayAdapter<String>(this, R.layout.log_item, mLogListArray);
198

199 200 201
		mLogListView = (ListView) findViewById(R.id.log_list);
		mLogListView.setAdapter(mLogListAdapter);
		mLogListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
202

203 204 205 206 207 208 209 210 211
		CheckBox checkbox = (CheckBox) findViewById(R.id.run_on_boot);
		checkbox.setOnCheckedChangeListener(mOnRunOnBootChangeListener);
		if (Preferences.getBoolean(this, Preferences.KEY_RUN_ON_BOOT, false))
			checkbox.setChecked(true);

		checkbox = (CheckBox) findViewById(R.id.wakelock);
		checkbox.setOnCheckedChangeListener(mOnWakelockChangeListener);
		if (Preferences.getBoolean(this, Preferences.KEY_WAKELOCK, false))
			checkbox.setChecked(true);
212 213 214 215

		super.onCreate(savedInstanceState);
	}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	private void checkRequestPermission(String permission) {
		if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
			return;

		try {
			this.requestPermissions(new String[]{permission}, 0);
		} catch (Exception e) {
			Log.e(TAG, "requestPermissions(" + permission + ") failed",
			      e);
		}
	}

	private void requestAllPermissions() {
		if (android.os.Build.VERSION.SDK_INT < 23)
			/* we don't need to request permissions on
			   this old Android version */
			return;

		/* starting with Android 6.0, we need to explicitly
		   request all permissions before using them;
		   mentioning them in the manifest is not enough */

		checkRequestPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
	}

241
	private void connectClient() {
242
		mClient = new Main.Client(this, new Main.Client.Callback() {
243 244 245 246 247 248 249 250

			private void removeMessages() {
				/* don't remove log messages */
				mHandler.removeMessages(MSG_STOPPED);
				mHandler.removeMessages(MSG_STARTED);
				mHandler.removeMessages(MSG_ERROR);
			}

251 252
			@Override
			public void onStopped() {
253
				removeMessages();
254 255 256 257 258
				mHandler.sendEmptyMessage(MSG_STOPPED);
			}

			@Override
			public void onStarted() {
259
				removeMessages();
260 261 262 263 264
				mHandler.sendEmptyMessage(MSG_STARTED);
			}

			@Override
			public void onError(String error) {
265
				removeMessages();
266 267 268 269 270
				mHandler.sendMessage(Message.obtain(mHandler, MSG_ERROR, error));
			}

			@Override
			public void onLog(int priority, String msg) {
271
				mHandler.sendMessage(Message.obtain(mHandler, MSG_LOG, priority, 0, msg));
272 273
			}
		});
274 275 276 277 278 279
	}

	@Override
	protected void onStart() {
		mFirstRun = false;
		connectClient();
280 281 282 283 284 285 286 287 288 289
		super.onStart();
	}

	@Override
	protected void onStop() {
		mClient.release();
		mClient = null;
		super.onStop();
	}
}