Added settings

master
Max Ammann 2015-08-19 23:32:57 +02:00
parent 0ec9c1cda5
commit 9e02f9a91b
11 changed files with 161 additions and 46 deletions

View File

@ -92,6 +92,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<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="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-annotations-22.0.0" level="project" />
</component>

View File

@ -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'
}

View File

@ -38,7 +38,7 @@
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
android:resource="@xml/control_widget_info" />
</receiver>
</application>

View File

@ -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");
}
}

View File

@ -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,

View File

@ -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);
}
}
}
}

View File

@ -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();
}
}

View File

@ -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;
}
});
}
}
}

View File

@ -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>

View File

@ -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>