package com.geel9.facepunch.activities; import java.util.List; import com.actionbarsherlock.ActionBarSherlock; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.MenuItem; import com.crittercism.app.Crittercism; import com.geel9.facepunch.APISession; import com.geel9.facepunch.FPApp; import com.geel9.facepunch.APISession.LogoutCallback; import com.geel9.facepunch.R.anim; import com.geel9.facepunch.R.drawable; import com.geel9.facepunch.R.string; import com.geel9.facepunch.R; import com.google.ads.AdRequest; import com.google.ads.AdSize; import com.google.ads.AdView; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.graphics.Typeface; import android.graphics.drawable.AnimationDrawable; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Bundle; import android.os.IBinder; import android.support.v4.app.NavUtils; import android.support.v4.app.TaskStackBuilder; import android.telephony.TelephonyManager; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class FPActivity extends SherlockFragmentActivity { protected APISession api = null; //No ads anymore <3 public AdView adView; public static final int ID_READTHREADS = 100; public static final int ID_SETTINGS = 101; public static final int ID_LOGOUT = 102; public static final int ID_LOGIN = 103; public static final int ID_REFRESH = 104; public static final int ID_POPULARTHREADS = 105; // Inheriting activities can prevent the user button from appearing, e.g. in the login activity protected boolean suppressUserButton = false; public ActionBar ActionBar(){ return getSupportActionBar(); } public void ActionText(String text){ ActionBar().setTitle(text); } public void HideKeyboard(IBinder windowToken){ InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(windowToken, 0); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ if(api.loggedIn()){ GetPreferences().edit().putString("sessionid", api.sessionID).commit(); OnLogin(); } } public void DoLogin(){ Intent loginIntent = new Intent( FPActivity.this, LoginActivity.class ); startActivityForResult(loginIntent, 0); } @Override public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { MenuItem refresh = menu.add(menu.NONE, ID_REFRESH, menu.NONE, "Refresh"); menu.add(menu.NONE, ID_READTHREADS, menu.NONE, "Read Threads"); menu.add(menu.NONE, ID_POPULARTHREADS, menu.NONE, "Popular Threads"); menu.add(menu.NONE, ID_SETTINGS, menu.NONE, "Settings"); menu.add(menu.NONE, ID_LOGOUT, menu.NONE, "Logout"); menu.add(menu.NONE, ID_LOGIN, menu.NONE, "Login"); refresh.setIcon(R.drawable.ic_menu_refresh); refresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); RemoveMenuItems(menu); return true; } public void OnLogin(){ invalidateOptionsMenu(); } public void OnLogout(){ invalidateOptionsMenu(); } public void RemoveMenuItems(com.actionbarsherlock.view.Menu menu){ if(!api.loggedIn()){ menu.removeItem(ID_LOGOUT); menu.removeItem(ID_READTHREADS); } else{ menu.removeItem(ID_LOGIN); } } public void OnRefreshPressed() { CheckLoginState(); invalidateOptionsMenu(); } public void OnNextPressed(){ } public void OnPreviousPressed(){ } @Override public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { // Handle item selection int order = item.getOrder(); switch (item.getItemId()) { case android.R.id.home: Intent upIntent = new Intent(this, FrontpageActivity.class); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { // This activity is not part of the application's task, so create a new task // with a synthesized back stack. TaskStackBuilder.from(this) .addNextIntent(upIntent) .startActivities(); finish(); } else { // This activity is part of the application's task, so simply // navigate up to the hierarchical parent activity. NavUtils.navigateUpTo(this, upIntent); } return true; case ID_LOGOUT: final ProgressDialog logoutDialog = ProgressDialog.show( FPActivity.this, "", getString( R.string.loggingOut ), true ); logoutDialog.show(); api.logout( new APISession.LogoutCallback() { public void onResult( boolean success ) { logoutDialog.dismiss(); SharedPreferences p = getSharedPreferences("com.geel9.facepunch_preferences", MODE_PRIVATE); p.edit().putString("sessionid", "").commit(); if ( success ) { OnLogout(); } else { Toast.makeText( FPActivity.this, getString( R.string.loggingOutFailed ), Toast.LENGTH_SHORT ).show(); } } } ); break; case ID_READTHREADS: //Read threads with new posts Intent read = new Intent(FPActivity.this, ReadThreadActivity.class); startActivity(read); break; case ID_SETTINGS: Intent prefsIntent = new Intent(FPActivity.this, Preferences.class); startActivity(prefsIntent); break; case ID_REFRESH: OnRefreshPressed(); break; case ID_POPULARTHREADS: Intent popular = new Intent(FPActivity.this, PopularThreadsActivity.class); startActivity(popular); break; case ID_LOGIN: DoLogin(); break; default: return super.onOptionsItemSelected(item); } return true; } @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); Crittercism.init(this, "4f1c675db093157101000084"); // Retrieve global API handle api = ( (FPApp)getApplicationContext() ).api(); } private boolean isDonateInstalled(final Context context){ PackageManager p = context.getPackageManager(); List<ApplicationInfo> packages = p.getInstalledApplications(0); for(int i = 0; i < packages.size(); i++){ ApplicationInfo ac = packages.get(i); String packageName = ac.packageName; if(packageName != null && (packageName.equals("com.geel9.donate") || packageName.equals("com.geel9.facepunchpremium"))) return true; } return false; } public boolean hasUserDonated(final Context context){ String name = context.getPackageName(); if(name.toLowerCase().contains("premium")){ return true; } SharedPreferences p = getSharedPreferences("com.geel9.facepunch_preferences", MODE_PRIVATE); boolean donated = p.getBoolean("hasDonated", false); boolean ret = donated || isDonateInstalled(context); p.edit().putBoolean("hasDonated", ret).commit(); return ret || true; } protected void shamelesslyBeg(){ if(hasUserDonated(this)) return; SharedPreferences p = getSharedPreferences("com.geel9.facepunch_preferences", MODE_PRIVATE); if(p.getBoolean("hasBegged", false)) return; p.edit().putBoolean("hasBegged", true).commit(); new AlertDialog.Builder( FPActivity.this ) .setTitle( "Donate?" ) .setPositiveButton("Donate", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("https://market.android.com/details?id=com.geel9.facepunchpremium")); startActivity(i); dialog.dismiss(); } }).setNegativeButton("I'm heartless.", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setMessage("Donating is easy, costs only $1, and is very fast. A lot of time and effort went into making this app. This message will not appear again, whether or not you donate.") .create().show(); } public void DisplayAd(LinearLayout layout){ if(!hasUserDonated(this)){ // Create the adView adView = new AdView(this, AdSize.BANNER, "a15014d4f4dce8a"); // Add the adView to it layout.addView(adView); // Initiate a generic request to load it with an ad adView.loadAd(new AdRequest()); } } @Override public void onDestroy() { if (adView != null) { adView.destroy(); } super.onDestroy(); } protected void CheckLoginState(){ SharedPreferences p = GetPreferences(); String sessionid = p.getString("sessionid", ""); //If we're logged in but the api doesn't think so if(!api.loggedIn()){ if(!sessionid.equals("")){ api.sessionID = sessionid; api.username = p.getString("username", ""); api.bb_userid = p.getInt("userid", -1); api.loggedIn = true; OnLogin(); } } //If we're not logged in but the api thinks we are. else{ if(sessionid.equals("") && api.sessionID.equals("")){ api.logout(new LogoutCallback(){ public void onResult(boolean success) { GetPreferences().edit().putString("sessionid", "").commit(); OnLogout(); } }); } } } public void lockOrientation(){ setRequestedOrientation(getResources().getConfiguration().orientation); } public void unLockOrientation(){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } @Override public void onContentChanged() { ActionBar action = ActionBar(); if(action == null) return; //action.setDisplayHomeAsUpEnabled(true); action.setHomeButtonEnabled(true); } // Show animated loading icon in ImageView protected void applyLoadingIcon( final ImageView iv ) { iv.setImageResource( R.anim.loadingspinner ); final AnimationDrawable loadingAnimation = (AnimationDrawable)iv.getDrawable(); iv.post( new Runnable() { public void run() { loadingAnimation.start(); } } ); } protected boolean is3G(){ ConnectivityManager mConnectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = mConnectivity.getActiveNetworkInfo(); return info.getType() == ConnectivityManager.TYPE_MOBILE; } protected SharedPreferences GetPreferences(){ return getSharedPreferences("com.geel9.facepunch_preferences", MODE_PRIVATE); } protected boolean showAvatars(){ SharedPreferences p = GetPreferences(); boolean onMobile = p.getBoolean("prefAvatars3G", false); return onMobile || !is3G(); } protected boolean showImages(){ SharedPreferences p = GetPreferences(); boolean onMobile = p.getBoolean("prefImages3G", false); return onMobile || !is3G(); } public boolean loadNitro(){ SharedPreferences p = GetPreferences(); return p.getBoolean("prefNitro", true); } protected boolean showWidescreen(){ int orientation = getResources().getConfiguration().orientation; boolean alwaysWideScreen = GetPreferences().getBoolean("prefAlwaysWide", false); return (orientation == Configuration.ORIENTATION_LANDSCAPE) || alwaysWideScreen; } protected boolean cacheAvatars(){ SharedPreferences p = GetPreferences(); boolean prefCacheAvatars = p.getBoolean("prefCacheAvatars", false); return prefCacheAvatars; } protected int cacheAvatarsSize(){ SharedPreferences p = GetPreferences(); int prefCacheAvatars = 2; try{ prefCacheAvatars = Integer.parseInt(p.getString("prefAvatarCacheLimit", "2")); }catch(Exception e){ } return prefCacheAvatars; } protected boolean autoLogin(){ SharedPreferences p = GetPreferences(); return p.getBoolean("autologin", false); } }