Skip to content

Commit

Permalink
add HTTP endpoint: "/exit-service"
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Oct 15, 2021
1 parent dd26c3b commit 2990061
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ __extended APIs:__
--data-binary "${post_body}" \
"http://${airplay_ip}/start-activity"
```
* exit the Service:
```bash
# note: also closes the video player foreground Activity, and kills the process
curl --silent -X GET \
"http://${airplay_ip}/exit-service"
```

#### Notes:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ public interface Msg {
public static final int Msg_Show_Player = 15;
public static final int Msg_Hide_Player = 16;
public static final int Msg_Start_Activity = 17;
public static final int Msg_Exit_Service = 18;

public static final int Msg_Runtime_Permissions_Granted = 18;
public static final int Msg_Runtime_Permissions_Granted = 19;
}

public interface Target {
Expand All @@ -210,6 +211,7 @@ public interface Target {
public static final String PLAYER_SHOW = "/show-player";
public static final String PLAYER_HIDE = "/hide-player";
public static final String ACTIVITY_START = "/start-activity";
public static final String SERVICE_EXIT = "/exit-service";
}

public interface Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,13 @@ else if (
setCommonHeaders(httpResponse, HttpStatus.SC_BAD_REQUEST);
}
}
else if (target.equals(Constant.Target.SERVICE_EXIT)) {
Message msg = Message.obtain();
msg.what = Constant.Msg.Msg_Exit_Service;
MainApp.broadcastMessage(msg);

setCommonHeaders(httpResponse, HttpStatus.SC_OK);
}

// =======================================================================
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
Expand Down Expand Up @@ -95,7 +94,6 @@ public void handleMessage(Message msg) {
toast.show();

service.stopSelf();
Process.killProcess(Process.myPid()); //Quit the program completely
break;
}

Expand Down Expand Up @@ -132,6 +130,15 @@ public void handleMessage(Message msg) {
break;
}

// =======================================================================
// Exit Service
// =======================================================================

case Constant.Msg.Msg_Exit_Service : {
service.stopSelf();
break;
}

// =======================================================================
// Add media URLs to ExoPlayer queue.
// Display a video player when starting playback of a video URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.os.Build;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import android.view.Gravity;
import android.widget.RemoteViews;
Expand Down Expand Up @@ -116,14 +117,11 @@ public void onDestroy() {
super.onDestroy();
Log.d(tag, "onDestroy");

shutdown(false);
shutdown();
}

private void shutdown(boolean useForce) {
if (playerManager == null) {
if (useForce) stopSelf();
return;
}
private void shutdown() {
if (playerManager == null) return;

hide_player();

Expand Down Expand Up @@ -151,7 +149,7 @@ public void run() {
Log.e(tag, "problem shutting down HTTP server and Bonjour services", e);
}
finally {
if (useForce) stopSelf();
Process.killProcess(Process.myPid()); //Quit the program completely
}
}
}.start();
Expand Down Expand Up @@ -384,7 +382,9 @@ private void processIntent(Intent intent) {

switch (action) {
case ACTION_STOP : {
shutdown(true);
Message msg = Message.obtain();
msg.what = Constant.Msg.Msg_Exit_Service;
MainApp.broadcastMessage(msg);
break;
}
case ACTION_PLAY : {
Expand Down
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
releaseVersionCode = Integer.parseInt("002003116", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.00.31-16API'
releaseVersionCode = Integer.parseInt("002003216", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.00.32-16API'
javaVersion = JavaVersion.VERSION_1_8
minSdkVersion = 16
targetSdkVersion = 29
Expand Down
38 changes: 33 additions & 5 deletions tests/02. AirPlay sender.es5.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@
var $volume = document.querySelector('button#volume')
var $toast_message = document.querySelector('textarea#toast_message')
var $show_toast = document.querySelector('button#show_toast')
var $show_player = document.querySelector('button#show_player')
var $hide_player = document.querySelector('button#hide_player')
var $show_player = document.querySelector('button#show_player')
var $exit_service = document.querySelector('button#exit_service')
var $intent_data = document.querySelector('textarea#intent_data')
var $load_video = document.querySelector('button#load_video')
var $send_intent = document.querySelector('button#send_intent')
Expand Down Expand Up @@ -707,6 +708,16 @@
send_message(path, data)
}

$hide_player.onclick = function(event) {
event.preventDefault()
event.stopPropagation()

var path = '/hide-player'
var data = null

send_message(path, data)
}

$show_player.onclick = function(event) {
event.preventDefault()
event.stopPropagation()
Expand All @@ -717,11 +728,11 @@
send_message(path, data)
}

$hide_player.onclick = function(event) {
$exit_service.onclick = function(event) {
event.preventDefault()
event.stopPropagation()

var path = '/hide-player'
var path = '/exit-service'
var data = null

send_message(path, data)
Expand Down Expand Up @@ -1105,10 +1116,27 @@ <h3>User Interface:</h3>
</div>
<div>
<div><button id="show_toast">Show Toast</button></div>
<div><button id="show_player">Show Player</button></div>
<div><button id="hide_player">Hide Player</button></div>
<div></div>
<div></div>
</div>
</div>
<div>
<table align="center">
<tr>
<td class="oneline fullwidth">
<button id="hide_player">Hide Player</button>
</td>
<td class="oneline fullwidth">
<button id="show_player">Show Player</button>
</td>
</tr>
<tr>
<td colspan=2 align="center" class="oneline">
<button id="exit_service">Quit Application</button>
</td>
</tr>
</table>
</div>
<hr />
<h3>Start Activity:</h3>
<div class="flex">
Expand Down
38 changes: 33 additions & 5 deletions tests/02. AirPlay sender.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@
const $volume = document.querySelector('button#volume')
const $toast_message = document.querySelector('textarea#toast_message')
const $show_toast = document.querySelector('button#show_toast')
const $show_player = document.querySelector('button#show_player')
const $hide_player = document.querySelector('button#hide_player')
const $show_player = document.querySelector('button#show_player')
const $exit_service = document.querySelector('button#exit_service')
const $intent_data = document.querySelector('textarea#intent_data')
const $load_video = document.querySelector('button#load_video')
const $send_intent = document.querySelector('button#send_intent')
Expand Down Expand Up @@ -924,6 +925,16 @@
send_message(path, data)
}

$hide_player.onclick = (event) => {
event.preventDefault()
event.stopPropagation()

const path = '/hide-player'
const data = null

send_message(path, data)
}

$show_player.onclick = (event) => {
event.preventDefault()
event.stopPropagation()
Expand All @@ -934,11 +945,11 @@
send_message(path, data)
}

$hide_player.onclick = (event) => {
$exit_service.onclick = (event) => {
event.preventDefault()
event.stopPropagation()

const path = '/hide-player'
const path = '/exit-service'
const data = null

send_message(path, data)
Expand Down Expand Up @@ -1321,10 +1332,27 @@ <h3>User Interface:</h3>
</div>
<div>
<div><button id="show_toast">Show Toast</button></div>
<div><button id="show_player">Show Player</button></div>
<div><button id="hide_player">Hide Player</button></div>
<div></div>
<div></div>
</div>
</div>
<div>
<table align="center">
<tr>
<td class="oneline fullwidth">
<button id="hide_player">Hide Player</button>
</td>
<td class="oneline fullwidth">
<button id="show_player">Show Player</button>
</td>
</tr>
<tr>
<td colspan=2 align="center" class="oneline">
<button id="exit_service">Quit Application</button>
</td>
</tr>
</table>
</div>
<hr />
<h3>Start Activity:</h3>
<div class="flex">
Expand Down

0 comments on commit 2990061

Please sign in to comment.