Added receivers

master
Max Ammann 2016-06-14 20:50:35 +02:00
parent 2543eb1132
commit e4720c077c
3 changed files with 73 additions and 29 deletions

View File

@ -13,13 +13,23 @@
android:theme="@style/AppTheme">
<service
android:name=".service.LibraryService"
android:enabled="true" />
android:enabled="true"
android:launchMode="singleInstance"
/>
<!--android:process=":sync_service"-->
<receiver android:name=".service.PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
<!--<receiver android:name=".service.PowerConnectionReceiver">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />-->
<!--</intent-filter>-->
<!--</receiver>-->
<receiver
android:name=".tracker.ForceClearReceiver"
android:exported="false">
<intent-filter>
<action android:name="max.music_cyclon.force_clear" />
</intent-filter>
</receiver>
<activity
android:name=".SynchronizeActivity"
@ -31,7 +41,7 @@
</intent-filter>
</activity>
<activity android:name=".preference.MainPreferenceActivity"/>
<activity android:name=".preference.MainPreferenceActivity" />
</application>
</manifest>

View File

@ -2,6 +2,7 @@ package max.music_cyclon.service;
import android.Manifest;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
@ -13,17 +14,15 @@ import android.os.Messenger;
import android.os.Parcelable;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.maxmpz.poweramp.player.PowerampAPI;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@ -56,6 +55,8 @@ public class LibraryService extends IntentService {
public static final int MSG_STARTED = 4;
public static final int MSG_FINISHED = 5;
public static final String ARGUMENT_CONFIGS = "configs";
/**
* Keeps track of all current registered clients.
*/
@ -75,7 +76,7 @@ public class LibraryService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
Parcelable[] configs = intent.getParcelableArrayExtra("configs");
Parcelable[] configs = intent.getParcelableArrayExtra(ARGUMENT_CONFIGS);
ProgressUpdater updater = new ProgressUpdater(this);
@ -83,7 +84,7 @@ public class LibraryService extends IntentService {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
updater.showMessage("No permission to write!", false);
updater.showMessage("No permission to write!");
finished();
return;
}
@ -98,7 +99,7 @@ public class LibraryService extends IntentService {
BeetsFetcher fetcher = new BeetsFetcher(address, getResources());
if (root.exists() && !root.isDirectory()) {
updater.showMessage("Library is no dictionary! Fix manually", false);
updater.showMessage("Library is no dictionary! Fix manually");
finished();
return;
}
@ -108,14 +109,22 @@ public class LibraryService extends IntentService {
FileTracker tracker = new FileTracker(getApplicationContext());
try {
updater.showMessage("Cleaning library", true);
updater.showOngoingMessage("Cleaning library");
tracker.delete();
} catch (IOException e) {
e.printStackTrace();
}
if (root.exists() && root.list().length != 0) {
updater.showMessage("Library not empty! Clean in manually", false);
NotificationCompat.Builder builder = updater.notificationBuilder();
builder.setContentTitle("Library not empty! Clean in manually");
Intent libraryIntent = new Intent();
libraryIntent.setAction("max.music_cyclon.force_clear");
PendingIntent pending = PendingIntent.getBroadcast(this, 0, libraryIntent, 0);
builder.addAction(android.R.drawable.ic_delete, "Clear now", pending);
updater.updateNotification(builder);
finished();
return;
}
@ -126,25 +135,21 @@ public class LibraryService extends IntentService {
SynchronizeConfig config = (SynchronizeConfig) parcelable;
List<Item> items;
try {
updater.showMessage("Fetching music information for %s", true, config.getName());
updater.showOngoingMessage("Fetching music information for %s", config.getName());
items = fetcher.fetch(config);
} catch (IOException e) {
Log.wtf("WTF", e);
updater.showMessage("Remote not available", false);
updater.showMessage("Remote not available");
finished();
return;
}
updater.showMessage("Mixing new music for %s!", true, config.getName());
updater.showOngoingMessage("Mixing new music for %s!", config.getName());
updater.setMaximumProgress(items.size());
for (Item item : items) {
try {
URI uri = new URI(address + "/item/" + item.getId() + "/file");
tasks.add(new DownloadTask(config, uri, item.getPath(), tracker, updater));
} catch (URISyntaxException e) {
e.printStackTrace();
}
String url = address + "/item/" + item.getID() + "/file";
tasks.add(new DownloadTask(config, url, item.getPath(), tracker, updater));
}
}
@ -163,11 +168,7 @@ public class LibraryService extends IntentService {
e.printStackTrace();
}
updater.showMessage("Musik aktualisiert", false);
// Update last_updated info
SharedPreferences preferences = getSharedPreferences("info", MODE_PRIVATE);
preferences.edit().putLong("last_updated", System.currentTimeMillis()).apply();
updater.showMessage("Musik aktualisiert");
// Poweramp support
Intent poweramp = new Intent(PowerampAPI.Scanner.ACTION_SCAN_DIRS);
@ -175,6 +176,7 @@ public class LibraryService extends IntentService {
poweramp.putExtra(PowerampAPI.Scanner.EXTRA_FULL_RESCAN, true);
startService(poweramp);
finished();
}

View File

@ -0,0 +1,32 @@
package max.music_cyclon.tracker;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import max.music_cyclon.service.ProgressUpdater;
public class ForceClearReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
File root = new File(Environment.getExternalStorageDirectory(), "library");
try {
FileUtils.deleteDirectory(root);
} catch (IOException e) {
Log.e("LIBRARY", "Failed to delete library", e);
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(ProgressUpdater.NOTIFICATION_ID);
}
}