Added settings
parent
0ec9c1cda5
commit
9e02f9a91b
|
@ -92,6 +92,7 @@
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" exported="" name="appcompat-v7-22.0.0" level="project" />
|
<orderEntry type="library" exported="" name="appcompat-v7-22.0.0" level="project" />
|
||||||
<orderEntry type="library" exported="" name="commons-io-2.4" level="project" />
|
<orderEntry type="library" exported="" name="commons-io-2.4" level="project" />
|
||||||
|
<orderEntry type="library" exported="" name="httpclient-android-4.3.5.1" level="project" />
|
||||||
<orderEntry type="library" exported="" name="support-v4-22.0.0" level="project" />
|
<orderEntry type="library" exported="" name="support-v4-22.0.0" level="project" />
|
||||||
<orderEntry type="library" exported="" name="support-annotations-22.0.0" level="project" />
|
<orderEntry type="library" exported="" name="support-annotations-22.0.0" level="project" />
|
||||||
</component>
|
</component>
|
||||||
|
|
|
@ -27,4 +27,5 @@ dependencies {
|
||||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
compile fileTree(include: ['*.jar'], dir: 'libs')
|
||||||
compile 'com.android.support:appcompat-v7:22.0.0'
|
compile 'com.android.support:appcompat-v7:22.0.0'
|
||||||
compile 'commons-io:commons-io:2.4'
|
compile 'commons-io:commons-io:2.4'
|
||||||
|
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="android.appwidget.provider"
|
android:name="android.appwidget.provider"
|
||||||
android:resource="@xml/new_app_widget_info" />
|
android:resource="@xml/control_widget_info" />
|
||||||
</receiver>
|
</receiver>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import android.app.IntentService;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v4.app.NotificationManagerCompat;
|
import android.support.v4.app.NotificationManagerCompat;
|
||||||
import android.util.JsonReader;
|
import android.util.JsonReader;
|
||||||
|
@ -63,17 +64,16 @@ public class LibraryService extends IntentService {
|
||||||
.setSmallIcon(R.mipmap.ic_launcher);
|
.setSmallIcon(R.mipmap.ic_launcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private NotificationCompat.Builder progressNotificationBuilder() {
|
private NotificationCompat.Builder progressNotificationBuilder() {
|
||||||
return notificationBuilder().setUsesChronometer(true)
|
return notificationBuilder().setUsesChronometer(true)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setProgress(0, 0, true);
|
.setProgress(0, 0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onHandleIntent(Intent intent) {
|
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);
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
||||||
|
@ -94,14 +94,14 @@ public class LibraryService extends IntentService {
|
||||||
try {
|
try {
|
||||||
builder.setContentTitle("Fetching music information");
|
builder.setContentTitle("Fetching music information");
|
||||||
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
||||||
items = fetchRandom(1000);
|
items = fetchRandom(Integer.parseInt(settings.getString("download", "10")));
|
||||||
|
|
||||||
builder.setContentTitle("Cleaning library");
|
builder.setContentTitle("Cleaning library");
|
||||||
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
||||||
tracker.delete();
|
tracker.delete();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.wtf("WTF", e);
|
Log.wtf("WTF", e);
|
||||||
notificationManager.cancel(NOTIFICATION_ID);
|
notificationManager.notify(NOTIFICATION_ID, notificationBuilder().setContentTitle("Remote not available").build());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,11 +112,11 @@ public class LibraryService extends IntentService {
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.setContentTitle("Mixing new music!");
|
builder.setContentTitle("Mixing new music!");
|
||||||
// builder.setProgress(1, 0, false);
|
builder.setProgress(items.size(), 0, false);
|
||||||
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
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++) {
|
for (int i = 0, size = items.size(); i < size; i++) {
|
||||||
executor.submit(new ProcessTask(latch, items.get(i), tracker,
|
executor.submit(new ProcessTask(latch, items.get(i), tracker,
|
||||||
|
|
|
@ -4,27 +4,48 @@ import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class PowerConnectionReceiver extends BroadcastReceiver {
|
public class PowerConnectionReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
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);
|
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||||
Intent batteryStatus = context.registerReceiver(null, ifilter);
|
Intent batteryStatus = context.registerReceiver(null, ifilter);
|
||||||
|
|
||||||
|
if (batteryStatus == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
||||||
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
||||||
|
|
||||||
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
|
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
|
||||||
status == BatteryManager.BATTERY_STATUS_FULL;
|
status == BatteryManager.BATTERY_STATUS_FULL;
|
||||||
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
|
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
|
||||||
|
|
||||||
if (acCharge && isCharging) {
|
if (acCharge && isCharging) {
|
||||||
// Intent serviceIntend = new Intent(context, LibraryService.class);
|
|
||||||
//
|
SharedPreferences preferences = context.getSharedPreferences("info", Context.MODE_PRIVATE);
|
||||||
// context.startService(serviceIntend);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,14 +6,16 @@ import android.support.v4.app.NotificationManagerCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
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.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
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.CountDownLatch;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.zip.Adler32;
|
import java.util.zip.Adler32;
|
||||||
|
@ -50,18 +52,12 @@ public class ProcessTask implements Runnable {
|
||||||
File target = new File(root, item);
|
File target = new File(root, item);
|
||||||
Adler32 checksum = new Adler32();
|
Adler32 checksum = new Adler32();
|
||||||
|
|
||||||
// builder.setContentText(item);
|
InputStream input = prepareConnection(item);
|
||||||
// notificationManager.notify(NOTIFICATION_ID, builder.build());
|
|
||||||
|
|
||||||
|
if (input == null) {
|
||||||
HttpURLConnection connection = prepareConnection(item);
|
|
||||||
|
|
||||||
if (connection == null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
InputStream input = connection.getInputStream();
|
|
||||||
FileOutputStream output = FileUtils.openOutputStream(target);
|
FileOutputStream output = FileUtils.openOutputStream(target);
|
||||||
|
|
||||||
byte[] buffer = new byte[4 * 1024];
|
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();
|
HttpPost httpPost = new HttpPost("http://max-arch:5785/get");
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
|
|
||||||
PrintWriter output = new PrintWriter(connection.getOutputStream());
|
httpPost.setEntity(new ByteArrayEntity(item.getBytes("UTF-8")));
|
||||||
output.write(item);
|
|
||||||
output.flush();
|
|
||||||
|
|
||||||
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||||
Log.e("ERROR", "Server returned HTTP " + connection.getResponseCode()
|
|
||||||
+ " " + connection.getResponseMessage());
|
if (response.getStatusLine().getStatusCode() != 200) {
|
||||||
|
Log.e("ERROR", "Server returned HTTP " + response.getStatusLine().getStatusCode());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return connection;
|
return response.getEntity().getContent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,39 @@
|
||||||
package max.music_cyclon;
|
package max.music_cyclon;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.preference.Preference;
|
||||||
|
import android.preference.PreferenceActivity;
|
||||||
|
import android.preference.PreferenceFragment;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
|
|
||||||
public class SettingsActivity extends Activity {
|
public class SettingsActivity extends PreferenceActivity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_settings);
|
|
||||||
|
|
||||||
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
|
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
|
||||||
@Override
|
|
||||||
public void onClick(View view) {
|
|
||||||
Intent intent = new Intent(SettingsActivity.this, LibraryService.class);
|
|
||||||
startService(intent);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<resources>
|
||||||
|
<string-array name="listArray">
|
||||||
|
<item>Headings</item>
|
||||||
|
<item>Headings and Details</item>
|
||||||
|
<item>All Data</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
|
<string-array name="listValues">
|
||||||
|
<item>1</item>
|
||||||
|
<item>2</item>
|
||||||
|
<item>3</item>
|
||||||
|
</string-array>
|
||||||
|
</resources>
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<Preference
|
||||||
|
android:key="start"
|
||||||
|
android:summary="Starts the download"
|
||||||
|
android:title="Start Service" />
|
||||||
|
|
||||||
|
<EditTextPreference
|
||||||
|
android:defaultValue="2"
|
||||||
|
android:inputType="number"
|
||||||
|
android:key="threads"
|
||||||
|
android:summary="Number of threads to use for downloading"
|
||||||
|
android:title="Threads" />
|
||||||
|
|
||||||
|
<EditTextPreference
|
||||||
|
android:defaultValue="1000"
|
||||||
|
android:inputType="number"
|
||||||
|
android:key="download"
|
||||||
|
android:summary="Approximately amount in MB to download"
|
||||||
|
android:title="Download size" />
|
||||||
|
|
||||||
|
<SwitchPreference
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:key="start_charging"
|
||||||
|
android:summary="This option if selected will allow to start the download if connected with a ac charger"
|
||||||
|
android:title="Charging starts download" />
|
||||||
|
|
||||||
|
<EditTextPreference
|
||||||
|
android:defaultValue="7"
|
||||||
|
android:inputType="number"
|
||||||
|
android:key="min_download_interval"
|
||||||
|
android:summary="Minimum download interval in days"
|
||||||
|
android:title="Download interval" />
|
||||||
|
|
||||||
|
|
||||||
|
<max.music_cyclon.InfoPreference
|
||||||
|
android:summary="0.1"
|
||||||
|
android:title="Application info" />
|
||||||
|
|
||||||
|
</PreferenceScreen>
|
Loading…
Reference in New Issue