Skip to content

Commit

Permalink
interpolate special substrings in POST to HTTP endpoint: "/show-toast"
Browse files Browse the repository at this point in the history
summary:
* "{{date}}"
  - current date
  - ex: "Mon, 01/01/1970"
* "{{time}}"
  - current time
  - ex: "1:30 PM"
* "{{version}}"
  - installed version of ExoAirPlayer
  - ex: "ExoAirPlayer 002.00.34-16API"
* "{{abi}}"
  - list of ABIs supported by Android device
  - ex: "armeabi-v7a, armeabi"
* "{{top-process}}"
  - package name of top Process
  - ex: "com.github.warren_bank.exoplayer_airplay_receiver"
  - notes:
    * only supported by Android < 5.1.1
* "{{top-activity}}"
  - class name of top Activity
  - ex: "com.github.warren_bank.exoplayer_airplay_receiver.ui.VideoPlayerActivity"
  - notes:
    * only supported by Android < 5.0
    * requires permission: "android.permission.GET_TASKS"
  • Loading branch information
warren-bank committed Oct 22, 2021
1 parent 570dbe7 commit 1f0a171
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.GET_TASKS" /> <!-- used to interpolate "{{top-activity}}" in "/show-toast" text, which is only supported by Android < 5.0 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void onAudioSessionIdChanged(int audioSessionId) {
this.audioListener.onAudioSessionIdChanged( this.exoPlayer.getAudioSessionId() );
}

String userAgent = context.getResources().getString(R.string.user_agent);
String userAgent = context.getString(R.string.user_agent);
this.httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
this.rawDataSourceFactory = new DefaultDataSourceFactory(context, userAgent);
this.loadErrorHandlingPolicy = new MyLoadErrorHandlingPolicy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.github.warren_bank.exoplayer_airplay_receiver.utils.ExternalStorageUtils;
import com.github.warren_bank.exoplayer_airplay_receiver.utils.MediaTypeUtils;
import com.github.warren_bank.exoplayer_airplay_receiver.utils.StringUtils;
import com.github.warren_bank.exoplayer_airplay_receiver.utils.ToastUtils;

import android.content.Intent;
import android.net.Uri;
Expand Down Expand Up @@ -114,7 +115,13 @@ public void handleMessage(Message msg) {
// =======================================================================

case Constant.Msg.Msg_Show_Toast : {
Toast.makeText(service.getApplicationContext(), (String) msg.obj, Toast.LENGTH_LONG).show();
String text;
text = (String) msg.obj;
text = ToastUtils.interpolate_variables(service.getApplicationContext(), text);

if (!TextUtils.isEmpty(text)) {
Toast.makeText(service.getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.warren_bank.exoplayer_airplay_receiver.utils;

import android.text.TextUtils;

import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
Expand Down Expand Up @@ -124,7 +126,7 @@ public static HashMap<String, String> parseDuplicateKeyValues(List<String> list)
public static HashMap<String, String> parseDuplicateKeyValues(List<String> list, boolean normalize_lowercase_keys) {
if ((list == null) || list.isEmpty()) return null;

String requestBody = String.join("\n", list);
String requestBody = TextUtils.join("\n", list);

return StringUtils.parseRequestBody(requestBody, normalize_lowercase_keys);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package com.github.warren_bank.exoplayer_airplay_receiver.utils;

import com.github.warren_bank.exoplayer_airplay_receiver.BuildConfig;
import com.github.warren_bank.exoplayer_airplay_receiver.R;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ToastUtils {

public static String interpolate_variables(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

text = ToastUtils.interpolate_date(context, text);
text = ToastUtils.interpolate_time(context, text);
text = ToastUtils.interpolate_version(context, text);
text = ToastUtils.interpolate_abi(context, text);
text = ToastUtils.interpolate_top_process(context, text);
text = ToastUtils.interpolate_top_activity(context, text);

text = (TextUtils.isEmpty(text)) ? null : text.trim();

return text;
}

private static String interpolate_date(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

String variable_substring = context.getString(R.string.toast_variable_date);

if (!text.contains(variable_substring))
return text;

try {
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MM/dd/yyyy");
String date = formatter.format(new Date());

return ToastUtils.interpolate_variable(text, variable_substring, date);
}
catch(Exception e) {
return ToastUtils.interpolate_variable(text, variable_substring, "");
}
}

private static String interpolate_time(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

String variable_substring = context.getString(R.string.toast_variable_time);

if (!text.contains(variable_substring))
return text;

try {
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
String time = formatter.format(new Date());

return ToastUtils.interpolate_variable(text, variable_substring, time);
}
catch(Exception e) {
return ToastUtils.interpolate_variable(text, variable_substring, "");
}
}

private static String interpolate_version(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

String variable_substring = context.getString(R.string.toast_variable_version);

if (!text.contains(variable_substring))
return text;

try {
String appname = context.getString(R.string.app_name);
String version = appname + " " + BuildConfig.VERSION_NAME;

return ToastUtils.interpolate_variable(text, variable_substring, version);
}
catch(Exception e) {
return ToastUtils.interpolate_variable(text, variable_substring, "");
}
}

private static String interpolate_abi(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

String variable_substring = context.getString(R.string.toast_variable_abi);

if (!text.contains(variable_substring))
return text;

try {
String abi;

if (Build.VERSION.SDK_INT >= 21) {
abi = TextUtils.join(", ", Build.SUPPORTED_ABIS);
}
else {
abi = TextUtils.isEmpty(Build.CPU_ABI)
? null
: TextUtils.isEmpty(Build.CPU_ABI2)
? Build.CPU_ABI
: Build.CPU_ABI + ", " + Build.CPU_ABI2
;
}

return ToastUtils.interpolate_variable(text, variable_substring, abi);
}
catch(Exception e) {
return ToastUtils.interpolate_variable(text, variable_substring, "");
}
}

private static String interpolate_top_process(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

String variable_substring = context.getString(R.string.toast_variable_top_process);

if (!text.contains(variable_substring))
return text;

try {
String top_process = null;

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

// Android 5.1.1+ only returns the process that is running ExoAirPlayer
for (ActivityManager.RunningAppProcessInfo processInfo : am.getRunningAppProcesses()) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
top_process = processInfo.processName;
break;
}
}

return ToastUtils.interpolate_variable(text, variable_substring, top_process);
}
catch(Exception e) {
return ToastUtils.interpolate_variable(text, variable_substring, "");
}
}

private static String interpolate_top_activity(Context context, String text) {
if (TextUtils.isEmpty(text)) return null;

String variable_substring = context.getString(R.string.toast_variable_top_activity);

if (!text.contains(variable_substring))
return text;

try {
String top_activity = null;

if (Build.VERSION.SDK_INT < 21) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

for (ActivityManager.RunningTaskInfo taskInfo : am.getRunningTasks(3)) {
if (taskInfo.topActivity != null) {
top_activity = taskInfo.topActivity.getClassName();
break;
}
}
}

return ToastUtils.interpolate_variable(text, variable_substring, top_activity);
}
catch(Exception e) {
return ToastUtils.interpolate_variable(text, variable_substring, "");
}
}

private static String interpolate_variable(String text, String variable, String value) {
if (TextUtils.isEmpty(text))
return null;
if (TextUtils.isEmpty(variable))
return text;
if (value == null)
value = "";

try {
return text.replace((CharSequence) variable, (CharSequence) value);
}
catch(Exception e) {
return text;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<string name="toast_registration_success">AirPlay registration success</string>
<string name="toast_registration_failure">AirPlay registration failed</string>

<!-- strings that are interpolated within "/show-toast" text -->
<string translatable="false" name="toast_variable_date">{{date}}</string>
<string translatable="false" name="toast_variable_time">{{time}}</string>
<string translatable="false" name="toast_variable_version">{{version}}</string>
<string translatable="false" name="toast_variable_abi">{{abi}}</string>
<string translatable="false" name="toast_variable_top_process">{{top-process}}</string>
<string translatable="false" name="toast_variable_top_activity">{{top-activity}}</string>

<!-- HTTP request header -->
<string translatable="false" name="user_agent">Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36</string>

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("002003316", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.00.33-16API'
releaseVersionCode = Integer.parseInt("002003416", 10) //Integer.MAX_VALUE == 2147483647
releaseVersion = '002.00.34-16API'
javaVersion = JavaVersion.VERSION_1_8
minSdkVersion = 16
targetSdkVersion = 29
Expand Down
32 changes: 32 additions & 0 deletions tests/02. AirPlay sender.es5.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
body .contents textarea {
width: 100%;
}
body .contents textarea#req_headers,
body .contents textarea#drm_headers,
body .contents textarea#intent_data {
white-space: nowrap;
}
Expand Down Expand Up @@ -106,6 +108,11 @@
width: 10em;
}

select#toast_variables {
width: 8em;
padding: 4px;
}

input[type="file"] {
display: none;
}
Expand Down Expand Up @@ -172,6 +179,7 @@
var $volume_factor = document.querySelector('input#volume_factor')
var $volume = document.querySelector('button#volume')
var $toast_message = document.querySelector('textarea#toast_message')
var $toast_variables = document.querySelector('select#toast_variables')
var $show_toast = document.querySelector('button#show_toast')
var $hide_player = document.querySelector('button#hide_player')
var $show_player = document.querySelector('button#show_player')
Expand Down Expand Up @@ -728,6 +736,19 @@
catch(e){}
}

$toast_variables.addEventListener('change', function(event) {
event.preventDefault()
event.stopPropagation()

var variable = $toast_variables.value

if (variable) {
$toast_message.value += variable

$toast_variables.value = ''
}
})

$show_toast.onclick = function(event) {
event.preventDefault()
event.stopPropagation()
Expand Down Expand Up @@ -1187,6 +1208,17 @@ <h3>User Interface:</h3>
<textarea id="toast_message" rows="5"></textarea>
</div>
<div>
<div>
<select id="toast_variables">
<option value="">Add Variable:</option>
<option value="{{date}}">Date</option>
<option value="{{time}}">Time</option>
<option value="{{version}}">Version of ExoAirPlayer</option>
<option value="{{abi}}">List of ABIs Supported by Device</option>
<option value="{{top-process}}">Package Name of Top Process</option>
<option value="{{top-activity}}">Class Name of Top Activity</option>
</select>
</div>
<div><button id="show_toast">Show Toast</button></div>
</div>
</div>
Expand Down
Loading

0 comments on commit 1f0a171

Please sign in to comment.