22
33import android .app .Activity ;
44import android .app .AlertDialog ;
5- import android .app .Fragment ;
65import android .app .FragmentTransaction ;
76import android .content .DialogInterface ;
87import android .os .Bundle ;
2221
2322import oly .netpowerctrl .R ;
2423import oly .netpowerctrl .application_state .NetpowerctrlApplication ;
25- import oly .netpowerctrl .application_state .NetpowerctrlService ;
2624import oly .netpowerctrl .application_state .PluginInterface ;
2725import oly .netpowerctrl .device_ports .DevicePort ;
2826import oly .netpowerctrl .devices .Device ;
3331import oly .netpowerctrl .network .DeviceObserverResult ;
3432import oly .netpowerctrl .network .DeviceQuery ;
3533import oly .netpowerctrl .network .DeviceUpdate ;
34+ import oly .netpowerctrl .preferences .SharedPrefs ;
3635import oly .netpowerctrl .utils .DoneCancelFragmentHelper ;
3736
3837public class AnelDevicePreferences extends PreferenceFragment implements DeviceObserverResult , DeviceUpdate {
@@ -137,7 +136,44 @@ public boolean onPreferenceChange(Preference preference, Object o) {
137136 }
138137 });
139138
140- // update_connections();
139+ p = m .findPreference ("connections_http_new" );
140+ p .setOnPreferenceClickListener (new Preference .OnPreferenceClickListener () {
141+ @ Override
142+ public boolean onPreferenceClick (Preference preference ) {
143+ String host = "" ;
144+
145+ if (device .DeviceConnections .size () > 0 )
146+ host = device .DeviceConnections .get (0 ).getDestinationHost ();
147+ DeviceConnection deviceConnection = new DeviceConnectionHTTP (device , host , 80 );
148+ DeviceConnectionPreferences fragment = new DeviceConnectionPreferences ();
149+ fragment .setDeviceConnection (deviceConnection , device , true );
150+
151+ //noinspection ConstantConditions
152+ getFragmentManager ().beginTransaction ().addToBackStack (null ).
153+ setTransitionStyle (FragmentTransaction .TRANSIT_FRAGMENT_OPEN ).
154+ replace (R .id .content_frame , fragment ).commit ();
155+ return true ;
156+ }
157+ });
158+ p = m .findPreference ("connections_udp_new" );
159+ p .setOnPreferenceClickListener (new Preference .OnPreferenceClickListener () {
160+ @ Override
161+ public boolean onPreferenceClick (Preference preference ) {
162+ String host = "" ;
163+ if (device .DeviceConnections .size () > 0 )
164+ host = device .DeviceConnections .get (0 ).getDestinationHost ();
165+ DeviceConnection deviceConnection = new DeviceConnectionUDP (device , host ,
166+ SharedPrefs .getDefaultReceivePort (), SharedPrefs .getDefaultSendPort ());
167+ DeviceConnectionPreferences fragment = new DeviceConnectionPreferences ();
168+ fragment .setDeviceConnection (deviceConnection , device , true );
169+
170+ //noinspection ConstantConditions
171+ getFragmentManager ().beginTransaction ().addToBackStack (null ).
172+ setTransitionStyle (FragmentTransaction .TRANSIT_FRAGMENT_OPEN ).
173+ replace (R .id .content_frame , fragment ).commit ();
174+ return true ;
175+ }
176+ });
141177 }
142178
143179 @ Override
@@ -154,13 +190,13 @@ private void update_connections() {
154190
155191 for (int i = pc_http .getPreferenceCount () - 1 ; i >= 0 ; --i ) {
156192 Preference s = pc_http .getPreference (i );
157- if (s instanceof PreferenceScreen ) {
193+ if (! s . getKey (). equals ( "connections_http_new" ) ) {
158194 pc_http .removePreference (s );
159195 }
160196 }
161197 for (int i = pc_udp .getPreferenceCount () - 1 ; i >= 0 ; --i ) {
162198 Preference s = pc_udp .getPreference (i );
163- if (s instanceof PreferenceScreen ) {
199+ if (! s . getKey (). equals ( "connections_udp_new" ) ) {
164200 pc_udp .removePreference (s );
165201 }
166202 }
@@ -184,9 +220,13 @@ private void update_connections() {
184220 s .setOnPreferenceClickListener (new Preference .OnPreferenceClickListener () {
185221 @ Override
186222 public boolean onPreferenceClick (Preference preference ) {
187- Fragment fragment = Fragment .instantiate (getActivity (),
188- DeviceConnectionPreferences .class .getName ());
189- ((DeviceConnectionPreferences ) fragment ).setDeviceConnection (deviceConnection );
223+ // Fragment fragment = Fragment.instantiate(getActivity(),
224+ // DeviceConnectionPreferences.class.getName());
225+ // ((DeviceConnectionPreferences) fragment).setDeviceConnection(deviceConnection);
226+
227+ DeviceConnectionPreferences fragment = new DeviceConnectionPreferences ();
228+ fragment .setDeviceConnection (deviceConnection , device , false );
229+
190230 //noinspection ConstantConditions
191231 getFragmentManager ().beginTransaction ().addToBackStack (null ).
192232 setTransitionStyle (FragmentTransaction .TRANSIT_FRAGMENT_OPEN ).
@@ -223,9 +263,12 @@ private void testDevice() {
223263
224264 test_state = TestStates .TEST_REACHABLE ;
225265
226- PluginInterface pi = device .getPluginInterface (NetpowerctrlService .getService ());
227- assert pi != null ;
228- pi .enterFullNetworkState (device );
266+ PluginInterface pluginInterface = device .getPluginInterface ();
267+ if (pluginInterface == null ) {
268+ Toast .makeText (getActivity (), R .string .error_plugin_not_installed , Toast .LENGTH_SHORT ).show ();
269+ return ;
270+ }
271+ pluginInterface .enterFullNetworkState (device );
229272
230273 deviceQuery = new DeviceQuery (this , device );
231274 }
@@ -260,9 +303,10 @@ public void onClick(DialogInterface dialog, int whichButton) {
260303 }
261304
262305 private void saveAndFinish () {
263- PluginInterface pi = device .getPluginInterface (NetpowerctrlService .getService ());
264- assert pi != null ;
265- pi .enterFullNetworkState (device );
306+ PluginInterface pluginInterface = device .getPluginInterface ();
307+ if (pluginInterface != null ) {
308+ pluginInterface .enterFullNetworkState (device );
309+ }
266310
267311 NetpowerctrlApplication .getDataController ().addToConfiguredDevices (device );
268312 //noinspection ConstantConditions
@@ -288,7 +332,7 @@ public void onDeviceUpdated(Device updated_device, boolean willBeRemoved) {
288332 test_state = TestStates .TEST_ACCESS ;
289333 // Just send the current value of the first device port as target value.
290334 // Should change nothing but we will get a feedback if the credentials are working.
291- PluginInterface pi = device .getPluginInterface (NetpowerctrlService . getService () );
335+ PluginInterface pi = device .getPluginInterface ();
292336 assert pi != null ;
293337 if (deviceQuery != null ) {
294338 deviceQuery .addDevice (device , false );
0 commit comments