/*
 * Copyright 2014 Uwe Trottmann
 *
 * 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.battlelancer.seriesguide.appwidget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RemoteViews;
import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.ui.SeriesGuidePreferences;

/**
 * Hosts a {@link ListWidgetPreferenceFragment} to allow changing settings of the associated app
 * widget.
 * <p> Does specifically NOT extend {@link com.battlelancer.seriesguide.ui.BaseActivity} to avoid
 * triggering update and backup mechanisms.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ListWidgetConfigure extends Activity {

    private int mAppWidgetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // set a theme based on user preference
        setTheme(SeriesGuidePreferences.THEME);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listwidget_configure);

        // if the user backs out, no widget gets added
        setResult(RESULT_CANCELED);

        // get given app widget id
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
            return;
        }

        ListWidgetPreferenceFragment f = ListWidgetPreferenceFragment.newInstance(mAppWidgetId);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.content_container, f);
        ft.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.widget_config_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_save) {
            onUpdateWidget();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onUpdateWidget() {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        RemoteViews views = ListWidgetProvider
                .buildRemoteViews(this, appWidgetManager, mAppWidgetId);
        appWidgetManager.updateAppWidget(mAppWidgetId, views);
        appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.list_view);

        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(RESULT_OK, resultValue);
        finish();
    }
}

@@ -678,16 +678,46 @@
 getApi;%0A
+import android.app.ActionBar;%0A
 import a
@@ -737,16 +737,16 @@
 tivity;%0A
-
 import a
@@ -1094,16 +1094,67 @@
 erences;
+%0Aimport com.uwetrottmann.androidutils.AndroidUtils;
 %0A%0A/**%0A *
@@ -1401,16 +1401,84 @@
 anisms.%0A
+ * %3Cp%3E The list widget is only available on API level 11 and above.%0A
  */%0A@Tar
@@ -2439,32 +2439,59 @@
 urn;%0A        %7D%0A%0A
+        setupActionBar();%0A%0A
         ListWidg
@@ -2692,16 +2692,16 @@
 er, f);%0A
-
         
@@ -2712,32 +2712,252 @@
 ommit();%0A    %7D%0A%0A
+    private void setupActionBar() %7B%0A        ActionBar actionBar = getActionBar();%0A        if (actionBar != null && AndroidUtils.isICSOrHigher()) %7B%0A            actionBar.setIcon(R.drawable.ic_actionbar);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

Set white action bar icon for widget config activity.