Skip to content

Commit

Permalink
Updated UI and created buttons to request all the permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
north3221 committed Jul 19, 2022
1 parent 782a164 commit 1b82b4f
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ You also must have WifI tether on Master device
NB I use Tasker to automate both of these things i.e. turn on wifi tether when connected to car bluetooth and some screen touches for AA HUS

### SLAVE
Remove battery restrictions on AAgateway app on slave (I've added a prompt when app opens for this)
Allow storage access. Do this manually in device settings for app (TODO need to add prompt). This is for writing a log file to sdcard when logging set to full + log
Remove battery restrictions on AAgateway app on slave - Click the request battery button
Allow storage access - Click the request storage button This is for writing a log file to sdcard when logging set to full + log
Root is required - click request root button. This is to enable toggling USB to connect to car head unit when service ready
Ensure slave can connect to master wifi tether, i.e. save the network. But NO other wifi (you don't want it to connect to the wrong network)

#### Settings
* Logging Level: Prob worth changing it to full + log while setting up (shows stuff in ui and writes to sdcard/aagatewaylog.txt)
Full just shows it all in ui (ui only updates when app is open), and info just shows the key elements on screen.
* The setting control wifi means the app will turn on wifi when the slave is powered and turn it off after its has no power.
The app only waits for wifi connection, not specifically your master, hence make sure only one wifi set up
* Alternate usb toggle is if the standard one doesn't work (see later)
* Alternative usb toggle is if the standard one doesn't work (see later)

First time you try connecting you will need to allowed root access (TODO add prompt at startup)
NB it wont ask till connected to car and wifi
Expand Down
120 changes: 109 additions & 11 deletions app/src/main/java/com/north3221/aagateway/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
import static com.north3221.aagateway.ConnectionStateReceiver.SHARED_PREF_KEY_USB_CONTROL_TYPE;
import static com.north3221.aagateway.ConnectionStateReceiver.SHARED_PREF_KEY_WIFI_CONTROL;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatTextView;
Expand All @@ -32,22 +36,28 @@
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class MainActivity extends AppCompatActivity {

public static final String
TAG = "AAGateWay",
SHARED_PREF_NAME = TAG;

public static final String
SHARED_PREF_NAME = TAG,
MESSAGE_INTENT_BROADCAST = "TEXTVIEW_MESSAGE_BROADCAST",
MESSAGE_EXTRA = "MESSAGE",
MESSAGE_TV_NAME = "TEXTVIEWNAME";
MESSAGE_TV_NAME = "TEXTVIEWNAME",
SHARED_PREF_KEY_ROOT = "ISROOT";
private static final int STORAGE_PERMISSION_REQUEST_CODE = 0;

private static SharedPreferences sharedpreferences;
private static AAlogger aalogger;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -56,6 +66,8 @@ protected void onCreate(Bundle savedInstanceState) {

sharedpreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
checkBatteryOptimised();
checkRoot();
checkExternalStorage();

Button button = findViewById(R.id.exitButton);
button.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -92,7 +104,7 @@ protected void onNewIntent(Intent paramIntent) {
Intent usbIntent;
if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(paramIntent.getAction()) && paramIntent.getParcelableExtra("accessory") != null){
usbIntent = new Intent(ACTION_USB_ACCESSORY_ATTACHED);
usbIntent.putExtra("accessory",paramIntent.getParcelableExtra("accessory"));
usbIntent.putExtra("accessory", (Bundle) paramIntent.getParcelableExtra("accessory"));
} else {
usbIntent = new Intent(ACTION_USB_ACCESSORY_DETACHED);
}
Expand Down Expand Up @@ -187,14 +199,100 @@ public void onReceive(Context context, Intent intent) {

private void checkBatteryOptimised(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (pm != null && !pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
if (pm != null && !pm.isIgnoringBatteryOptimizations(getPackageName())) {
findViewById(R.id.requestGroup).setVisibility(View.VISIBLE);
final Button batteryButton = findViewById(R.id.requestBattery);
batteryButton.setVisibility(View.VISIBLE);
batteryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
batteryButton.setVisibility(View.INVISIBLE);
}
});
}
}
}

private void checkRoot(){
if (sharedpreferences.getBoolean(SHARED_PREF_KEY_ROOT,false))
return;

findViewById(R.id.requestGroup).setVisibility(View.VISIBLE);
final Button rootButton = findViewById(R.id.requestRoot);
rootButton.setVisibility(View.VISIBLE);
rootButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requstRoot();
finish();
startActivity(getIntent());
}
});
}

private void requstRoot(){
try {
String [] cmdTestSU = new String[]{"su", "-c", "ls", "/"};
Process p = Runtime.getRuntime().exec(cmdTestSU);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(p.getInputStream()));

// Grab the results
StringBuilder resRoot = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
resRoot.append(line).append("\n");
}
if (resRoot.length()>0) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(SHARED_PREF_KEY_ROOT, true);
editor.apply();
}

} catch (IOException e) {
e.printStackTrace();
Log.e(TAG,"Error getting root:", e);
}
}

private void checkExternalStorage(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
findViewById(R.id.requestGroup).setVisibility(View.VISIBLE);
final Button storageButton = findViewById(R.id.requestStorage);
storageButton.setVisibility(View.VISIBLE);
storageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requestStorate();
}
});
}
}

private void requestStorate(){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == STORAGE_PERMISSION_REQUEST_CODE) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
finish();
startActivity(getIntent());
} else {
Toast.makeText(this,"Unknown permission:= " + grantResults.toString(),Toast.LENGTH_SHORT).show();
aalogger.log("Unknown permission:= " + grantResults.toString());
}

}
}

Expand Down
130 changes: 97 additions & 33 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,82 +1,146 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_header" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/logging_level_label"
/>
<Spinner
android:id="@+id/logging"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:contentDescription=""/>
<Switch
android:id="@+id/wificontrol"
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/wifi_control_label" />

<Switch
android:id="@+id/alternateusbtoggle"
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/alternate_usb_toggle" />

<Button
android:id="@+id/exitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/exit" />


<TextView
android:id="@+id/usbconnection"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_height="wrap_content"
android:tag="USB"
android:text="" />
<TextView
android:id="@+id/wificonnection"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_height="wrap_content"
android:tag="WIFI"
android:text="" />

<TextView
android:id="@+id/aaservice"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_height="wrap_content"
android:tag="AA"
android:text="" />

<ScrollView
android:id="@+id/log_scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:scrollbars="vertical"
android:fillViewport="true">
android:layout_weight="1">

<TextView
android:id="@+id/log"
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="Log"/>
</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/logging_level_label"
android:textSize="12dp"
/>

<Spinner
android:id="@+id/logging"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription=""
android:minHeight="48dp"
android:spinnerMode="dropdown"
tools:ignore="SpeakableTextPresentCheck" />

</LinearLayout>

<Button
android:id="@+id/exitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"

android:text="@string/exit" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:visibility="gone"
android:id="@+id/requestGroup">

<Button
android:id="@+id/requestBattery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="bottom"
android:text="@string/request_battery"
android:layout_weight="1"
android:visibility="invisible"/>
<Button
android:id="@+id/requestRoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="bottom"
android:text="@string/request_root"
android:layout_weight="1"
android:visibility="invisible"/>
<Button
android:id="@+id/requestStorage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="bottom"
android:text="@string/request_storage"
android:layout_weight="1"
android:visibility="invisible"/>

</LinearLayout>

</LinearLayout>


Expand Down
Loading

0 comments on commit 1b82b4f

Please sign in to comment.