aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/activity/EditPlayActionActivity.java
blob: 9423eb38b0269627e7d675ad95c684598c082691 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
  This file is part of Subsonic.
	Subsonic 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 3 of the License, or
	(at your option) any later version.
	Subsonic 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 Subsonic. If not, see <http://www.gnu.org/licenses/>.
	Copyright 2014 (C) Scott Jackson
*/

package github.daneren2005.dsub.activity;

import android.app.Activity;
import androidx.appcompat.app.AlertDialog;

import android.content.Intent;
import android.os.Bundle;
import androidx.drawerlayout.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.Genre;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
import github.daneren2005.dsub.service.OfflineException;
import github.daneren2005.dsub.service.ServerTooOldException;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.LoadingTask;
import github.daneren2005.dsub.util.Util;

public class EditPlayActionActivity extends SubsonicActivity {
	private CheckBox shuffleCheckbox;
	private CheckBox startYearCheckbox;
	private EditText startYearBox;
	private CheckBox endYearCheckbox;
	private EditText endYearBox;
	private Button genreButton;
	private Spinner offlineSpinner;
	
	private String doNothing;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.tasker_start_playing_title);
		setContentView(R.layout.edit_play_action);
		final Activity context = this;
		doNothing = context.getResources().getString(R.string.tasker_edit_do_nothing);

		shuffleCheckbox = (CheckBox) findViewById(R.id.edit_shuffle_checkbox);
		shuffleCheckbox.setOnCheckedChangeListener((view, isChecked) -> {
			startYearCheckbox.setEnabled(isChecked);
			endYearCheckbox.setEnabled(isChecked);
			genreButton.setEnabled(isChecked);
		});

		startYearCheckbox = (CheckBox) findViewById(R.id.edit_start_year_checkbox);
		startYearBox = (EditText) findViewById(R.id.edit_start_year);
		// Disable/enable number box if checked
		startYearCheckbox.setOnCheckedChangeListener((view, isChecked) -> startYearBox.setEnabled(isChecked));
		
		endYearCheckbox = (CheckBox) findViewById(R.id.edit_end_year_checkbox);
		endYearBox = (EditText) findViewById(R.id.edit_end_year);
		endYearCheckbox.setOnCheckedChangeListener((view, isChecked) -> endYearBox.setEnabled(isChecked));

		genreButton = (Button) findViewById(R.id.edit_genre_spinner);
		genreButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new LoadingTask<List<Genre>>(context, true) {
					@Override
					protected List<Genre> doInBackground() throws Throwable {
						MusicService musicService = MusicServiceFactory.getMusicService(context);
						return musicService.getGenres(false, context, this);
					}

					@Override
					protected void done(final List<Genre> genres) {
						List<String> names = new ArrayList<>();
						String blank = context.getResources().getString(R.string.select_genre_blank);
						names.add(doNothing);
						names.add(blank);
						for(Genre genre: genres) {
							names.add(genre.getName());
						}
						final List<String> finalNames = names;

						AlertDialog.Builder builder = new AlertDialog.Builder(context);
						builder.setTitle(R.string.shuffle_pick_genre)
								.setItems(names.toArray(new CharSequence[0]), (dialog, which) -> {
									if(which == 1) {
										genreButton.setText("");
									} else {
										genreButton.setText(finalNames.get(which));
									}
								});
						AlertDialog dialog = builder.create();
						dialog.show();
					}

					@Override
					protected void error(Throwable error) {
						String msg;
						if (error instanceof OfflineException || error instanceof ServerTooOldException) {
							msg = getErrorMessage(error);
						} else {
							msg = context.getResources().getString(R.string.playlist_error) + " " + getErrorMessage(error);
						}

						Util.toast(context, msg, false);
					}
				}.execute();
			}
		});
		genreButton.setText(doNothing);

		offlineSpinner = (Spinner) findViewById(R.id.edit_offline_spinner);
		ArrayAdapter<CharSequence> offlineAdapter = ArrayAdapter.createFromResource(this, R.array.editServerOptions, android.R.layout.simple_spinner_item);
		offlineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		offlineSpinner.setAdapter(offlineAdapter);
		
		// Setup default for everything
		Bundle extras = getIntent().getBundleExtra(Constants.TASKER_EXTRA_BUNDLE);
		if(extras != null) {
			if(extras.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE)) {
				shuffleCheckbox.setChecked(true);
			}
			
			String startYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, null);
			if(startYear != null) {
				startYearCheckbox.setEnabled(true);
				startYearBox.setText(startYear);
			}
			String endYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, null);
			if(endYear != null) {
				endYearCheckbox.setEnabled(true);
				endYearBox.setText(endYear);
			}
			
			String genre = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, doNothing);
			if(genre != null) {
				genreButton.setText(genre);
			}
			
			int offline = extras.getInt(Constants.PREFERENCES_KEY_OFFLINE, 0);
			if(offline != 0) {
				offlineSpinner.setSelection(offline);
			}
		}

		drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.tasker_configuration, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			cancel();
			return true;
		} else if(item.getItemId() == R.id.menu_accept) {
			accept();
			return true;
		} else if(item.getItemId() == R.id.menu_cancel) {
			cancel();
			return true;
		}

		return false;
	}

	private void accept() {
		Intent intent = new Intent();

		String blurb = getResources().getString(shuffleCheckbox.isChecked() ? R.string.tasker_start_playing_shuffled : R.string.tasker_start_playing);
		intent.putExtra("com.twofortyfouram.locale.intent.extra.BLURB", blurb);

		// Get settings user specified
		Bundle data = new Bundle();
		boolean shuffle = shuffleCheckbox.isChecked();
		data.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, shuffle);
		if(shuffle) {
			if(startYearCheckbox.isChecked()) {
				data.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYearBox.getText().toString());
			}
			if(endYearCheckbox.isChecked()) {
				data.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYearBox.getText().toString());
			}
			String genre = genreButton.getText().toString();
			if(!genre.equals(doNothing)) {
				data.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre);
			}
		}
		
		int offline = offlineSpinner.getSelectedItemPosition();
		if(offline != 0) {
			data.putInt(Constants.PREFERENCES_KEY_OFFLINE, offline);
		}
		
		intent.putExtra(Constants.TASKER_EXTRA_BUNDLE, data);

		setResult(Activity.RESULT_OK, intent);
		finish();
	}
	private void cancel() {
		setResult(Activity.RESULT_CANCELED);
		finish();
	}
}