Cleanups and beautifies
parent
e4720c077c
commit
589750a4f7
|
@ -2,7 +2,6 @@ package max.music_cyclon;
|
|||
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
|
@ -37,7 +36,7 @@ public class RenameDialogFragment extends DialogFragment {
|
|||
newName = new EditText(getActivity());
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
|
||||
.setTitle("Rename")
|
||||
.setTitle(getActivity().getString(R.string.rename))
|
||||
.setView(newName)
|
||||
.setPositiveButton(android.R.string.ok, new ApplyRename())
|
||||
.setNegativeButton(android.R.string.cancel, STUB_CLICK);
|
||||
|
|
|
@ -61,6 +61,8 @@ import max.music_cyclon.service.LibraryService;
|
|||
*/
|
||||
public class SynchronizeActivity extends AppCompatActivity {
|
||||
|
||||
public static final String DEFAULT_CONFIG_PATH = "configs.json";
|
||||
|
||||
private PagerAdapter pagerAdapter;
|
||||
|
||||
/**
|
||||
|
@ -80,7 +82,9 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
/**
|
||||
* Target we publish for clients to send messages to IncomingHandler.
|
||||
*/
|
||||
private final Messenger mMessenger = new Messenger(new IncomingHandler(new WeakReference<>(this)));
|
||||
private final Messenger mMessenger = new Messenger(
|
||||
new IncomingHandler(new WeakReference<>(this))
|
||||
);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -92,7 +96,7 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
|
||||
List<SynchronizeConfig> configs = Collections.emptyList();
|
||||
try {
|
||||
FileInputStream in = openFileInput("configs.json");
|
||||
FileInputStream in = openFileInput(DEFAULT_CONFIG_PATH);
|
||||
configs = SynchronizeConfig.load(in);
|
||||
in.close();
|
||||
} catch (IOException | JSONException e) {
|
||||
|
@ -102,7 +106,7 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
pagerAdapter = new PagerAdapter(configs, getSupportFragmentManager());
|
||||
|
||||
if (pagerAdapter.getCount() == 0) {
|
||||
pagerAdapter.add("Default");
|
||||
pagerAdapter.add(getString(R.string.default_config_name));
|
||||
}
|
||||
|
||||
final ViewPager pager = (ViewPager) findViewById(R.id.container);
|
||||
|
@ -176,7 +180,7 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
unbindLibraryService();
|
||||
|
||||
try {
|
||||
FileOutputStream fos = openFileOutput("configs.json", Context.MODE_PRIVATE);
|
||||
FileOutputStream fos = openFileOutput(DEFAULT_CONFIG_PATH, Context.MODE_PRIVATE);
|
||||
getPagerAdapter().save(fos);
|
||||
fos.close();
|
||||
} catch (IOException | JSONException e) {
|
||||
|
@ -217,7 +221,7 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
break;
|
||||
case R.id.action_sync:
|
||||
if (isServiceRunning(LibraryService.class)) {
|
||||
Toast.makeText(this, "Already synchronizing!", Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(this, R.string.already_synchronizing, Toast.LENGTH_LONG).show();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -227,15 +231,18 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
|
||||
// Show sync control dialog
|
||||
syncProgress = new ProgressDialog(SynchronizeActivity.this);
|
||||
syncProgress.setMessage("Synchronizing");
|
||||
syncProgress.setMessage(getString(R.string.synchronizing));
|
||||
syncProgress.setCancelable(false);
|
||||
syncProgress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
syncProgress.show();
|
||||
// syncProgress.setButton(
|
||||
// DialogInterface.BUTTON_NEGATIVE,
|
||||
// getString(android.R.string.cancel),
|
||||
// new DialogInterface.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(DialogInterface dialog, int which) {
|
||||
// dialog.dismiss();
|
||||
// }
|
||||
// });
|
||||
// syncProgress.show();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
|
@ -257,16 +264,21 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
SynchronizeActivity activity = this.activity.get();
|
||||
if (activity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (msg.what) {
|
||||
case LibraryService.MSG_FINISHED:
|
||||
SynchronizeActivity activity = this.activity.get();
|
||||
if (activity != null) {
|
||||
Dialog dialog = activity.getSyncProgress();
|
||||
if (dialog != null) {
|
||||
dialog.dismiss();
|
||||
activity.clearSyncProgress();
|
||||
}
|
||||
activity.unbindLibraryService();
|
||||
|
||||
Dialog dialog = activity.getSyncProgress();
|
||||
if (dialog != null) {
|
||||
dialog.dismiss();
|
||||
activity.clearSyncProgress();
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
super.handleMessage(msg);
|
||||
|
@ -301,17 +313,30 @@ public class SynchronizeActivity extends AppCompatActivity {
|
|||
private void startLibraryService() {
|
||||
Intent intent = new Intent(SynchronizeActivity.this, LibraryService.class);
|
||||
List<SynchronizeConfig> configs = getPagerAdapter().getConfigs();
|
||||
intent.putExtra("configs", configs.toArray(new SynchronizeConfig[configs.size()]));
|
||||
|
||||
// Update last updated
|
||||
for (SynchronizeConfig config : configs) {
|
||||
config.updateLastUpdated(); // fixme the result of LibraryService does not affect this (for example: Remote not available)
|
||||
}
|
||||
|
||||
intent.putExtra(
|
||||
LibraryService.ARGUMENT_CONFIGS,
|
||||
configs.toArray(new SynchronizeConfig[configs.size()])
|
||||
);
|
||||
startService(intent);
|
||||
}
|
||||
|
||||
private boolean isServiceRunning(Class<?> serviceClass) {
|
||||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
List<ActivityManager.RunningServiceInfo> services =
|
||||
manager.getRunningServices(Integer.MAX_VALUE);
|
||||
|
||||
for (ActivityManager.RunningServiceInfo service : services) {
|
||||
if (serviceClass.getName().equals(service.service.getClassName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,16 @@ import java.util.Scanner;
|
|||
*/
|
||||
public class SynchronizeConfig implements Parcelable {
|
||||
|
||||
public static final Random RANDOM = new Random();
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
private static final String ID_KEY = "id";
|
||||
private static final String SIZE_KEY = "size";
|
||||
private static final String RANDOM_KEY = "random";
|
||||
private static final String ALBUM_KEY = "use_albums";
|
||||
private static final String QUERY_KEY = "query";
|
||||
private static final String START_CHARGING_KEY = "start_charging";
|
||||
private static final String DOWNLOAD_INTERVAL_KEY = "download_interval";
|
||||
|
||||
/**
|
||||
* The name of this config
|
||||
*/
|
||||
|
@ -40,7 +49,7 @@ public class SynchronizeConfig implements Parcelable {
|
|||
public SynchronizeConfig(String name, long id) {
|
||||
this(name, new JSONObject());
|
||||
try {
|
||||
json.put("id", id);
|
||||
json.put(ID_KEY, id);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException("Failed setting id!");
|
||||
}
|
||||
|
@ -52,7 +61,7 @@ public class SynchronizeConfig implements Parcelable {
|
|||
|
||||
public long getID() {
|
||||
try {
|
||||
return json.getLong("id");
|
||||
return json.getLong(ID_KEY);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException("Config has no id!");
|
||||
}
|
||||
|
@ -66,28 +75,41 @@ public class SynchronizeConfig implements Parcelable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public long getLastUpdated() {
|
||||
return json.optLong("last_update", 0);
|
||||
}
|
||||
|
||||
public void updateLastUpdated() {
|
||||
try {
|
||||
json.put("last_update", System.currentTimeMillis());
|
||||
} catch (JSONException ignored) {
|
||||
throw new RuntimeException("Failed to set last_update!");
|
||||
}
|
||||
}
|
||||
|
||||
public int getSize(Resources resources) {
|
||||
return json.optInt("size", resources.getInteger(R.integer.size));
|
||||
return json.optInt(SIZE_KEY, resources.getInteger(R.integer.size));
|
||||
}
|
||||
|
||||
public boolean isRandom(Resources resources) {
|
||||
return json.optBoolean("size", resources.getBoolean(R.bool.random));
|
||||
return json.optBoolean(RANDOM_KEY, resources.getBoolean(R.bool.random));
|
||||
}
|
||||
|
||||
public boolean isAlbum(Resources resources) {
|
||||
return json.optBoolean("use_albums", resources.getBoolean(R.bool.use_albums));
|
||||
return json.optBoolean(ALBUM_KEY, resources.getBoolean(R.bool.use_albums));
|
||||
}
|
||||
|
||||
public String getQuery(Resources resources) {
|
||||
return json.optString("query", resources.getString(R.string.query));
|
||||
return json.optString(QUERY_KEY, resources.getString(R.string.query));
|
||||
}
|
||||
|
||||
public boolean isStartCharging(Resources resources) {
|
||||
return json.optBoolean("start_charging", resources.getBoolean(R.bool.start_charging));
|
||||
return json.optBoolean(START_CHARGING_KEY, resources.getBoolean(R.bool.start_charging));
|
||||
}
|
||||
|
||||
public int getDownloadInterval(Resources resources) {
|
||||
return json.optInt("download_interval", resources.getInteger(R.integer.download_interval));
|
||||
return json.optInt(DOWNLOAD_INTERVAL_KEY,
|
||||
resources.getInteger(R.integer.download_interval));
|
||||
}
|
||||
|
||||
public JSONObject getJson() {
|
||||
|
@ -105,19 +127,7 @@ public class SynchronizeConfig implements Parcelable {
|
|||
dest.writeString(json.toString());
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<SynchronizeConfig> CREATOR = new Parcelable.Creator<SynchronizeConfig>() {
|
||||
public SynchronizeConfig createFromParcel(Parcel in) {
|
||||
try {
|
||||
return new SynchronizeConfig(in.readString(), new JSONObject(in.readString()));
|
||||
} catch (JSONException e) {
|
||||
return new SynchronizeConfig("none");
|
||||
}
|
||||
}
|
||||
|
||||
public SynchronizeConfig[] newArray(int size) {
|
||||
return new SynchronizeConfig[size];
|
||||
}
|
||||
};
|
||||
public static final Creator<SynchronizeConfig> CREATOR = new SynchronizeConfigCreator();
|
||||
|
||||
public static List<SynchronizeConfig> load(InputStream in) throws JSONException {
|
||||
String data = convertStreamToString(in);
|
||||
|
@ -160,4 +170,17 @@ public class SynchronizeConfig implements Parcelable {
|
|||
return s.hasNext() ? s.next() : "";
|
||||
}
|
||||
|
||||
private static class SynchronizeConfigCreator implements Creator<SynchronizeConfig> {
|
||||
public SynchronizeConfig createFromParcel(Parcel in) {
|
||||
try {
|
||||
return new SynchronizeConfig(in.readString(), new JSONObject(in.readString()));
|
||||
} catch (JSONException e) {
|
||||
return new SynchronizeConfig("none");
|
||||
}
|
||||
}
|
||||
|
||||
public SynchronizeConfig[] newArray(int size) {
|
||||
return new SynchronizeConfig[size];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,7 +142,7 @@ public class BeetsFetcher {
|
|||
String tag = reader.nextName();
|
||||
switch (tag) {
|
||||
case "id":
|
||||
item.setId(reader.nextInt());
|
||||
item.setID(reader.nextInt());
|
||||
break;
|
||||
case "path":
|
||||
item.setPath(reader.nextString());
|
||||
|
|
|
@ -9,7 +9,6 @@ import java.io.File;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.zip.Adler32;
|
||||
|
||||
|
@ -22,18 +21,19 @@ import okhttp3.Response;
|
|||
public class DownloadTask implements Runnable {
|
||||
|
||||
private final SynchronizeConfig config;
|
||||
private final URI uri;
|
||||
private final String url;
|
||||
private final String itemPath;
|
||||
|
||||
private final FileTracker tracker;
|
||||
private final ProgressUpdater progressUpdater;
|
||||
private CountDownLatch itemsLeftLatch;
|
||||
public static final OkHttpClient CLIENT = new OkHttpClient();
|
||||
|
||||
|
||||
public DownloadTask(SynchronizeConfig config, URI uri, String itemPath,
|
||||
public DownloadTask(SynchronizeConfig config, String url, String itemPath,
|
||||
FileTracker tracker, ProgressUpdater progressUpdater) {
|
||||
this.config = config;
|
||||
this.uri = uri;
|
||||
this.url = url;
|
||||
this.itemPath = itemPath;
|
||||
|
||||
this.tracker = tracker;
|
||||
|
@ -41,13 +41,11 @@ public class DownloadTask implements Runnable {
|
|||
}
|
||||
|
||||
private InputStream prepareConnection() throws IOException {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(uri.toURL())
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
Response response = CLIENT.newCall(request).execute();
|
||||
|
||||
if (response.code() != 200) {
|
||||
Log.e("ERROR", "Server returned HTTP " + response.message());
|
||||
|
|
|
@ -5,11 +5,11 @@ public class Item {
|
|||
private int id;
|
||||
private String path;
|
||||
|
||||
public int getId() {
|
||||
public int getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
public void setID(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,15 +26,28 @@ public class ProgressUpdater {
|
|||
this.notificationManager = NotificationManagerCompat.from(context);
|
||||
}
|
||||
|
||||
public void showMessage(String message, boolean persist, Object... args) {
|
||||
showMessage(String.format(message, args), persist);
|
||||
public void showMessage(String message, Object... args) {
|
||||
showMessage(String.format(message, args));
|
||||
}
|
||||
|
||||
public void showMessage(String message, boolean persist) {
|
||||
public void showMessage(String message) {
|
||||
NotificationCompat.Builder builder = notificationBuilder();
|
||||
builder.setContentTitle(message);
|
||||
builder.setContentText("");
|
||||
builder.setProgress(0, 0, !persist);
|
||||
|
||||
updateNotification(builder);
|
||||
}
|
||||
|
||||
public void showOngoingMessage(String message, Object... args) {
|
||||
showOngoingMessage(String.format(message, args));
|
||||
}
|
||||
|
||||
|
||||
public void showOngoingMessage(String message) {
|
||||
NotificationCompat.Builder builder = notificationBuilder();
|
||||
builder.setContentTitle(message);
|
||||
builder.setProgress(0, 0, true);
|
||||
builder.setOngoing(true);
|
||||
|
||||
updateNotification(builder);
|
||||
}
|
||||
|
||||
|
@ -45,6 +58,7 @@ public class ProgressUpdater {
|
|||
builder.setContentTitle("Aktualisiere Musik");
|
||||
builder.setContentText(downloadCount + "/" + maximum);
|
||||
builder.setProgress(maximum, downloadCount, false);
|
||||
builder.setOngoing(true);
|
||||
updateNotification(builder);
|
||||
}
|
||||
|
||||
|
@ -52,13 +66,13 @@ public class ProgressUpdater {
|
|||
this.maximum = maximum;
|
||||
}
|
||||
|
||||
private void updateNotification(NotificationCompat.Builder builder) {
|
||||
public void updateNotification(NotificationCompat.Builder builder) {
|
||||
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
||||
}
|
||||
|
||||
private NotificationCompat.Builder notificationBuilder() {
|
||||
public NotificationCompat.Builder notificationBuilder() {
|
||||
return new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher);
|
||||
.setSmallIcon(R.drawable.ic_sync_white_24dp);
|
||||
}
|
||||
|
||||
private NotificationCompat.Builder progressNotificationBuilder() {
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
android:id="@+id/tabs"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#FF9800" />
|
||||
android:background="@color/colorPrimary" />
|
||||
|
||||
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
|
|
|
@ -8,14 +8,14 @@
|
|||
app:showAsAction="ifRoom"/>
|
||||
|
||||
<item android:id="@+id/action_settings"
|
||||
android:title="Settings"
|
||||
android:title="@string/settings"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item android:id="@+id/action_help"
|
||||
android:title="Hilfe"
|
||||
android:title="@string/help"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item android:id="@+id/action_version"
|
||||
android:title="Version"
|
||||
android:title="@string/version"
|
||||
app:showAsAction="never"/>
|
||||
</menu>
|
|
@ -1,4 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="accentColor">#607D8B</color>
|
||||
<color name="colorPrimary">#FF9800</color>
|
||||
</resources>
|
|
@ -2,4 +2,11 @@
|
|||
<string name="app_name">music-cyclon</string>
|
||||
<string name="title_activity_synchronize">Synchronizing</string>
|
||||
<string name="synchronize">Synchronize</string>
|
||||
<string name="settings">Settings</string>
|
||||
<string name="help">Help</string>
|
||||
<string name="version">Version</string>
|
||||
<string name="synchronizing">Synchronizing</string>
|
||||
<string name="already_synchronizing">Already synchronizing!</string>
|
||||
<string name="default_config_name">Default</string>
|
||||
<string name="rename">Rename</string>
|
||||
</resources>
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<style name="AppTheme" parent="@style/PreferenceFixTheme.Light.NoActionBar">
|
||||
<!-- Main theme colors -->
|
||||
<!-- your app branding color for the app bar -->
|
||||
<item name="colorPrimary">#FF9800</item>
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<!-- darker variant for the status bar and contextual app bars -->
|
||||
<item name="colorPrimaryDark">#F57C00</item>
|
||||
|
||||
|
|
|
@ -11,12 +11,12 @@
|
|||
android:summary="The amount of tracks or albums to download"
|
||||
android:title="Number of items" />
|
||||
|
||||
<!--<SwitchPreference-->
|
||||
<!--android:persistent="false"-->
|
||||
<!--android:defaultValue="@bool/random"-->
|
||||
<!--android:key="random"-->
|
||||
<!--android:summary=""-->
|
||||
<!--android:title="Select title/albums randomly" />-->
|
||||
<SwitchPreference
|
||||
android:defaultValue="@bool/random"
|
||||
android:key="random"
|
||||
android:persistent="false"
|
||||
android:summary="Select title/albums randomly"
|
||||
android:title="Random" />
|
||||
|
||||
<SwitchPreference
|
||||
android:defaultValue="@bool/use_albums"
|
||||
|
|
Loading…
Reference in New Issue