1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.android.car.obd2app; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.content.SharedPreferences; 22 import android.content.pm.ActivityInfo; 23 import android.os.Bundle; 24 import android.preference.PreferenceManager; 25 import android.text.TextUtils; 26 import android.util.Log; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.widget.Button; 30 import android.widget.TextView; 31 import java.util.Timer; 32 33 public class MainActivity extends Activity implements StatusNotification { 34 public static final String TAG = MainActivity.class.getSimpleName(); 35 36 private static final String BLUETOOTH_MAC_PREFERENCE_ID = "bluetooth_mac"; 37 private static final String SCAN_DELAY_PREFERENCE_ID = "scan_delay"; 38 39 private Obd2CollectionTask mCollectionTask = null; 40 private final Timer mTimer = new Timer("com.google.android.car.obd2app.collection"); 41 getBluetoothDongleMacFromPreferences(String defaultValue)42 private String getBluetoothDongleMacFromPreferences(String defaultValue) { 43 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this); 44 return appPreferences.getString(BLUETOOTH_MAC_PREFERENCE_ID, defaultValue); 45 } 46 getScanDelayFromPreferences(int defaultValue)47 private int getScanDelayFromPreferences(int defaultValue) { 48 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this); 49 return appPreferences.getInt(SCAN_DELAY_PREFERENCE_ID, defaultValue); 50 } 51 52 @Override onCreate(Bundle savedInstanceState)53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.activity_main); 56 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 57 String bluetoothDongleMac = getBluetoothDongleMacFromPreferences(""); 58 if (TextUtils.isEmpty(bluetoothDongleMac)) { 59 notifyNoDongle(); 60 } else { 61 notifyPaired(bluetoothDongleMac); 62 } 63 findViewById(R.id.connection) 64 .setOnClickListener( 65 new OnClickListener() { 66 @Override 67 public void onClick(View v) { 68 handleConnection(v); 69 } 70 }); 71 Log.i(TAG, "I did all the things"); 72 } 73 stopConnection()74 private void stopConnection() { 75 mCollectionTask.cancel(); 76 mTimer.purge(); 77 mCollectionTask = null; 78 } 79 80 @Override onDestroy()81 protected void onDestroy() { 82 stopConnection(); 83 } 84 doSettings(View view)85 public void doSettings(View view) { 86 Intent launchSettings = new Intent(this, SettingsActivity.class); 87 startActivity(launchSettings); 88 } 89 90 @Override notify(String status)91 public void notify(String status) { 92 Log.i(TAG, status); 93 runOnUiThread(() -> ((TextView) findViewById(R.id.statusBar)).setText(status)); 94 } 95 handleConnection(View view)96 public void handleConnection(View view) { 97 String deviceAddress = getBluetoothDongleMacFromPreferences(""); 98 Log.i(TAG, "Considering a connection to " + deviceAddress); 99 if (TextUtils.isEmpty(deviceAddress)) { 100 notifyNoDongle(); 101 } 102 if (mCollectionTask == null) { 103 mCollectionTask = Obd2CollectionTask.create(this, this, deviceAddress); 104 if (null == mCollectionTask) { 105 notifyConnectionFailed(); 106 return; 107 } 108 final int delay = 1000 * getScanDelayFromPreferences(2); 109 mTimer.scheduleAtFixedRate(mCollectionTask, delay, delay); 110 ((Button) view).setText("Disconnect"); 111 } else { 112 stopConnection(); 113 ((Button) view).setText("Connect"); 114 } 115 } 116 } 117