// Path: app/src/main/java/com/xargsgrep/portknocker/widget/HostWidget.java
// public class HostWidget extends AppWidgetProvider
// {
//     @Override
//     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
//     {
//         for (int appWidgetId : appWidgetIds)
//         {
//             long hostId = ConfigureWidgetActivity.getHostIdPreference(context, appWidgetId);
//             updateAppWidget(context, appWidgetManager, appWidgetId, hostId);
//         }
//     }
// 
//     @Override
//     public void onDeleted(Context context, int[] appWidgetIds)
//     {
//         for (int i = 0; i < appWidgetIds.length; i++)
//         {
//             ConfigureWidgetActivity.deleteHostIdPreference(context, appWidgetIds[i]);
//         }
//     }
// 
//     public static void updateAllAppWidgets(Context context)
//     {
//         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//         int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, HostWidget.class));
//         for (int appWidgetId : appWidgetIds)
//         {
//             updateAppWidget(context, appWidgetManager, appWidgetId, null);
//         }
//     }
// 
//     public static void updateAllAppWidgetsForHost(Context context, long hostId)
//     {
//         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//         int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, HostWidget.class));
//         for (int appWidgetId : appWidgetIds)
//         {
//             updateAppWidget(context, appWidgetManager, appWidgetId, hostId);
//         }
//     }
// 
//     public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Long hostId)
//     {
//         // workaround for phantom widgets
//         boolean configured = ConfigureWidgetActivity.getConfiguredPreference(context, appWidgetId);
//         if (!configured) return;
// 
//         Long widgetHostId = ConfigureWidgetActivity.getHostIdPreference(context, appWidgetId);
//         if (hostId != null && !hostId.equals(widgetHostId)) return;
// 
//         DatabaseManager hostDataManager = new DatabaseManager(context);
//         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// 
// 
//         boolean hostExists = hostDataManager.hostExists(widgetHostId);
//         if (hostExists)
//         {
//             Host host = hostDataManager.getHost(widgetHostId);
// 
//             Intent intent = new Intent(context, HostListActivity.class);
//             intent.putExtra("hostId", widgetHostId);
//             PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//             views.setOnClickPendingIntent(R.id.widget, pendingIntent);
// 
//             views.setTextViewText(R.id.widget_host_label, host.getLabel());
//             views.setTextViewText(R.id.widget_host_hostname, host.getHostname());
//             views.setTextViewText(R.id.widget_host_ports, host.getPortsString());
// 
//             SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
//             if (sharedPreferences.getBoolean(context.getString(R.string.pref_key_hide_ports_widget), false))
//             {
//                 views.setViewVisibility(R.id.widget_host_ports, View.GONE);
//             }
//             else
//             {
//                 views.setViewVisibility(R.id.widget_host_ports, View.VISIBLE);
//             }
//         }
//         else
//         {
//             views.setTextViewText(R.id.widget_host_label, "Invalid Host");
//             views.setViewVisibility(R.id.widget_host_hostname, View.GONE);
//             views.setViewVisibility(R.id.widget_host_ports, View.GONE);
// 
//             Intent intent = new Intent(context, HostListActivity.class);
//             PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//             views.setOnClickPendingIntent(R.id.widget, pendingIntent);
//         }
// 
//         appWidgetManager.updateAppWidget(appWidgetId, views);
//     }
// }


import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.MenuItem;
import com.xargsgrep.portknocker.R;
import com.xargsgrep.portknocker.widget.HostWidget;
/*
 *  Copyright 2014 Ahsan Rabbani
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.xargsgrep.portknocker.activity;



public class SettingsActivityCompat extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
    @Override
    @SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                Intent hostListIntent = new Intent(this, HostListActivity.class);
                hostListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(hostListIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        if (getString(R.string.pref_key_hide_ports_widget).equals(key))
        {
// Path: app/src/main/java/com/xargsgrep/portknocker/widget/HostWidget.java
// public class HostWidget extends AppWidgetProvider
// {
//     @Override
//     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
//     {
//         for (int appWidgetId : appWidgetIds)
//         {
//             long hostId = ConfigureWidgetActivity.getHostIdPreference(context, appWidgetId);
//             updateAppWidget(context, appWidgetManager, appWidgetId, hostId);
//         }
//     }
// 
//     @Override
//     public void onDeleted(Context context, int[] appWidgetIds)
//     {
//         for (int i = 0; i < appWidgetIds.length; i++)
//         {
//             ConfigureWidgetActivity.deleteHostIdPreference(context, appWidgetIds[i]);
//         }
//     }
// 
//     public static void updateAllAppWidgets(Context context)
//     {
//         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//         int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, HostWidget.class));
//         for (int appWidgetId : appWidgetIds)
//         {
//             updateAppWidget(context, appWidgetManager, appWidgetId, null);
//         }
//     }
// 
//     public static void updateAllAppWidgetsForHost(Context context, long hostId)
//     {
//         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//         int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, HostWidget.class));
//         for (int appWidgetId : appWidgetIds)
//         {
//             updateAppWidget(context, appWidgetManager, appWidgetId, hostId);
//         }
//     }
// 
//     public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Long hostId)
//     {
//         // workaround for phantom widgets
//         boolean configured = ConfigureWidgetActivity.getConfiguredPreference(context, appWidgetId);
//         if (!configured) return;
// 
//         Long widgetHostId = ConfigureWidgetActivity.getHostIdPreference(context, appWidgetId);
//         if (hostId != null && !hostId.equals(widgetHostId)) return;
// 
//         DatabaseManager hostDataManager = new DatabaseManager(context);
//         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// 
// 
//         boolean hostExists = hostDataManager.hostExists(widgetHostId);
//         if (hostExists)
//         {
//             Host host = hostDataManager.getHost(widgetHostId);
// 
//             Intent intent = new Intent(context, HostListActivity.class);
//             intent.putExtra("hostId", widgetHostId);
//             PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//             views.setOnClickPendingIntent(R.id.widget, pendingIntent);
// 
//             views.setTextViewText(R.id.widget_host_label, host.getLabel());
//             views.setTextViewText(R.id.widget_host_hostname, host.getHostname());
//             views.setTextViewText(R.id.widget_host_ports, host.getPortsString());
// 
//             SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
//             if (sharedPreferences.getBoolean(context.getString(R.string.pref_key_hide_ports_widget), false))
//             {
//                 views.setViewVisibility(R.id.widget_host_ports, View.GONE);
//             }
//             else
//             {
//                 views.setViewVisibility(R.id.widget_host_ports, View.VISIBLE);
//             }
//         }
//         else
//         {
//             views.setTextViewText(R.id.widget_host_label, "Invalid Host");
//             views.setViewVisibility(R.id.widget_host_hostname, View.GONE);
//             views.setViewVisibility(R.id.widget_host_ports, View.GONE);
// 
//             Intent intent = new Intent(context, HostListActivity.class);
//             PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//             views.setOnClickPendingIntent(R.id.widget, pendingIntent);
//         }
// 
//         appWidgetManager.updateAppWidget(appWidgetId, views);
//     }
// }


// Path: app/src/main/java/com/xargsgrep/portknocker/activity/SettingsActivityCompat.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.MenuItem;
import com.xargsgrep.portknocker.R;
import com.xargsgrep.portknocker.widget.HostWidget;

/*
 *  Copyright 2014 Ahsan Rabbani
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.xargsgrep.portknocker.activity;



public class SettingsActivityCompat extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
    @Override
    @SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                Intent hostListIntent = new Intent(this, HostListActivity.class);
                hostListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(hostListIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        if (getString(R.string.pref_key_hide_ports_widget).equals(key))
        {
            HostWidget.updateAllAppWidgets(this);