/*
* Copyright 2011-2014 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package devcoin.wallet.ui;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import devcoin.wallet.Constants;
import devcoin.wallet.WalletApplication;
import devcoin.wallet.R;
/**
* @author Andreas Schildbach
*/
public abstract class AbstractWalletActivity extends SherlockFragmentActivity
{
private WalletApplication application;
protected static final Logger log = LoggerFactory.getLogger(AbstractWalletActivity.class);
@Override
protected void onCreate(final Bundle savedInstanceState)
{
application = (WalletApplication) getApplication();
super.onCreate(savedInstanceState);
}
protected WalletApplication getWalletApplication()
{
return application;
}
protected final void toast(@Nonnull final String text, final Object... formatArgs)
{
toast(text, 0, Toast.LENGTH_SHORT, formatArgs);
}
protected final void longToast(@Nonnull final String text, final Object... formatArgs)
{
toast(text, 0, Toast.LENGTH_LONG, formatArgs);
}
protected final void toast(@Nonnull final String text, final int imageResId, final int duration, final Object... formatArgs)
{
final View view = getLayoutInflater().inflate(R.layout.transient_notification, null);
TextView tv = (TextView) view.findViewById(R.id.transient_notification_text);
tv.setText(String.format(text, formatArgs));
tv.setCompoundDrawablesWithIntrinsicBounds(imageResId, 0, 0, 0);
final Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(duration);
toast.show();
}
protected final void toast(final int textResId, final Object... formatArgs)
{
toast(textResId, 0, Toast.LENGTH_SHORT, formatArgs);
}
protected final void longToast(final int textResId, final Object... formatArgs)
{
toast(textResId, 0, Toast.LENGTH_LONG, formatArgs);
}
protected final void toast(final int textResId, final int imageResId, final int duration, final Object... formatArgs)
{
final View view = getLayoutInflater().inflate(R.layout.transient_notification, null);
TextView tv = (TextView) view.findViewById(R.id.transient_notification_text);
tv.setText(getString(textResId, formatArgs));
tv.setCompoundDrawablesWithIntrinsicBounds(imageResId, 0, 0, 0);
final Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(duration);
toast.show();
}
protected void touchLastUsed()
{
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final long prefsLastUsed = prefs.getLong(Constants.PREFS_KEY_LAST_USED, 0);
final long now = System.currentTimeMillis();
prefs.edit().putLong(Constants.PREFS_KEY_LAST_USED, now).commit();
log.info("just being used - last used {} minutes ago", (now - prefsLastUsed) / DateUtils.MINUTE_IN_MILLIS);
}
}