|
|
|
@ -18,7 +18,7 @@ package androidx.car.app.connection;
|
|
|
|
|
|
|
|
|
|
import static androidx.car.app.connection.CarConnection.ACTION_CAR_CONNECTION_UPDATED;
|
|
|
|
|
import static androidx.car.app.connection.CarConnection.CAR_CONNECTION_STATE;
|
|
|
|
|
//import static androidx.car.app.utils.LogTags.TAG_CONNECTION_TO_CAR;
|
|
|
|
|
import static androidx.car.app.utils.LogTags.TAG_CONNECTION_TO_CAR;
|
|
|
|
|
|
|
|
|
|
import android.content.AsyncQueryHandler;
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
@ -28,8 +28,11 @@ import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.DoNotInline;
|
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
|
import androidx.car.app.connection.CarConnection.ConnectionType;
|
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
@ -60,8 +63,15 @@ final class CarConnectionTypeLiveData extends LiveData<@ConnectionType Integer>
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onActive() {
|
|
|
|
|
mContext.registerReceiver(mBroadcastReceiver,
|
|
|
|
|
new IntentFilter(ACTION_CAR_CONNECTION_UPDATED));
|
|
|
|
|
// TODO(b/240576633): Replace this entire if-block with a call to
|
|
|
|
|
// ContextCompat#registerReceiver once it's released in androidx.core
|
|
|
|
|
IntentFilter filter = new IntentFilter(ACTION_CAR_CONNECTION_UPDATED);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
|
Api33Impl.registerExportedReceiver(mContext, mBroadcastReceiver, filter);
|
|
|
|
|
} else {
|
|
|
|
|
mContext.registerReceiver(mBroadcastReceiver, filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryForState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -86,23 +96,23 @@ final class CarConnectionTypeLiveData extends LiveData<@ConnectionType Integer>
|
|
|
|
|
@Override
|
|
|
|
|
protected void onQueryComplete(int token, Object cookie, Cursor response) {
|
|
|
|
|
if (response == null) {
|
|
|
|
|
//Log.w(TAG_CONNECTION_TO_CAR, "Null response from content provider when checking "
|
|
|
|
|
// + "connection to the car, treating as disconnected");
|
|
|
|
|
Log.w(TAG_CONNECTION_TO_CAR, "Null response from content provider when checking "
|
|
|
|
|
+ "connection to the car, treating as disconnected");
|
|
|
|
|
postValue(CarConnection.CONNECTION_TYPE_NOT_CONNECTED);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int carConnectionTypeColumn = response.getColumnIndex(CAR_CONNECTION_STATE);
|
|
|
|
|
if (carConnectionTypeColumn < 0) {
|
|
|
|
|
//Log.e(TAG_CONNECTION_TO_CAR, "Connection to car response is missing the "
|
|
|
|
|
// + "connection type, treating as disconnected");
|
|
|
|
|
Log.e(TAG_CONNECTION_TO_CAR, "Connection to car response is missing the "
|
|
|
|
|
+ "connection type, treating as disconnected");
|
|
|
|
|
postValue(CarConnection.CONNECTION_TYPE_NOT_CONNECTED);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!response.moveToNext()) {
|
|
|
|
|
//Log.e(TAG_CONNECTION_TO_CAR, "Connection to car response is empty, treating as "
|
|
|
|
|
// + "disconnected");
|
|
|
|
|
Log.e(TAG_CONNECTION_TO_CAR, "Connection to car response is empty, treating as "
|
|
|
|
|
+ "disconnected");
|
|
|
|
|
postValue(CarConnection.CONNECTION_TYPE_NOT_CONNECTED);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -117,4 +127,17 @@ final class CarConnectionTypeLiveData extends LiveData<@ConnectionType Integer>
|
|
|
|
|
queryForState();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(33)
|
|
|
|
|
static class Api33Impl {
|
|
|
|
|
private Api33Impl() {
|
|
|
|
|
// Not instantiable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DoNotInline
|
|
|
|
|
static void registerExportedReceiver(Context context, BroadcastReceiver broadcastReceiver,
|
|
|
|
|
IntentFilter intentFilter) {
|
|
|
|
|
context.registerReceiver(broadcastReceiver, intentFilter, Context.RECEIVER_EXPORTED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|