From 0119e703ff4a8f3201610c2866702f7f92da0b26 Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 26 Jul 2020 18:11:10 +0200 Subject: initial import --- .gitignore | 14 ++ .idea/codeStyles/Project.xml | 116 ++++++++++++++ .idea/compiler.xml | 6 + .idea/gradle.xml | 22 +++ .idea/jarRepositories.xml | 25 +++ .idea/misc.xml | 48 ++++++ .idea/runConfigurations.xml | 12 ++ .idea/vcs.xml | 6 + app/.gitignore | 1 + app/build.gradle | 33 ++++ app/proguard-rules.pro | 21 +++ app/src/main/AndroidManifest.xml | 31 ++++ .../autobluetether/BtConnectReceiver.java | 46 ++++++ .../autobluetether/BtDeviceEnabledSetAdapter.java | 54 +++++++ .../BtDeviceRecyclerViewAdapter.java | 96 ++++++++++++ .../javispedro/autobluetether/BtPanWrapper.java | 39 +++++ .../javispedro/autobluetether/BtTetherEnabler.java | 53 +++++++ .../javispedro/autobluetether/DeviceFragment.java | 92 +++++++++++ .../javispedro/autobluetether/MainActivity.java | 68 ++++++++ .../autobluetether/SystemSettingsFragment.java | 34 ++++ .../res/drawable-v24/ic_launcher_foreground.xml | 30 ++++ .../main/res/drawable/ic_launcher_background.xml | 170 ++++++++++++++++++++ app/src/main/res/drawable/ic_twotone_error_24.xml | 8 + app/src/main/res/layout/activity_main.xml | 40 +++++ app/src/main/res/layout/fragment_device.xml | 51 ++++++ app/src/main/res/layout/fragment_device_list.xml | 13 ++ .../main/res/layout/fragment_system_settings.xml | 54 +++++++ app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../res/mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3593 bytes app/src/main/res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5339 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2636 bytes app/src/main/res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 3388 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4926 bytes .../main/res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7472 bytes app/src/main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7909 bytes .../main/res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 11873 bytes app/src/main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10652 bytes .../main/res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 16570 bytes app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 4 + app/src/main/res/values/strings.xml | 10 ++ app/src/main/res/values/styles.xml | 10 ++ build.gradle | 24 +++ gradle.properties | 19 +++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 +++++++++++++++++++++ gradlew.bat | 84 ++++++++++ settings.gradle | 2 + 50 files changed, 1530 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/javispedro/autobluetether/BtConnectReceiver.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/BtDeviceEnabledSetAdapter.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/BtDeviceRecyclerViewAdapter.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/BtPanWrapper.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/DeviceFragment.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/MainActivity.java create mode 100644 app/src/main/java/com/javispedro/autobluetether/SystemSettingsFragment.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/drawable/ic_twotone_error_24.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/fragment_device.xml create mode 100644 app/src/main/res/layout/fragment_device_list.xml create mode 100644 app/src/main/res/layout/fragment_system_settings.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..603b140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..23a89bb --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..a5f05cd --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1d653f7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..d784c17 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 29 + + defaultConfig { + applicationId "com.javispedro.autobluetether" + minSdkVersion 23 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: "libs", include: ["*.jar"]) + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation 'androidx.recyclerview:recyclerview:1.1.0' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8ab94ac --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/javispedro/autobluetether/BtConnectReceiver.java b/app/src/main/java/com/javispedro/autobluetether/BtConnectReceiver.java new file mode 100644 index 0000000..680f4fc --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/BtConnectReceiver.java @@ -0,0 +1,46 @@ +package com.javispedro.autobluetether; + +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; + +import java.util.Set; + +public class BtConnectReceiver extends BroadcastReceiver { + private static final String TAG = "BtConnectReceiver"; + + private boolean isDeviceEnabled(Context context, BluetoothDevice device) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + Set enabledAddresses = prefs.getStringSet(context.getString(R.string.prefs_key_device_list), null); + if (enabledAddresses == null) return false; + Log.d(TAG, "enabled devices in prefs: " + enabledAddresses); + return enabledAddresses.contains(device.getAddress()); + } + + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + if (action == null) return; + + if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) { + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + if (device == null) return; + + Log.d(TAG, "ACL connected: " + device.getName()); + + if (isDeviceEnabled(context, device)) { + Log.d(TAG, "trying to enable tethering"); + try { + new BtTetherEnabler(context).startTethering(); + } catch (BtTetherEnabler.EnableException e) { + e.printStackTrace(); + Log.e(TAG, "Can't enable tethering: " + e.getMessage()); + } + } + } + } +} diff --git a/app/src/main/java/com/javispedro/autobluetether/BtDeviceEnabledSetAdapter.java b/app/src/main/java/com/javispedro/autobluetether/BtDeviceEnabledSetAdapter.java new file mode 100644 index 0000000..7d2c8fc --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/BtDeviceEnabledSetAdapter.java @@ -0,0 +1,54 @@ +package com.javispedro.autobluetether; + +import android.bluetooth.BluetoothDevice; +import android.content.SharedPreferences; +import android.util.ArraySet; +import android.util.Log; + +import java.util.Set; + +public class BtDeviceEnabledSetAdapter { + private final static String TAG = "BtDeviceEnabledSetAd"; + + private final SharedPreferences mPrefs; + private final String mPrefsKey; + private final ArraySet mSet = new ArraySet<>(); + + public BtDeviceEnabledSetAdapter(SharedPreferences prefs, String key) { + mPrefs = prefs; + mPrefsKey = key; + + readPrefs(); + } + + public boolean contains(BluetoothDevice device) { + return mSet.contains(device.getAddress()); + } + + public void set(BluetoothDevice device, boolean enabled) { + String address = device.getAddress(); + boolean currently = mSet.contains(address); + if (enabled && !currently) { + mSet.add(address); + savePrefs(); + } else if (currently && !enabled) { + mSet.remove(address); + savePrefs(); + } + } + + private void readPrefs() { + mSet.clear(); + Set prefsSet = mPrefs.getStringSet(mPrefsKey, null); + if (prefsSet != null) { + mSet.addAll(prefsSet); + } + } + + private void savePrefs() { + SharedPreferences.Editor editor = mPrefs.edit(); + editor.putStringSet(mPrefsKey, mSet); + editor.apply(); + Log.d(TAG, "saved " + mPrefsKey + ":" + mSet); + } +} diff --git a/app/src/main/java/com/javispedro/autobluetether/BtDeviceRecyclerViewAdapter.java b/app/src/main/java/com/javispedro/autobluetether/BtDeviceRecyclerViewAdapter.java new file mode 100644 index 0000000..4515a95 --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/BtDeviceRecyclerViewAdapter.java @@ -0,0 +1,96 @@ +package com.javispedro.autobluetether; + +import android.bluetooth.BluetoothDevice; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +/** + * {@link RecyclerView.Adapter} that can display a list of {@link BluetoothDevice}, + * checkboxes show whether the device is in "enabled set" or not + */ +public class BtDeviceRecyclerViewAdapter extends RecyclerView.Adapter { + private final static String TAG = "BtDeviceRecyclerViewAd"; + + private List mValues = new ArrayList<>(); + private BtDeviceEnabledSetAdapter mEnabledAdapter; + + public void setValues(@NonNull List values) { + mValues = values; + Log.d(TAG, "values size:" + values.size()); + notifyDataSetChanged(); + } + + public void setBtDeviceEnabledSetAdapter(@NonNull BtDeviceEnabledSetAdapter adapter) { + mEnabledAdapter = adapter; + notifyDataSetChanged(); + } + + @NonNull + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.fragment_device, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(final ViewHolder holder, int position) { + holder.setItem(mValues.get(position)); + } + + @Override + public int getItemCount() { + return mValues.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + private final View mView; + private final TextView mNameView; + private final TextView mAddressView; + private final CheckBox mCheckBox; + private BluetoothDevice mItem; + + public ViewHolder(View view) { + super(view); + mView = view; + mNameView = view.findViewById(R.id.name); + mAddressView = view.findViewById(R.id.address); + mCheckBox = view.findViewById(R.id.checked); + mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (mEnabledAdapter != null && mItem != null) { + Log.d(TAG, "onCheckedChanged " + mItem.getName() + " " + isChecked); + mEnabledAdapter.set(mItem, isChecked); + } + } + }); + } + + public void setItem(BluetoothDevice device) { + mItem = device; + mNameView.setText(device.getName()); + mAddressView.setText(device.getAddress()); + if (mEnabledAdapter != null) { + mCheckBox.setChecked(mEnabledAdapter.contains(device)); + } + } + + @NonNull + @Override + public String toString() { + return super.toString() + " [" + mItem.getAddress() + "]"; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/javispedro/autobluetether/BtPanWrapper.java b/app/src/main/java/com/javispedro/autobluetether/BtPanWrapper.java new file mode 100644 index 0000000..40cd84a --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/BtPanWrapper.java @@ -0,0 +1,39 @@ +package com.javispedro.autobluetether; + +import android.bluetooth.BluetoothProfile; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class BtPanWrapper { + private static final String CLASS_NAME = "android.bluetooth.BluetoothPan"; + public static final int PROFILE_ID = 5 /*BluetoothProfile.PAN*/; + private final BluetoothProfile p; + + public BtPanWrapper(BluetoothProfile pan) { + p = pan; + } + + private Object invoke(String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + Class c = Class.forName(CLASS_NAME); + Method m = c.getDeclaredMethod(methodName, parameterTypes); + return m.invoke(this.p, args); + } + + public boolean isTetheringOn() { + try { + return (boolean) invoke("isTetheringOn", null, null); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + public void setBluetoothTethering(boolean on) { + try { + invoke("setBluetoothTethering", new Class[] {Boolean.TYPE}, new Object[] {on}); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java b/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java new file mode 100644 index 0000000..bae42c8 --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java @@ -0,0 +1,53 @@ +package com.javispedro.autobluetether; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothProfile; +import android.content.Context; +import android.content.ContextWrapper; +import android.util.Log; + +public class BtTetherEnabler extends ContextWrapper { + private final static String TAG = "BtTetherEnabler"; + + public BtTetherEnabler(Context context) { + super(context); + } + + public static class EnableException extends Exception { + public EnableException(String message, Throwable cause) { + super(message, cause); + } + + public EnableException(String message) { + super(message); + } + } + + public void startTethering() throws EnableException { + final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if (adapter == null) { + Log.e(TAG, "no BT adapter"); + throw new EnableException("No Bluetooth adapter"); + } + + adapter.getProfileProxy(getBaseContext(), new BluetoothProfile.ServiceListener() { + public void onServiceConnected(int profile, BluetoothProfile proxy) { + Log.d(TAG, "onServiceConnected"); + final BtPanWrapper wrapper = new BtPanWrapper(proxy); + boolean is_on = wrapper.isTetheringOn(); + Log.d(TAG, "isTetheringOn: " + is_on); + if (!is_on) { + Log.d(TAG, "trying to turn on"); + wrapper.setBluetoothTethering(true); + } + adapter.closeProfileProxy(profile, proxy); + } + + public void onServiceDisconnected(int profile) { + Log.d(TAG, "onServiceDisconnected"); + } + }, BtPanWrapper.PROFILE_ID); + } + + +} diff --git a/app/src/main/java/com/javispedro/autobluetether/DeviceFragment.java b/app/src/main/java/com/javispedro/autobluetether/DeviceFragment.java new file mode 100644 index 0000000..2f8099c --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/DeviceFragment.java @@ -0,0 +1,92 @@ +package com.javispedro.autobluetether; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +/** + * A fragment representing a list of Items. + */ +public class DeviceFragment extends Fragment { + private RecyclerView mRecyclerView; + private BtDeviceRecyclerViewAdapter mAdapter; + private BtDeviceEnabledSetAdapter mEnabledAdapter; + + private static final String ARG_COLUMN_COUNT = "column-count"; + private int mColumnCount = 1; + + /** + * Mandatory empty constructor for the fragment manager to instantiate the + * fragment (e.g. upon screen orientation changes). + */ + public DeviceFragment() { + } + + @SuppressWarnings("unused") + public static DeviceFragment newInstance(int columnCount) { + DeviceFragment fragment = new DeviceFragment(); + Bundle args = new Bundle(); + args.putInt(ARG_COLUMN_COUNT, columnCount); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (getArguments() != null) { + mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_device_list, container, false); + mRecyclerView = (RecyclerView) view; + + Context context = view.getContext(); + + if (mColumnCount <= 1) { + mRecyclerView.setLayoutManager(new LinearLayoutManager(context)); + } else { + mRecyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); + } + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + mEnabledAdapter = new BtDeviceEnabledSetAdapter(prefs, getString(R.string.prefs_key_device_list)); + + mAdapter = new BtDeviceRecyclerViewAdapter(); + mAdapter.setBtDeviceEnabledSetAdapter(mEnabledAdapter); + mRecyclerView.setAdapter(mAdapter); + + return mRecyclerView; + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mRecyclerView = null; + mAdapter = null; + } + + public void refreshList() { + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + ArrayList devices = new ArrayList<>(adapter.getBondedDevices()); + mAdapter.setValues(devices); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/javispedro/autobluetether/MainActivity.java b/app/src/main/java/com/javispedro/autobluetether/MainActivity.java new file mode 100644 index 0000000..079985c --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/MainActivity.java @@ -0,0 +1,68 @@ +package com.javispedro.autobluetether; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.util.Log; +import android.view.View; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; + +public class MainActivity extends AppCompatActivity { + private final static String TAG = "MainActivity"; + + private SystemSettingsFragment systemSettingsFragment; + private DeviceFragment deviceFragment; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + FragmentManager manager = getSupportFragmentManager(); + systemSettingsFragment = (SystemSettingsFragment) manager.findFragmentById(R.id.fragmentSystemSettings); + deviceFragment = (DeviceFragment) manager.findFragmentById(R.id.fragmentDeviceList); + } + + @Override + protected void onResume() { + super.onResume(); + refreshSystemSettingsFragmentVisible(); + refreshDeviceList(); + } + + @Override + protected void onDestroy() { + deviceFragment = null; + systemSettingsFragment = null; + super.onDestroy(); + } + + private void refreshSystemSettingsFragmentVisible() { + boolean can_write_settings = Settings.System.canWrite(this); + Log.d(TAG, "can_write_settings: " + can_write_settings); + setSystemSettingsFragmentVisible(!can_write_settings); + } + + private void refreshDeviceList() { + deviceFragment.refreshList(); + } + + private void setSystemSettingsFragmentVisible(boolean visible) { + FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); + if (visible) { + tx.show(systemSettingsFragment); + } else { + tx.hide(systemSettingsFragment); + } + tx.commit(); + } + + public void manageWriteSettings(View view) { + Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); + intent.setData(Uri.parse("package:" + getPackageName())); + startActivity(intent); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/javispedro/autobluetether/SystemSettingsFragment.java b/app/src/main/java/com/javispedro/autobluetether/SystemSettingsFragment.java new file mode 100644 index 0000000..a97f8c8 --- /dev/null +++ b/app/src/main/java/com/javispedro/autobluetether/SystemSettingsFragment.java @@ -0,0 +1,34 @@ +package com.javispedro.autobluetether; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.fragment.app.Fragment; + +public class SystemSettingsFragment extends Fragment { + public SystemSettingsFragment() { + // Required empty public constructor + } + + public static SystemSettingsFragment newInstance() { + SystemSettingsFragment fragment = new SystemSettingsFragment(); + Bundle args = new Bundle(); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_system_settings, container, false); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_twotone_error_24.xml b/app/src/main/res/drawable/ic_twotone_error_24.xml new file mode 100644 index 0000000..e52175d --- /dev/null +++ b/app/src/main/res/drawable/ic_twotone_error_24.xml @@ -0,0 +1,8 @@ + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..f2896a6 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,40 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_device.xml b/app/src/main/res/layout/fragment_device.xml new file mode 100644 index 0000000..f02b1d3 --- /dev/null +++ b/app/src/main/res/layout/fragment_device.xml @@ -0,0 +1,51 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_device_list.xml b/app/src/main/res/layout/fragment_device_list.xml new file mode 100644 index 0000000..8b10502 --- /dev/null +++ b/app/src/main/res/layout/fragment_device_list.xml @@ -0,0 +1,13 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_system_settings.xml b/app/src/main/res/layout/fragment_system_settings.xml new file mode 100644 index 0000000..e5936b9 --- /dev/null +++ b/app/src/main/res/layout/fragment_system_settings.xml @@ -0,0 +1,54 @@ + + + + + + + +