summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/preference/Preferences.java
blob: a138c75e500684619413b32e6c7ea1d157327850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2012-2014 The CyanogenMod Project (DvTonder)
 *
 * 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.cyanogenmod.lockclock.preference;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.cyanogenmod.lockclock.R;

import java.util.List;

public class Preferences extends PreferenceActivity {

    // only used when adding a new widget
    private int mNewWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preferences_headers, target);

        // Check if triggered from adding a new widget
        Intent intent = getIntent();
        if (intent != null
                && AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(intent.getAction())) {
            mNewWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mNewWidgetId);
            // See http://code.google.com/p/android/issues/detail?id=2539
            myResult(RESULT_CANCELED);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        ActionBar mActionBar = getActionBar();
        // Show up navigation and hide 'done' button when not triggered from adding a new widget
        if ((mActionBar != null) && (mNewWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)) {
            mActionBar.setDisplayHomeAsUpEnabled(true);
            MenuItem item = menu.findItem(R.id.menu_done);
            item.setVisible(false);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_done:
            case android.R.id.home:
                myResult(RESULT_OK);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        // If launched from the configure intent, signal RESULT_OK
        myResult(RESULT_OK);
        super.onBackPressed();
    }

    private void myResult(int result) {
        if (mNewWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            setResult(result, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mNewWidgetId));
        }
    }

    /**
     * This is required to be able to build with API level 19
     */
    @SuppressLint("Override")
    @Override
    public boolean isValidFragment(String fragmentName) {
        // Assume a valid fragment name at all times
        return true;
    }
}