1/*
2 * Copyright (C) 2008 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;
18
19import com.android.email.Controller;
20import com.android.email.Email;
21import com.android.email.Preferences;
22import com.android.email.R;
23import com.android.exchange.Eas;
24import com.android.exchange.utility.FileLogger;
25
26import android.app.Activity;
27import android.content.Context;
28import android.os.Bundle;
29import android.view.Menu;
30import android.view.MenuItem;
31import android.widget.CheckBox;
32import android.widget.CompoundButton;
33import android.widget.TextView;
34import android.widget.CompoundButton.OnCheckedChangeListener;
35
36public class Debug extends Activity implements OnCheckedChangeListener {
37    private TextView mVersionView;
38    private CheckBox mEnableDebugLoggingView;
39    private CheckBox mEnableSensitiveLoggingView;
40    private CheckBox mEnableExchangeLoggingView;
41    private CheckBox mEnableExchangeFileLoggingView;
42
43    private Preferences mPreferences;
44
45    @Override
46    public void onCreate(Bundle savedInstanceState) {
47        super.onCreate(savedInstanceState);
48
49        setContentView(R.layout.debug);
50
51        mPreferences = Preferences.getPreferences(this);
52
53        mVersionView = (TextView)findViewById(R.id.version);
54        mEnableDebugLoggingView = (CheckBox)findViewById(R.id.debug_logging);
55        mEnableSensitiveLoggingView = (CheckBox)findViewById(R.id.sensitive_logging);
56
57        mEnableDebugLoggingView.setOnCheckedChangeListener(this);
58        mEnableSensitiveLoggingView.setOnCheckedChangeListener(this);
59
60        mVersionView.setText(String.format(getString(R.string.debug_version_fmt).toString(),
61                getString(R.string.build_number)));
62
63        mEnableDebugLoggingView.setChecked(Email.DEBUG);
64        mEnableSensitiveLoggingView.setChecked(Email.DEBUG_SENSITIVE);
65
66        //EXCHANGE-REMOVE-SECTION-START
67        mEnableExchangeLoggingView = (CheckBox)findViewById(R.id.exchange_logging);
68        mEnableExchangeFileLoggingView = (CheckBox)findViewById(R.id.exchange_file_logging);
69        mEnableExchangeLoggingView.setOnCheckedChangeListener(this);
70        mEnableExchangeFileLoggingView.setOnCheckedChangeListener(this);
71        mEnableExchangeLoggingView.setChecked(Eas.USER_LOG);
72        mEnableExchangeFileLoggingView.setChecked(Eas.FILE_LOG);
73        //EXCHANGE-REMOVE-SECTION-END
74    }
75
76    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
77        switch (buttonView.getId()) {
78            case R.id.debug_logging:
79                Email.DEBUG = isChecked;
80                mPreferences.setEnableDebugLogging(Email.DEBUG);
81                break;
82            case R.id.sensitive_logging:
83                Email.DEBUG_SENSITIVE = isChecked;
84                mPreferences.setEnableSensitiveLogging(Email.DEBUG_SENSITIVE);
85                break;
86            //EXCHANGE-REMOVE-SECTION-START
87            case R.id.exchange_logging:
88                mPreferences.setEnableExchangeLogging(isChecked);
89                break;
90            case R.id.exchange_file_logging:
91                mPreferences.setEnableExchangeFileLogging(isChecked);
92                if (!isChecked) {
93                    FileLogger.close();
94                }
95                break;
96            //EXCHANGE-REMOVE-SECTION-END
97        }
98
99        updateLoggingFlags(this);
100    }
101
102    @Override
103    public boolean onOptionsItemSelected(MenuItem item) {
104        int id = item.getItemId();
105        if (id == R.id.dump_settings) {
106            Preferences.getPreferences(this).dump();
107            return true;
108        }
109        return super.onOptionsItemSelected(item);
110    }
111
112    @Override
113    public boolean onCreateOptionsMenu(Menu menu) {
114        super.onCreateOptionsMenu(menu);
115        getMenuInflater().inflate(R.menu.debug_option, menu);
116        return true;
117    }
118
119    /**
120     * Load enabled debug flags from the preferences and upadte the EAS debug flag.
121     */
122    public static void updateLoggingFlags(Context context) {
123        //EXCHANGE-REMOVE-SECTION-START
124        Preferences prefs = Preferences.getPreferences(context);
125        int debugLogging = prefs.getEnableDebugLogging() ? Eas.DEBUG_BIT : 0;
126        int exchangeLogging = prefs.getEnableExchangeLogging() ? Eas.DEBUG_EXCHANGE_BIT : 0;
127        int fileLogging = prefs.getEnableExchangeFileLogging() ? Eas.DEBUG_FILE_BIT : 0;
128        int debugBits = debugLogging | exchangeLogging | fileLogging;
129        Controller.getInstance(context).serviceLogging(debugBits);
130        //EXCHANGE-REMOVE-SECTION-END
131    }
132}
133