From bc773ad44d5c7a5272a41c1367e421562d40e173 Mon Sep 17 00:00:00 2001 From: Keith Conger Date: Thu, 12 Mar 2015 11:29:12 -0600 Subject: [PATCH] * Revert: Fix a potential handler leak * Disable preference when no sensor is found --- .../org/thecongers/mtpms/IStaticHandler.java | 24 ---- .../org/thecongers/mtpms/MainActivity.java | 9 +- .../mtpms/StaticHandlerFactory.java | 47 ------- .../thecongers/mtpms/UserSettingActivity.java | 1 + .../res/layout-small-land/activity_main.xml | 129 ----------------- .../main/res/layout-small/activity_main.xml | 129 ----------------- .../activity_main.xml | 0 .../activity_main.xml | 0 .../res/layout-xlarge-land/activity_main.xml | 132 ------------------ .../main/res/layout-xlarge/activity_main.xml | 129 ----------------- 10 files changed, 4 insertions(+), 596 deletions(-) delete mode 100644 app/src/main/java/org/thecongers/mtpms/IStaticHandler.java delete mode 100644 app/src/main/java/org/thecongers/mtpms/StaticHandlerFactory.java delete mode 100644 app/src/main/res/layout-small-land/activity_main.xml delete mode 100644 app/src/main/res/layout-small/activity_main.xml rename app/src/main/res/{layout-large-land => layout-sw400dp-land}/activity_main.xml (100%) rename app/src/main/res/{layout-large => layout-sw50dp}/activity_main.xml (100%) delete mode 100644 app/src/main/res/layout-xlarge-land/activity_main.xml delete mode 100644 app/src/main/res/layout-xlarge/activity_main.xml diff --git a/app/src/main/java/org/thecongers/mtpms/IStaticHandler.java b/app/src/main/java/org/thecongers/mtpms/IStaticHandler.java deleted file mode 100644 index 982cbfe..0000000 --- a/app/src/main/java/org/thecongers/mtpms/IStaticHandler.java +++ /dev/null @@ -1,24 +0,0 @@ -/* -Copyright (C) 2014 Keith Conger - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.thecongers.mtpms; - -import android.os.Message; - -public interface IStaticHandler { - void handleMessage(Message msg); -} diff --git a/app/src/main/java/org/thecongers/mtpms/MainActivity.java b/app/src/main/java/org/thecongers/mtpms/MainActivity.java index f91871f..654fee2 100644 --- a/app/src/main/java/org/thecongers/mtpms/MainActivity.java +++ b/app/src/main/java/org/thecongers/mtpms/MainActivity.java @@ -100,8 +100,8 @@ public class MainActivity extends ActionBarActivity { static boolean hasSensor = false; static SensorIdDatabase sensorDB; private LogData logger = null; + private Handler sensorMessages; private ConnectThread btConnectThread; - private Handler handler; @SuppressLint("HandlerLeak") @Override @@ -204,8 +204,7 @@ public void onCreate(Bundle savedInstanceState) { new AlertDialog.Builder(this).setTitle(R.string.alert_setup_title).setMessage(R.string.alert_setup_message).setNeutralButton(R.string.alert_setup_button, null).show(); } - IStaticHandler sensorMessages = new IStaticHandler() { - @Override + sensorMessages = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case RECEIVE_MESSAGE: @@ -420,7 +419,6 @@ public void handleMessage(Message msg) { } } }; - handler = StaticHandlerFactory.create(sensorMessages); // Light Sensor Stuff SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); @@ -778,8 +776,7 @@ public void run() { try { // Read from the InputStream bytes = btInStream.read(buffer); // Get number of bytes and message in "buffer" - - handler.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler + sensorMessages.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler } catch (IOException e) { Log.d(TAG, "IO Exception while reading stream"); btConnectThread.cancel(); diff --git a/app/src/main/java/org/thecongers/mtpms/StaticHandlerFactory.java b/app/src/main/java/org/thecongers/mtpms/StaticHandlerFactory.java deleted file mode 100644 index 5aef10f..0000000 --- a/app/src/main/java/org/thecongers/mtpms/StaticHandlerFactory.java +++ /dev/null @@ -1,47 +0,0 @@ -/* -Copyright (C) 2014 Keith Conger - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.thecongers.mtpms; - -import android.os.Handler; -import android.os.Message; -import java.lang.ref.WeakReference; - -public class StaticHandlerFactory { - - public static StaticHandler create(IStaticHandler ref) { - return new StaticHandler(ref); - } - - // This has to be nested. - static class StaticHandler extends Handler { - WeakReference weakRef; - - public StaticHandler(IStaticHandler ref) { - this.weakRef = new WeakReference(ref); - } - - @Override - public void handleMessage(Message msg) { - if (weakRef.get() == null) { - throw new RuntimeException("Something goes wrong."); - } else { - weakRef.get().handleMessage(msg); - } - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/thecongers/mtpms/UserSettingActivity.java b/app/src/main/java/org/thecongers/mtpms/UserSettingActivity.java index 3b0f27b..6812b9c 100644 --- a/app/src/main/java/org/thecongers/mtpms/UserSettingActivity.java +++ b/app/src/main/java/org/thecongers/mtpms/UserSettingActivity.java @@ -44,6 +44,7 @@ public void onCreate(final Bundle savedInstanceState) // Disable auto night mode option when no sensor is found if (!MainActivity.hasSensor){ getPreferenceScreen().findPreference("prefAutoNightMode").setEnabled(false); + getPreferenceScreen().findPreference("prefAutoNightModeDelay").setEnabled(false); } // Dynamically populate sensorID lists final ListPreference listFrontIDPreference = (ListPreference) findPreference("prefFrontID"); diff --git a/app/src/main/res/layout-small-land/activity_main.xml b/app/src/main/res/layout-small-land/activity_main.xml deleted file mode 100644 index 1cfe1d9..0000000 --- a/app/src/main/res/layout-small-land/activity_main.xml +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout-small/activity_main.xml b/app/src/main/res/layout-small/activity_main.xml deleted file mode 100644 index 5d97af7..0000000 --- a/app/src/main/res/layout-small/activity_main.xml +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout-large-land/activity_main.xml b/app/src/main/res/layout-sw400dp-land/activity_main.xml similarity index 100% rename from app/src/main/res/layout-large-land/activity_main.xml rename to app/src/main/res/layout-sw400dp-land/activity_main.xml diff --git a/app/src/main/res/layout-large/activity_main.xml b/app/src/main/res/layout-sw50dp/activity_main.xml similarity index 100% rename from app/src/main/res/layout-large/activity_main.xml rename to app/src/main/res/layout-sw50dp/activity_main.xml diff --git a/app/src/main/res/layout-xlarge-land/activity_main.xml b/app/src/main/res/layout-xlarge-land/activity_main.xml deleted file mode 100644 index 4cb8eea..0000000 --- a/app/src/main/res/layout-xlarge-land/activity_main.xml +++ /dev/null @@ -1,132 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout-xlarge/activity_main.xml b/app/src/main/res/layout-xlarge/activity_main.xml deleted file mode 100644 index 5d97af7..0000000 --- a/app/src/main/res/layout-xlarge/activity_main.xml +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - -