Skip to content

Commit

Permalink
Merge pull request #141 from tobexyz/feat/issue131
Browse files Browse the repository at this point in the history
Feat/issue131
  • Loading branch information
tobexyz authored Dec 25, 2024
2 parents 53f4cac + fac23e7 commit 1875a59
Show file tree
Hide file tree
Showing 35 changed files with 93 additions and 1,777 deletions.
3 changes: 3 additions & 0 deletions yaacc/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:maxSdkVersion="33"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ public boolean onBackPressed() {
}
// currentPosition
initBrowsItemAdapter(itemList);
itemList.postDelayed(() -> itemList.smoothScrollToPosition(lastPosition.getPositionId()), 100);
((LinearLayoutManager) itemList.getLayoutManager()).scrollToPositionWithOffset(lastPosition.getPositionId(), 0);
itemList.postDelayed(() -> {
((LinearLayoutManager) itemList.getLayoutManager()).scrollToPositionWithOffset(lastPosition.getPositionId(), 0);
}, 200);
bItemAdapter.clear();
bItemAdapter.loadMore();

Expand Down
26 changes: 24 additions & 2 deletions yaacc/src/main/java/de/yaacc/browser/TabBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package de.yaacc.browser;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
Expand All @@ -26,6 +28,8 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
Expand Down Expand Up @@ -88,9 +92,10 @@ public class TabBrowserActivity extends AppCompatActivity implements OnClickList
Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.GET_TASKS,
Manifest.permission.RECEIVE_BOOT_COMPLETED,
Manifest.permission.WAKE_LOCK
Manifest.permission.WAKE_LOCK,
Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

};
private static final String CURRENT_TAB_KEY = "currentTab";
//FIXME dirty
Expand Down Expand Up @@ -170,6 +175,8 @@ public void onPageSelected(int position) {
Log.d(getClass().getName(), "All permissions granted");
}

checkBatteryOptimizationEnabled();

// local server startup
upnpClient = ((Yaacc) getApplicationContext()).getUpnpClient();
if (upnpClient == null) {
Expand All @@ -188,6 +195,21 @@ public void onPageSelected(int position) {
Log.d(this.getClass().getName(), "on create took: " + (System.currentTimeMillis() - start));
}

private void checkBatteryOptimizationEnabled() {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
try {
startActivity(intent);
} catch (ActivityNotFoundException ex) {
Log.d(getClass().getName(), "Ignoring exception ActivityNotFoundException during check for battery optimization");
}
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import de.yaacc.R;
import de.yaacc.upnp.ActionState;
import de.yaacc.upnp.SynchronizationInfo;
import de.yaacc.upnp.UpnpClient;

public class AVTransportController extends AVTransportPlayer {
Expand All @@ -48,7 +47,6 @@ public AVTransportController(UpnpClient upnpClient, Device<?, ?, ?> receiverDevi
+ "@" + deviceName;
setName(deviceName);
setShortName(receiverDevice.getDetails().getFriendlyName());
setSyncInfo(new SynchronizationInfo());
}

public void onServiceConnected(ComponentName className, IBinder binder) {
Expand Down
5 changes: 2 additions & 3 deletions yaacc/src/main/java/de/yaacc/player/AVTransportPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public class AVTransportPlayer extends AbstractPlayer {
private PositionInfo currentPositionInfo;
private ActionState positionActionState = null;
private URI albumArtUri;
private long itemDuration;


/**
Expand Down Expand Up @@ -176,13 +175,13 @@ protected void startItem(PlayableItem playableItem, Object loadedItem) {
return;
}
Log.d(getClass().getName(), "Action SetAVTransportURI ");
itemDuration = playableItem.getDuration();
final ActionState actionState = new ActionState();
actionState.actionFinished = false;
Item item = playableItem.getItem();
String metadata;
try {
metadata = new DIDLParser().generate((item == null) ? new DIDLContent() : new DIDLContent().addItem(item), false);

} catch (Exception e) {
Log.d(getClass().getName(), "Error while generating Didl-Item xml: " + e);
metadata = "";
Expand Down Expand Up @@ -562,7 +561,7 @@ private void doExit() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
Log.w(getClass().getName(), e);
}
};
waitForActionComplete(actionState, fn);
Expand Down
67 changes: 9 additions & 58 deletions yaacc/src/main/java/de/yaacc/player/AbstractPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,14 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import de.yaacc.R;
import de.yaacc.Yaacc;
import de.yaacc.upnp.SynchronizationInfo;
import de.yaacc.upnp.UpnpClient;

/**
Expand All @@ -73,7 +70,6 @@ public abstract class AbstractPlayer implements Player, ServiceConnection {
private PlayerService playerService;
private String name;
private String shortName;
private SynchronizationInfo syncInfo;
private boolean paused;
private Object loadedItem = null;
private int currentLoadedIndex = -1;
Expand Down Expand Up @@ -201,7 +197,7 @@ public void previous() {
cancelTimer();
currentIndex--;
if (currentIndex < 0) {
if (items.size() > 0) {
if (!items.isEmpty()) {
currentIndex = items.size() - 1;
} else {
currentIndex = 0;
Expand Down Expand Up @@ -249,7 +245,7 @@ public void run() {
doPause();
setProcessingCommand(false);
}
}, getExecutionTime());
}, new Date(System.currentTimeMillis()));
}

/*
Expand Down Expand Up @@ -288,9 +284,7 @@ public void run() {
setProcessingCommand(false);
}
}
}, getExecutionTime());


}, new Date(System.currentTimeMillis()));
}


Expand Down Expand Up @@ -320,14 +314,14 @@ public void run() {
toast.show();
});
}
if (items.size() > 0) {
if (!items.isEmpty()) {
stopItem(items.get(currentIndex));
}
isPlaying = false;
paused = false;
setProcessingCommand(false);
}
}, getExecutionTime());
}, new Date(System.currentTimeMillis()));
}

/**
Expand Down Expand Up @@ -459,7 +453,7 @@ protected Object loadItem(int toLoadIndex) {
}

protected void loadItem(int previousIndex, int nextIndex) {
if (items.size() == 0)
if (items.isEmpty())
return;
PlayableItem playableItem = items.get(nextIndex);
Object loadedItem = loadItem(nextIndex);
Expand Down Expand Up @@ -571,23 +565,14 @@ public void startTimer(final long duration) {
);
Log.d(getClass().getName(), "AndAllowWhileIdle alarm event in: " + (System.currentTimeMillis() + duration));
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
} else {
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + duration,
alarmIntent
);
Log.d(getClass().getName(), "exact alarm event in: " + (System.currentTimeMillis() + duration));
} else {
alarmManager.set(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + duration,
alarmIntent
);
Log.d(getClass().getName(), "set alarm event in: " + (System.currentTimeMillis() + duration));
}


});

}
Expand Down Expand Up @@ -759,40 +744,6 @@ public URI getAlbumArt() {
return null;
}

@Override
public SynchronizationInfo getSyncInfo() {
return syncInfo;
}

@Override
public void setSyncInfo(SynchronizationInfo syncInfo) {
if (syncInfo == null) {
syncInfo = new SynchronizationInfo();
}
this.syncInfo = syncInfo;
}

protected Date getExecutionTime() {
Calendar execTime = Calendar.getInstance(Locale.getDefault());
if (getSyncInfo() != null) {
execTime.set(Calendar.HOUR_OF_DAY, getSyncInfo().getReferencedPresentationTimeOffset().getHour());
execTime.set(Calendar.MINUTE, getSyncInfo().getReferencedPresentationTimeOffset().getMinute());
execTime.set(Calendar.SECOND, getSyncInfo().getReferencedPresentationTimeOffset().getSecond());
execTime.set(Calendar.MILLISECOND, getSyncInfo().getReferencedPresentationTimeOffset().getMillis());
execTime.add(Calendar.HOUR, getSyncInfo().getOffset().getHour());
execTime.add(Calendar.MINUTE, getSyncInfo().getOffset().getMinute());
execTime.add(Calendar.SECOND, getSyncInfo().getOffset().getSecond());
execTime.add(Calendar.MILLISECOND, getSyncInfo().getOffset().getMillis());
Log.d(getClass().getName(), "ReferencedRepresentationTimeOffset: " + getSyncInfo().getReferencedPresentationTimeOffset());
}
Log.d(getClass().getName(), "current time: " + new Date() + " get execution time: " + execTime.getTime());
if (execTime.getTime().getTime() <= System.currentTimeMillis()) {
Log.d(getClass().getName(), "ExecutionTime is in past!! We will start immediately");
return null;

}
return execTime.getTime();
}

protected void executeCommand(TimerTask command, Date executionTime) {
if (execTimer != null) {
Expand Down Expand Up @@ -853,12 +804,12 @@ public boolean hasActionGetMute() {
@Override
public void fastForward(int i) {

seekTo(getCurrentPosition() + (i * 1000));
seekTo(getCurrentPosition() + (i * 1000L));
}

@Override
public void fastRewind(int i) {
seekTo(getCurrentPosition() - (i * 1000));
seekTo(getCurrentPosition() - (i * 1000L));
}

}
25 changes: 4 additions & 21 deletions yaacc/src/main/java/de/yaacc/player/LocalImagePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -42,7 +43,6 @@
import de.yaacc.Yaacc;
import de.yaacc.imageviewer.ImageViewerActivity;
import de.yaacc.imageviewer.ImageViewerBroadcastReceiver;
import de.yaacc.upnp.SynchronizationInfo;
import de.yaacc.upnp.UpnpClient;
import de.yaacc.util.NotificationId;

Expand All @@ -58,7 +58,6 @@ public class LocalImagePlayer implements Player, ServiceConnection {
private Timer commandExecutionTimer;
private String name;
private String shortName;
private SynchronizationInfo syncInfo;
private PendingIntent notificationIntent;
private PlayerService playerService;
private boolean isPlaying;
Expand Down Expand Up @@ -190,7 +189,7 @@ public void run() {
setPlaying(false);

}
}, getExecutionTime());
}, new Date());

}

Expand Down Expand Up @@ -218,7 +217,7 @@ public void run() {
setPlaying(true);

}
}, getExecutionTime());
}, new Date());

}

Expand All @@ -245,7 +244,7 @@ public void run() {
setPlaying(false);

}
}, getExecutionTime());
}, new Date());

}

Expand Down Expand Up @@ -500,22 +499,6 @@ public void setIcon(Bitmap icon) {

}

@Override
public SynchronizationInfo getSyncInfo() {
return syncInfo;
}

@Override
public void setSyncInfo(SynchronizationInfo syncInfo) {
if (syncInfo == null) {
syncInfo = new SynchronizationInfo();
}
this.syncInfo = syncInfo;
}

private long getExecutionTime() {
return getSyncInfo().getOffset().toNanos() / 1000000 + 600L;
}

//TODO Refactor not every player has a volume control
public boolean getMute() {
Expand Down
Loading

0 comments on commit 1875a59

Please sign in to comment.