From f9d24052b7e0e5c55393f1c86b15e9f75c7fb408 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 18:19:23 +0000 Subject: [PATCH 1/9] launch the device selection in onCreate not onStart. This only launches the device selection Activity Once and if it's canceled with back doesn't try to launch again. This stops you having to hit back twice fast to exit the device selection screen. --- src/org/metawatch/manager/MetaWatch.java | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/org/metawatch/manager/MetaWatch.java b/src/org/metawatch/manager/MetaWatch.java index 8bc0c14f..0a2c9c81 100644 --- a/src/org/metawatch/manager/MetaWatch.java +++ b/src/org/metawatch/manager/MetaWatch.java @@ -149,18 +149,6 @@ protected void onCreate(Bundle savedInstanceState) { textView = MetaWatchStatus.textView; toggleButton = MetaWatchStatus.toggleButton; } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.menu, menu); - return true; - } - - @Override - protected void onStart() { - super.onStart(); MetaWatchService.loadPreferences(this); @@ -186,6 +174,19 @@ public void onClick(View v) { if (isServiceRunning() || Preferences.autoConnect) { startService(); } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.menu, menu); + return true; + } + + @Override + protected void onStart() { + super.onStart(); + } @Override From 90c1c6b63d64aee14d4544f1727d8d371ef73128 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 18:42:38 +0000 Subject: [PATCH 2/9] only start device selection if there is an adaptor. --- src/org/metawatch/manager/DeviceSelection.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/org/metawatch/manager/DeviceSelection.java b/src/org/metawatch/manager/DeviceSelection.java index 8a158558..0160fc41 100644 --- a/src/org/metawatch/manager/DeviceSelection.java +++ b/src/org/metawatch/manager/DeviceSelection.java @@ -109,6 +109,13 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; + // No point in discovering if it's switched off + BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); + if((defaultAdapter == null) || (!BluetoothAdapter.getDefaultAdapter().isEnabled())) { + finish(); + return; + } + pdWait = ProgressDialog.show(this, "Please wait", "Searching Bluetooth devices..."); pdWait.setCancelable(true); pdWait.setOnCancelListener(new OnCancelListener() { From a20ebd64619ddf2f7375dccbe2d508400a2ad96c Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 19:12:14 +0000 Subject: [PATCH 3/9] call unregisterReceiver on the receiver like a good boy --- src/org/metawatch/manager/DeviceSelection.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/org/metawatch/manager/DeviceSelection.java b/src/org/metawatch/manager/DeviceSelection.java index 0160fc41..6c1927f4 100644 --- a/src/org/metawatch/manager/DeviceSelection.java +++ b/src/org/metawatch/manager/DeviceSelection.java @@ -103,7 +103,7 @@ public void onReceive(Context context, Intent intent) { ListView listView; //static ArrayList menuList = new ArrayList(); List> list = new ArrayList>(); - + private Receiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -162,7 +162,7 @@ public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { } } - Receiver receiver = new Receiver(); + receiver = new Receiver(); IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); @@ -210,13 +210,7 @@ public void handleMessage(Message msg) { @Override protected void onDestroy() { - super.onDestroy(); + unregisterReceiver(receiver); } - - - - - - } From 7d9c107f2a60b1dcc5e4372f0f9117e653af19d8 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 19:17:25 +0000 Subject: [PATCH 4/9] Boolean -> boolean --- src/org/metawatch/manager/Protocol.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/org/metawatch/manager/Protocol.java b/src/org/metawatch/manager/Protocol.java index da2496c4..570b3eb6 100644 --- a/src/org/metawatch/manager/Protocol.java +++ b/src/org/metawatch/manager/Protocol.java @@ -151,7 +151,7 @@ public static synchronized void stopProtocolSender() { } } - public static Boolean sendLcdBitmap(Bitmap bitmap, int bufferType) { + public static boolean sendLcdBitmap(Bitmap bitmap, int bufferType) { if (bitmap==null) return false; @@ -162,7 +162,7 @@ public static Boolean sendLcdBitmap(Bitmap bitmap, int bufferType) { return sendLcdArray(pixelArray, bufferType); } - static Boolean sendLcdArray(int[] pixelArray, int bufferType) { + static boolean sendLcdArray(int[] pixelArray, int bufferType) { byte send[] = new byte[1152]; for (int i = 0; i < 1152; i++) { @@ -186,7 +186,7 @@ static Boolean sendLcdArray(int[] pixelArray, int bufferType) { return sendLcdBuffer(send, bufferType); } - static Boolean sendLcdBuffer(byte[] buffer, int bufferType) { + static boolean sendLcdBuffer(byte[] buffer, int bufferType) { if (MetaWatchService.connectionState != MetaWatchService.ConnectionState.CONNECTED) return false; @@ -703,7 +703,7 @@ public static void setNvalTime(boolean militaryTime) { enqueue(bytes); } - public static void ledChange(Boolean ledOn) { + public static void ledChange(boolean ledOn) { if (Preferences.logging) Log.d(MetaWatch.TAG, "Protocol.ledChange()"); byte[] bytes = new byte[4]; From 30ee6f8c76ec6906ef6a106868b39dd237a64935 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 19:38:06 +0000 Subject: [PATCH 5/9] hmmm. box? --- src/org/metawatch/manager/Monitors.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/org/metawatch/manager/Monitors.java b/src/org/metawatch/manager/Monitors.java index 2e5c2cca..dec355f7 100644 --- a/src/org/metawatch/manager/Monitors.java +++ b/src/org/metawatch/manager/Monitors.java @@ -379,10 +379,8 @@ private static synchronized void updateWeatherDataGoogle(Context context) { WeatherData.forecast[i].day = wfc.getDayofWeek(); if (Preferences.weatherCelsius) { - WeatherData.forecast[i].tempHigh = - Integer.toString(wfc.getTempMaxCelsius()); - WeatherData.forecast[i].tempLow = - Integer.toString(wfc.getTempMinCelsius()); + WeatherData.forecast[i].tempHigh = wfc.getTempMaxCelsius().toString(); + WeatherData.forecast[i].tempLow = wfc.getTempMinCelsius().toString(); } else { WeatherData.forecast[i].tempHigh = Integer.toString(WeatherUtils From 154678c7825df8a6cb7beb5a0e11e78b7e312437 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 19:45:13 +0000 Subject: [PATCH 6/9] clean up logic --- src/org/metawatch/manager/Utils.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/org/metawatch/manager/Utils.java b/src/org/metawatch/manager/Utils.java index 7a9064f2..694c82fe 100644 --- a/src/org/metawatch/manager/Utils.java +++ b/src/org/metawatch/manager/Utils.java @@ -115,12 +115,8 @@ public static String getContactNameFromNumber(Context context, String number) { if (c.moveToFirst()) { String name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)); - c.close(); - if (name.length() > 0) return name; - else - return number; } c.close(); From 6d9e9715b8cc6140cafe6e7bab895e2f55247307 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 19:49:24 +0000 Subject: [PATCH 7/9] if we return in a func the let next flow be clear --- src/org/metawatch/manager/MediaControl.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/org/metawatch/manager/MediaControl.java b/src/org/metawatch/manager/MediaControl.java index 343247a9..580065ea 100644 --- a/src/org/metawatch/manager/MediaControl.java +++ b/src/org/metawatch/manager/MediaControl.java @@ -136,11 +136,10 @@ public static void updateNowPlaying(Context context, String artist, String album if (artist.equals(MediaControl.lastArtist) && track.equals(MediaControl.lastTrack) && album.equals(MediaControl.lastAlbum)) { if (Preferences.logging) Log.d(MetaWatch.TAG, "updateNowPlaying(): Track info hasn't changed, ignoring"); return; - } else { - MediaControl.lastArtist = artist; - MediaControl.lastTrack = track; - MediaControl.lastAlbum = album; } + MediaControl.lastArtist = artist; + MediaControl.lastTrack = track; + MediaControl.lastAlbum = album; if(MediaControl.mediaPlayerActive) Idle.updateLcdIdle(context); From 0de788a6e17848c9ad157fb5ee9ca115fbe3d830 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 19:50:34 +0000 Subject: [PATCH 8/9] catch if is is not assigned then don't try to use it --- src/org/metawatch/manager/Monitors.java | 71 +++++++++++++------------ 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/org/metawatch/manager/Monitors.java b/src/org/metawatch/manager/Monitors.java index dec355f7..188d86a8 100644 --- a/src/org/metawatch/manager/Monitors.java +++ b/src/org/metawatch/manager/Monitors.java @@ -757,43 +757,48 @@ public static JSONObject getJSONfromURL(String url){ } //convert response to string - try{ - BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); - StringBuilder sb = new StringBuilder(); - String line = null; - while ((line = reader.readLine()) != null) { - sb.append(line + "\n"); + if (is != null) { + try { + BufferedReader reader = new BufferedReader( + new InputStreamReader(is, "iso-8859-1"), 8); + StringBuilder sb = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + sb.append(line + "\n"); + } + is.close(); + result = sb.toString(); + } catch (Exception e) { + if (Preferences.logging) + Log.e(MetaWatch.TAG, + "Error converting result " + e.toString()); } - is.close(); - result=sb.toString(); - }catch(Exception e){ - if (Preferences.logging) Log.e(MetaWatch.TAG, "Error converting result "+e.toString()); - } - //dump to sdcard for debugging - File sdCard = Environment.getExternalStorageDirectory(); - File file = new File(sdCard, "weather.json"); + // dump to sdcard for debugging + File sdCard = Environment.getExternalStorageDirectory(); + File file = new File(sdCard, "weather.json"); + + try { + FileWriter writer = new FileWriter(file); + writer.append(result); + writer.flush(); + writer.close(); + } catch (FileNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - try { - FileWriter writer = new FileWriter(file); - writer.append(result); - writer.flush(); - writer.close(); - } catch (FileNotFoundException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - //try parse the string to a JSON object - try{ - jArray = new JSONObject(result); - }catch(JSONException e){ - if (Preferences.logging) Log.e(MetaWatch.TAG, "Error parsing data "+e.toString()); + // try parse the string to a JSON object + try { + jArray = new JSONObject(result); + } catch (JSONException e) { + if (Preferences.logging) + Log.e(MetaWatch.TAG, "Error parsing data " + e.toString()); + } } - return jArray; } From efaec034548e6d305efc41743f49904d84d563f4 Mon Sep 17 00:00:00 2001 From: Garth Bushell Date: Fri, 9 Mar 2012 20:28:56 +0000 Subject: [PATCH 9/9] reuse view or we end up running out of memory. I think this is a combination of icons being 96*96 on ICS and me having 200+ apps installed. I was getting out of memory if I open up blacklist and scrolled to the end. --- src/org/metawatch/manager/AppBlacklist.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/org/metawatch/manager/AppBlacklist.java b/src/org/metawatch/manager/AppBlacklist.java index cbde9862..68b01d36 100644 --- a/src/org/metawatch/manager/AppBlacklist.java +++ b/src/org/metawatch/manager/AppBlacklist.java @@ -86,8 +86,7 @@ protected void onPostExecute(List appInfos) { ListView listView = (ListView) findViewById(android.R.id.list); // listView.setAdapter(new ArrayAdapter(this, // android.R.layout.simple_list_item_1, menuList)); - listView.setAdapter(new BlacklistAdapter(AppBlacklist.this, - appInfos)); + listView.setAdapter(new BlacklistAdapter(appInfos)); AppBlacklist.this.appInfos = appInfos; pdWait.dismiss(); @@ -107,20 +106,20 @@ public int compareTo(AppInfo another) { } class BlacklistAdapter extends ArrayAdapter { - private final Activity context; private final List apps; - public BlacklistAdapter(Activity context, List apps) { - super(context, R.layout.app_blacklist_list_item, apps); - this.context = context; + public BlacklistAdapter( List apps) { + super(AppBlacklist.this, R.layout.app_blacklist_list_item, apps); this.apps = apps; } @Override public View getView(int position, View convertView, ViewGroup parent) { - View view = null; - LayoutInflater inflater = context.getLayoutInflater(); - view = inflater.inflate(R.layout.app_blacklist_list_item, null); + View view = convertView; + LayoutInflater inflater = AppBlacklist.this.getLayoutInflater(); + if(view == null) { + view = inflater.inflate(R.layout.app_blacklist_list_item, null); + } ImageView icon = (ImageView) view .findViewById(R.id.app_blacklist_list_item_icon); TextView appName = (TextView) view