Cleanup and added icon

master
Max Ammann 2015-08-20 17:40:16 +02:00
parent 9e02f9a91b
commit 77299d2966
20 changed files with 56 additions and 184 deletions

View File

@ -2,7 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="max.music_cyclon" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
@ -13,7 +12,7 @@
android:theme="@style/AppTheme" >
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
android:label="Music Cyclon" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -31,15 +30,6 @@
</intent-filter>
</receiver>
<receiver android:name=".ControlWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/control_widget_info" />
</receiver>
</application>
</manifest>

View File

@ -1,37 +0,0 @@
package max.music_cyclon;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class ControlWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.control_button);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.control_widget);
views.setTextViewText(R.id.control_button, widgetText);
Intent intent = new Intent(context, LibraryService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.control_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

View File

@ -10,13 +10,17 @@ import android.support.v4.app.NotificationManagerCompat;
import android.util.JsonReader;
import android.util.Log;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
@ -42,11 +46,20 @@ public class LibraryService extends IntentService {
this.port = port;
}
public List<String> fetchRandom(int amount) throws IOException {
URL url = new URL("http", host, port, "/random/" + amount);
InputStream in = url.openStream();
public List<String> fetchRandom(String address, int amount) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(in, "UTF-8")));
HttpGet httpGet = new HttpGet(address + "/random/" + amount);
CloseableHttpResponse response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != 200) {
Log.e("ERROR", "Server returned HTTP " + response.getStatusLine().getStatusCode());
return Collections.emptyList();
}
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")));
ArrayList<String> items = new ArrayList<>();
@ -75,6 +88,8 @@ public class LibraryService extends IntentService {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
ExecutorService executor = Executors.newFixedThreadPool(Integer.parseInt(settings.getString("threads", "2")));
String address = settings.getString("address", "127.0.0.1");
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder builder = progressNotificationBuilder().setContentTitle("Aktualisiere Musik");
@ -94,7 +109,7 @@ public class LibraryService extends IntentService {
try {
builder.setContentTitle("Fetching music information");
notificationManager.notify(NOTIFICATION_ID, builder.build());
items = fetchRandom(Integer.parseInt(settings.getString("download", "10")));
items = fetchRandom(address, Integer.parseInt(settings.getString("download", "10")));
builder.setContentTitle("Cleaning library");
notificationManager.notify(NOTIFICATION_ID, builder.build());
@ -119,7 +134,7 @@ public class LibraryService extends IntentService {
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,
executor.submit(new ProcessTask(address, latch, items.get(i), tracker,
current, items.size(), builder, notificationManager));
}

View File

@ -15,7 +15,7 @@ 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"));
boolean start = settings.getBoolean("start_charging", false);
if (!start) {
return;

View File

@ -22,6 +22,7 @@ import java.util.zip.Adler32;
public class ProcessTask implements Runnable {
private String address;
private final CountDownLatch latch;
private final String item;
@ -32,8 +33,9 @@ public class ProcessTask implements Runnable {
private final NotificationCompat.Builder builder;
private final NotificationManagerCompat notificationManager;
public ProcessTask(CountDownLatch latch, String item, FileTracker tracker,
AtomicInteger current, int maximum,NotificationCompat.Builder builder, NotificationManagerCompat notificationCompat) {
public ProcessTask(String address, CountDownLatch latch, String item, FileTracker tracker,
AtomicInteger current, int maximum, NotificationCompat.Builder builder, NotificationManagerCompat notificationCompat) {
this.address = address;
this.latch = latch;
this.item = item;
@ -48,28 +50,30 @@ public class ProcessTask implements Runnable {
public void run() {
File root = new File(Environment.getExternalStorageDirectory(), "library");
try {
File target = new File(root, item);
Adler32 checksum = new Adler32();
InputStream input = prepareConnection(item);
InputStream input = prepareConnection(address, item);
if (input == null) {
return;
if (input != null) {
FileOutputStream output = FileUtils.openOutputStream(target);
byte[] buffer = new byte[4 * 1024];
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
checksum.update(buffer, 0, n);
}
output.flush();
output.close();
input.close();
}
FileOutputStream output = FileUtils.openOutputStream(target);
byte[] buffer = new byte[4 * 1024];
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
checksum.update(buffer, 0, n);
}
output.flush();
output.close();
input.close();
// todo else error
tracker.track(target, checksum.getValue());
@ -84,26 +88,10 @@ public class ProcessTask implements Runnable {
}
}
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;
// }
public InputStream prepareConnection(String address, String item) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://max-arch:5785/get");
HttpPost httpPost = new HttpPost(address + "/get");
httpPost.setEntity(new ByteArrayEntity(item.getBytes("UTF-8")));

View File

@ -5,7 +5,6 @@ import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.view.View;
public class SettingsActivity extends PreferenceActivity {

View File

@ -1,17 +0,0 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="max.music_cyclon.SettingsActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Music"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp" />
</RelativeLayout>

View File

@ -1,21 +0,0 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">
<Button
android:id="@+id/control_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:background="#09C"
android:contentDescription="@string/control_button"
android:text="@string/control_button"
android:textColor="#ffffff"
android:textSize="24sp"
android:textStyle="bold|italic" />
</RelativeLayout>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">0dp</dimen>
</resources>

View File

@ -1,6 +0,0 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@ -1,13 +0,0 @@
<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

@ -1,11 +0,0 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<!--
Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">8dp</dimen>
</resources>

View File

@ -1,6 +1,3 @@
<resources>
<string name="app_name">music-cyclon</string>
<string name="control_button">MUSIC</string>
<string name="title_activity_settings">Title</string>
</resources>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/control_widget"
android:initialLayout="@layout/control_widget"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />

View File

@ -6,6 +6,14 @@
android:summary="Starts the download"
android:title="Start Service" />
<EditTextPreference
android:defaultValue="http://localhost:5785"
android:inputType="text"
android:key="address"
android:summary="Address of the synchronisation server"
android:title="Address" />
<EditTextPreference
android:defaultValue="2"
android:inputType="number"