diff --git a/app/app.iml b/app/app.iml
index eaf5b40..f70e089 100644
--- a/app/app.iml
+++ b/app/app.iml
@@ -92,6 +92,7 @@
+
diff --git a/app/build.gradle b/app/build.gradle
index 28a66fe..42b5482 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -27,4 +27,5 @@ dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'commons-io:commons-io:2.4'
+ compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9e82dc8..b1a6aa6 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -38,7 +38,7 @@
+ android:resource="@xml/control_widget_info" />
diff --git a/app/src/main/java/max/music_cyclon/InfoPreference.java b/app/src/main/java/max/music_cyclon/InfoPreference.java
new file mode 100644
index 0000000..34f9288
--- /dev/null
+++ b/app/src/main/java/max/music_cyclon/InfoPreference.java
@@ -0,0 +1,15 @@
+package max.music_cyclon;
+
+import android.content.Context;
+import android.preference.DialogPreference;
+import android.util.AttributeSet;
+
+public class InfoPreference extends DialogPreference {
+
+ public InfoPreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+
+ setDialogMessage("Info");
+ }
+
+}
diff --git a/app/src/main/java/max/music_cyclon/LibraryService.java b/app/src/main/java/max/music_cyclon/LibraryService.java
index 226ec51..3255bcd 100644
--- a/app/src/main/java/max/music_cyclon/LibraryService.java
+++ b/app/src/main/java/max/music_cyclon/LibraryService.java
@@ -4,6 +4,7 @@ import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;
+import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.JsonReader;
@@ -63,17 +64,16 @@ public class LibraryService extends IntentService {
.setSmallIcon(R.mipmap.ic_launcher);
}
-
private NotificationCompat.Builder progressNotificationBuilder() {
return notificationBuilder().setUsesChronometer(true)
.setOngoing(true)
.setProgress(0, 0, true);
}
-
@Override
protected void onHandleIntent(Intent intent) {
- ExecutorService executor = Executors.newFixedThreadPool(4);
+ SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
+ ExecutorService executor = Executors.newFixedThreadPool(Integer.parseInt(settings.getString("threads", "2")));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
@@ -94,14 +94,14 @@ public class LibraryService extends IntentService {
try {
builder.setContentTitle("Fetching music information");
notificationManager.notify(NOTIFICATION_ID, builder.build());
- items = fetchRandom(1000);
+ items = fetchRandom(Integer.parseInt(settings.getString("download", "10")));
builder.setContentTitle("Cleaning library");
notificationManager.notify(NOTIFICATION_ID, builder.build());
tracker.delete();
} catch (IOException e) {
Log.wtf("WTF", e);
- notificationManager.cancel(NOTIFICATION_ID);
+ notificationManager.notify(NOTIFICATION_ID, notificationBuilder().setContentTitle("Remote not available").build());
return;
}
@@ -112,11 +112,11 @@ public class LibraryService extends IntentService {
}
builder.setContentTitle("Mixing new music!");
-// builder.setProgress(1, 0, false);
+ builder.setProgress(items.size(), 0, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());
- final CountDownLatch latch = new CountDownLatch(items.size());
+ CountDownLatch latch = new CountDownLatch(items.size());
for (int i = 0, size = items.size(); i < size; i++) {
executor.submit(new ProcessTask(latch, items.get(i), tracker,
diff --git a/app/src/main/java/max/music_cyclon/PowerConnectionReceiver.java b/app/src/main/java/max/music_cyclon/PowerConnectionReceiver.java
index 4a94691..ee834b2 100644
--- a/app/src/main/java/max/music_cyclon/PowerConnectionReceiver.java
+++ b/app/src/main/java/max/music_cyclon/PowerConnectionReceiver.java
@@ -4,27 +4,48 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.SharedPreferences;
import android.os.BatteryManager;
+import android.preference.PreferenceManager;
+
+import java.util.concurrent.TimeUnit;
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
+ SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
+ boolean start = Boolean.parseBoolean(settings.getString("start_charging", "false"));
+
+ if (!start) {
+ return;
+ }
+
+
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
+ if (batteryStatus == null) {
+ return;
+ }
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
- boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
+ boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (acCharge && isCharging) {
-// Intent serviceIntend = new Intent(context, LibraryService.class);
-//
-// context.startService(serviceIntend);
+
+ SharedPreferences preferences = context.getSharedPreferences("info", Context.MODE_PRIVATE);
+ long lastUpdated = preferences.getLong("last_updated", 0);
+
+ if (lastUpdated < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Integer.parseInt(settings.getString("min_download_interval", "7")))) {
+ Intent serviceIntend = new Intent(context, LibraryService.class);
+
+ context.startService(serviceIntend);
+ }
}
}
}
\ No newline at end of file
diff --git a/app/src/main/java/max/music_cyclon/ProcessTask.java b/app/src/main/java/max/music_cyclon/ProcessTask.java
index 52b0825..07ab086 100644
--- a/app/src/main/java/max/music_cyclon/ProcessTask.java
+++ b/app/src/main/java/max/music_cyclon/ProcessTask.java
@@ -6,14 +6,16 @@ import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import org.apache.commons.io.FileUtils;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.PrintWriter;
-import java.net.HttpURLConnection;
-import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Adler32;
@@ -50,18 +52,12 @@ public class ProcessTask implements Runnable {
File target = new File(root, item);
Adler32 checksum = new Adler32();
-// builder.setContentText(item);
-// notificationManager.notify(NOTIFICATION_ID, builder.build());
+ InputStream input = prepareConnection(item);
-
- HttpURLConnection connection = prepareConnection(item);
-
- if (connection == null) {
+ if (input == null) {
return;
}
-
- InputStream input = connection.getInputStream();
FileOutputStream output = FileUtils.openOutputStream(target);
byte[] buffer = new byte[4 * 1024];
@@ -88,24 +84,36 @@ public class ProcessTask implements Runnable {
}
}
- public HttpURLConnection prepareConnection(String item) throws IOException {
+ public InputStream prepareConnection(String item) throws IOException {
+// URL url = new URL("http", "max-arch", 5785, "/get");
+//
+// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+// connection.setRequestMethod("POST");
+// connection.setDoOutput(true);
+//
+// PrintWriter output = new PrintWriter(connection.getOutputStream());
+// output.write(item);
+// output.flush();
+//
+// if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
+// Log.e("ERROR", "Server returned HTTP " + connection.getResponseCode()
+// + " " + connection.getResponseMessage());
+// return null;
+// }
- URL url = new URL("http", "max-arch", 5000, "/get");
+ CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setDoOutput(true);
+ HttpPost httpPost = new HttpPost("http://max-arch:5785/get");
- PrintWriter output = new PrintWriter(connection.getOutputStream());
- output.write(item);
- output.flush();
+ httpPost.setEntity(new ByteArrayEntity(item.getBytes("UTF-8")));
- if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
- Log.e("ERROR", "Server returned HTTP " + connection.getResponseCode()
- + " " + connection.getResponseMessage());
+ CloseableHttpResponse response = httpclient.execute(httpPost);
+
+ if (response.getStatusLine().getStatusCode() != 200) {
+ Log.e("ERROR", "Server returned HTTP " + response.getStatusLine().getStatusCode());
return null;
}
- return connection;
+ return response.getEntity().getContent();
}
}
diff --git a/app/src/main/java/max/music_cyclon/SettingsActivity.java b/app/src/main/java/max/music_cyclon/SettingsActivity.java
index 26581c1..5f532d6 100644
--- a/app/src/main/java/max/music_cyclon/SettingsActivity.java
+++ b/app/src/main/java/max/music_cyclon/SettingsActivity.java
@@ -1,24 +1,39 @@
package max.music_cyclon;
-import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceFragment;
import android.view.View;
-public class SettingsActivity extends Activity {
-
+public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_settings);
- findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(SettingsActivity.this, LibraryService.class);
- startService(intent);
- }
- });
+ getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
+
+
+ public static class MyPreferenceFragment extends PreferenceFragment {
+
+ @Override
+ public void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.preferences);
+
+ findPreference("start").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ Intent intent = new Intent(getActivity(), LibraryService.class);
+ getActivity().startService(intent);
+ return true;
+ }
+ });
+
+ }
+ }
+
}
diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml
new file mode 100644
index 0000000..0d27c0c
--- /dev/null
+++ b/app/src/main/res/values/array.xml
@@ -0,0 +1,13 @@
+
+
+ - Headings
+ - Headings and Details
+ - All Data
+
+
+
+ - 1
+ - 2
+ - 3
+
+
\ No newline at end of file
diff --git a/app/src/main/res/xml/new_app_widget_info.xml b/app/src/main/res/xml/control_widget_info.xml
similarity index 100%
rename from app/src/main/res/xml/new_app_widget_info.xml
rename to app/src/main/res/xml/control_widget_info.xml
diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml
new file mode 100644
index 0000000..98fb730
--- /dev/null
+++ b/app/src/main/res/xml/preferences.xml
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file