1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.activity.setup;
18
19import android.app.Fragment;
20import android.content.Context;
21import android.os.Bundle;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.webkit.WebView;
26import android.widget.CheckBox;
27import android.widget.CompoundButton;
28import android.widget.CompoundButton.OnCheckedChangeListener;
29
30import com.android.email.DebugUtils;
31import com.android.email.Preferences;
32import com.android.email.R;
33import com.android.email.activity.UiUtilities;
34import com.android.email.service.EmailServiceUtils;
35import com.android.emailcommon.Logging;
36import com.android.mail.preferences.MailPrefs;
37import com.android.mail.utils.LogUtils;
38
39public class DebugFragment extends Fragment implements OnCheckedChangeListener,
40        View.OnClickListener {
41    private Preferences mPreferences;
42
43    // Public no-args constructor needed for fragment re-instantiation
44    public DebugFragment() {}
45
46    @Override
47    public View onCreateView(LayoutInflater inflater, ViewGroup container,
48            Bundle savedInstanceState) {
49        if (Logging.DEBUG_LIFECYCLE && DebugUtils.DEBUG) {
50            LogUtils.d(Logging.LOG_TAG, "DebugFragment onCreateView");
51        }
52        View view = inflater.inflate(R.layout.debug, container, false);
53
54        Context context = getActivity();
55        mPreferences = Preferences.getPreferences(context);
56
57        final CheckBox enableDebugLoggingView = UiUtilities.getView(view, R.id.debug_logging);
58        enableDebugLoggingView.setChecked(DebugUtils.DEBUG);
59
60        final CheckBox enableExchangeLoggingView = UiUtilities.getView(view, R.id.exchange_logging);
61        final CheckBox enableFileLoggingView = UiUtilities.getView(view, R.id.file_logging);
62
63        // Note:  To prevent recursion while presetting checkboxes, assign all listeners last
64        enableDebugLoggingView.setOnCheckedChangeListener(this);
65
66        if (EmailServiceUtils.areRemoteServicesInstalled(context)) {
67            enableExchangeLoggingView.setChecked(DebugUtils.DEBUG_EXCHANGE);
68            enableFileLoggingView.setChecked(DebugUtils.DEBUG_FILE);
69            enableExchangeLoggingView.setOnCheckedChangeListener(this);
70            enableFileLoggingView.setOnCheckedChangeListener(this);
71        } else {
72            enableExchangeLoggingView.setVisibility(View.GONE);
73            enableFileLoggingView.setVisibility(View.GONE);
74        }
75
76        UiUtilities.getView(view, R.id.clear_webview_cache).setOnClickListener(this);
77        UiUtilities.getView(view, R.id.clear_migration_state).setOnClickListener(this);
78
79        final CheckBox enableStrictModeView =
80                UiUtilities.getView(view, R.id.debug_enable_strict_mode);
81        enableStrictModeView.setChecked(mPreferences.getEnableStrictMode());
82        enableStrictModeView.setOnCheckedChangeListener(this);
83
84        return view;
85    }
86
87    @Override
88    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
89        switch (buttonView.getId()) {
90            case R.id.debug_logging:
91                mPreferences.setEnableDebugLogging(isChecked);
92                DebugUtils.DEBUG = isChecked;
93                break;
94            case R.id.exchange_logging:
95                mPreferences.setEnableExchangeLogging(isChecked);
96                DebugUtils.DEBUG_EXCHANGE = isChecked;
97                break;
98            case R.id.file_logging:
99                mPreferences.setEnableExchangeFileLogging(isChecked);
100                DebugUtils.DEBUG_FILE = isChecked;
101                break;
102            case R.id.debug_enable_strict_mode:
103                mPreferences.setEnableStrictMode(isChecked);
104                DebugUtils.enableStrictMode(isChecked);
105                break;
106        }
107
108        DebugUtils.updateLoggingFlags(getActivity());
109    }
110
111    @Override
112    public void onClick(View v) {
113        switch (v.getId()) {
114            case R.id.clear_webview_cache:
115                clearWebViewCache();
116                break;
117            case R.id.clear_migration_state:
118                clearMigrationState();
119                break;
120        }
121    }
122
123    private void clearWebViewCache() {
124        WebView webview = new WebView(getActivity());
125        try {
126            webview.clearCache(true);
127            LogUtils.w(Logging.LOG_TAG, "Cleard WebView cache.");
128        } finally {
129            webview.destroy();
130        }
131    }
132
133    private void clearMigrationState() {
134        MailPrefs prefs = MailPrefs.get(getActivity());
135        prefs.setMigrationState(MailPrefs.PreferenceKeys.MIGRATION_STATE_NONE);
136    }
137}
138