/* Copyright (c) 2009 Christoph Studer <chstuder@gmail.com>
*
* 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 tv.studer.smssync;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import tv.studer.smssync.SmsSyncService.SmsSyncState;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* This is the main activity showing the status of the SMS Sync service and
* providing controls to configure it.
*/
public class SmsSync extends PreferenceActivity implements OnPreferenceChangeListener {
private static final int DIALOG_MISSING_CREDENTIALS = 1;
private static final int DIALOG_FIRST_SYNC = 2;
private static final int DIALOG_SYNC_DATA_RESET = 3;
private static final int DIALOG_INVALID_IMAP_FOLDER = 4;
private static final int DIALOG_NEED_FIRST_MANUAL_SYNC = 5;
private static final int DIALOG_ABOUT = 6;
private static final int MENU_INFO = 0;
private static final int MENU_SHARE = 1;
private static final int MENU_MARKET = 2;
private StatusPreference mStatusPref;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager prefMgr = getPreferenceManager();
addPreferencesFromResource(R.xml.main_screen);
mStatusPref = new StatusPreference(this);
mStatusPref.setSelectable(false);
int sdkLevel = 1;
try {
sdkLevel = Integer.parseInt(Build.VERSION.SDK);
} catch (NumberFormatException nfe) {
// ignore (assume sdkLevel == 1)
}
if (sdkLevel < 3) {
// Older versions don't show the title bar for PreferenceActivity
PreferenceCategory cat = new PreferenceCategory(this);
cat.setOrder(0);
getPreferenceScreen().addPreference(cat);
cat.setTitle(R.string.ui_status_label);
cat.addPreference(mStatusPref);
} else {
// Newer SDK version show the title bar for PreferenceActivity
mStatusPref.setOrder(0);
getPreferenceScreen().addPreference(mStatusPref);
}
Preference pref = prefMgr.findPreference(PrefStore.PREF_LOGIN_USER);
pref.setOnPreferenceChangeListener(this);
pref = prefMgr.findPreference(PrefStore.PREF_IMAP_FOLDER);
pref.setOnPreferenceChangeListener(this);
pref = prefMgr.findPreference(PrefStore.PREF_ENABLE_AUTO_SYNC);
pref.setOnPreferenceChangeListener(this);
pref = prefMgr.findPreference(PrefStore.PREF_LOGIN_PASSWORD);
pref.setOnPreferenceChangeListener(this);
pref = prefMgr.findPreference(PrefStore.PREF_MAX_ITEMS_PER_SYNC);
pref.setOnPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
SmsSyncService.unsetStateChangeListener();
}
@Override
protected void onResume() {
super.onResume();
SmsSyncService.setStateChangeListener(mStatusPref);
updateUsernameLabelFromPref();
updateImapFolderLabelFromPref();
updateMaxItemsPerSync(null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_INFO, 0, R.string.menu_info).setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(0, MENU_SHARE, 1, R.string.menu_share).setIcon(
android.R.drawable.ic_menu_share);
menu.add(0, MENU_MARKET, 2, R.string.menu_market).setIcon(
R.drawable.ic_menu_update);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_INFO:
// openLink(Consts.URL_INFO_LINK);
showDialog(DIALOG_ABOUT);
return true;
case MENU_SHARE:
share();
return true;
case MENU_MARKET:
openLink(Consts.URL_MARKET_SEARCH);
return true;
}
return false;
}
private void openLink(String link) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(intent);
}
private void share() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_body,
Consts.URL_MARKET_SEARCH, Consts.URL_INFO_LINK));
startActivity(intent);
}
private void updateUsernameLabelFromPref() {
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
String username = prefs.getString(PrefStore.PREF_LOGIN_USER,
getString(R.string.ui_login_label));
Preference pref = getPreferenceManager().findPreference(PrefStore.PREF_LOGIN_USER);
pref.setTitle(username);
}
private void updateImapFolderLabelFromPref() {
String imapFolder = PrefStore.getImapFolder(this);
Preference pref = getPreferenceManager().findPreference(PrefStore.PREF_IMAP_FOLDER);
pref.setTitle(imapFolder);
}
private boolean initiateSync() {
if (!PrefStore.isLoginInformationSet(this)) {
showDialog(DIALOG_MISSING_CREDENTIALS);
return false;
} else if (PrefStore.isFirstSync(this)) {
showDialog(DIALOG_FIRST_SYNC);
return false;
} else {
startSync(false);
return true;
}
}
private void startSync(boolean skip) {
Intent intent = new Intent(this, SmsSyncService.class);
if (PrefStore.isFirstSync(this)) {
intent.putExtra(Consts.KEY_SKIP_MESSAGES, skip);
}
startService(intent);
}
private class StatusPreference extends Preference implements
SmsSyncService.StateChangeListener, OnClickListener {
private View mView;
private Button mSyncButton;
private ImageView mStatusIcon;
private TextView mStatusLabel;
private View mSyncDetails;
private TextView mErrorDetails;