Skip to content

Commit c769b79

Browse files
author
davidgraeff
committed
Fix crashes in all outlets window while switching and if more than one widget
1 parent ea6e466 commit c769b79

File tree

7 files changed

+216
-331
lines changed

7 files changed

+216
-331
lines changed

.idea/workspace.xml

Lines changed: 195 additions & 311 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="oly.netpowerctrl"
4-
android:versionCode="8"
5-
android:versionName="1.5">
4+
android:versionCode="10"
5+
android:versionName="1.5b">
66

7-
<!-- Api level 16: Android 4.1 27. Juni 2012 -->
7+
<!-- Api level 16: Android 4.1 27. Juni 2012 -->
88
<uses-sdk
99
android:minSdkVersion="16"
1010
android:targetSdkVersion="17" />

app/src/main/java/oly/netpowerctrl/listadapter/OutletSwitchListAdapter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class OutletSwitchListAdapter extends BaseAdapter implements ListAdapter, OnCheckedChangeListener, DevicesUpdate {
2626
private static class OutletInfoAdditional {
27-
OutletInfo oi;
27+
public OutletInfo oi;
2828
String displayText;
2929

3030
OutletInfoAdditional(OutletInfo oi, boolean showDevice) {
@@ -54,18 +54,21 @@ public OutletSwitchListAdapter(Context context) {
5454
onDevicesUpdated();
5555
}
5656

57+
@Override
5758
public int getCount() {
5859
return all_outlets.size();
5960
}
6061

61-
public Object getItem(int position) {
62-
return all_outlets.get(position);
62+
public OutletInfo getItem(int position) {
63+
return all_outlets.get(position).oi;
6364
}
6465

66+
@Override
6567
public long getItemId(int position) {
6668
return position;
6769
}
6870

71+
@Override
6972
public View getView(int position, View convertView, ViewGroup parent) {
7073
if (convertView == null) {
7174
convertView = inflater.inflate(R.layout.outlet_list_switch, null);
@@ -106,7 +109,7 @@ public void onCheckedChanged(CompoundButton arg0, boolean new_state) {
106109
if (position == -1)
107110
return;
108111
arg0.setEnabled(false);
109-
OutletInfo oi = (OutletInfo) getItem(position);
112+
OutletInfo oi = getItem(position);
110113
oi.Disabled = false;
111114
ash.setData(position, new_state);
112115
ash.removeMessages();

app/src/main/java/oly/netpowerctrl/main/NetpowerctrlApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.util.ArrayList;
1111
import java.util.Collection;
12+
import java.util.Collections;
1213
import java.util.HashSet;
1314
import java.util.Iterator;
1415
import java.util.List;
@@ -41,7 +42,7 @@ public class NetpowerctrlApplication extends Application implements DeviceUpdate
4142
private ArrayList<DevicesUpdate> observersNew = new ArrayList<DevicesUpdate>();
4243
private ArrayList<ServiceReady> observersServiceReady = new ArrayList<ServiceReady>();
4344

44-
private ArrayList<DeviceQuery> updateDeviceStateList = new ArrayList<DeviceQuery>();
45+
private List<DeviceQuery> updateDeviceStateList = Collections.synchronizedList(new ArrayList<DeviceQuery>());
4546

4647
//! get a list of all send ports of all configured scenes plus the default send port
4748
public ArrayList<Integer> getAllSendPorts() {

app/src/main/java/oly/netpowerctrl/utils/AfterSentHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void handleMessage(Message m) {
4444
return;
4545
}
4646

47-
OutletInfo oi = (OutletInfo) ola.getItem(list_position);
47+
OutletInfo oi = ola.getItem(list_position);
4848
if (oi.State != state) {
4949
retries++;
5050
Log.w("DeviceControl", "Sending again, no response " + Integer.valueOf(list_position).toString());

app/src/main/java/oly/netpowerctrl/widget/DeviceWidgetProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public void onDeleted(Context context, int[] appWidgetIds) {
1313
for (int appWidgetId : appWidgetIds) {
1414
SharedPrefs.DeleteWidgets(context, appWidgetId);
1515
}
16+
Intent intent = new Intent(context.getApplicationContext(), WidgetUpdateService.class);
17+
context.startService(intent);
1618
}
1719

1820
@Override

app/src/main/java/oly/netpowerctrl/widget/WidgetUpdateService.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.content.SharedPreferences;
1010
import android.os.IBinder;
1111
import android.preference.PreferenceManager;
12-
import android.util.Log;
1312
import android.util.SparseArray;
1413
import android.widget.RemoteViews;
1514

@@ -72,7 +71,9 @@ public void onDestroy() {
7271

7372
@Override
7473
public void onCreate() {
75-
Log.w("WIDGET", "CREATE");
74+
//noinspection ConstantConditions
75+
appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
76+
7677
/**
7778
* If the service is kept running, we will receive further device updates
7879
* and register on the listener service for this purpose.
@@ -97,14 +98,6 @@ public void onCreate() {
9798
*/
9899
@Override
99100
public int onStartCommand(Intent intent, int flags, int startId) {
100-
if (intent == null) {
101-
stopSelf();
102-
return START_NOT_STICKY;
103-
}
104-
105-
//noinspection ConstantConditions
106-
appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
107-
108101
// Extract widget ids from intent
109102
//int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
110103
//assert allWidgetIds != null;
@@ -121,7 +114,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
121114
}
122115

123116
boolean noFetch = false;
124-
if (intent.getExtras() != null)
117+
if (intent != null && intent.getExtras() != null)
125118
noFetch = intent.getExtras().containsKey("noFetch");
126119

127120
// Keep a list of scenes to fetch state updates
@@ -287,6 +280,8 @@ public void onDeviceUpdated(DeviceInfo di) {
287280
if (widgetUpdateRequests.size() == 0) {
288281
ComponentName thisWidget = new ComponentName(getApplicationContext(),
289282
DeviceWidgetProvider.class);
283+
//noinspection ConstantConditions
284+
appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
290285
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
291286
for (int appWidgetId : allWidgetIds) {
292287
DeviceInfoOutletNumber widget_di = allWidgets.get(appWidgetId);

0 commit comments

Comments
 (0)