Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.danvelazco.fbwrapper.activity.BaseFacebookWebViewActivity;
import com.danvelazco.fbwrapper.activity.DrawerFragment;
import com.danvelazco.fbwrapper.preferences.FacebookPreferences;
import com.danvelazco.fbwrapper.util.Logger;
import com.danvelazco.fbwrapper.util.OrbotHelper;
Expand All @@ -33,6 +34,8 @@ public class FbWrapper extends BaseFacebookWebViewActivity {
private DrawerLayout mDrawerLayout = null;
private RelativeLayout mWebViewContainer = null;
private String mDomainToUse = INIT_URL_MOBILE;
private String mDomainSuffix = INIT_URL_MOBILE_SUFFIX;


// Preferences stuff
private SharedPreferences mSharedPreferences = null;
Expand All @@ -49,10 +52,10 @@ protected void onActivityCreated() {

// Keep a reference of the DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.layout_main);
getFragmentManager().beginTransaction().replace(R.id.drawerFragment, new DrawerFragment()).commit();
mWebViewContainer = (RelativeLayout) findViewById(R.id.webview_container);

// Set the click listener interface for the buttons
setOnClickListeners();
}

/**
Expand Down Expand Up @@ -110,7 +113,7 @@ protected void onWebViewInit(Bundle savedInstanceState) {
} else {
// Load the URL depending on the type of device or preference
Logger.d(LOG_TAG, "Loading the init Facebook URL");
loadNewPage(mDomainToUse);
loadNewPage(mDomainToUse + mDomainSuffix);
}
}

Expand All @@ -129,30 +132,10 @@ protected void onResumeActivity() {

// If the domain changes, reload the page with the new domain
if (!mDomainToUse.equalsIgnoreCase(previousDomainUsed)) {
loadNewPage(mDomainToUse);
loadNewPage(mDomainToUse + mDomainSuffix);
}
}

/**
* Sets the click listener on all the buttons in the activity
*/
private void setOnClickListeners() {
// Create a new listener
MenuDrawerButtonListener buttonsListener = new MenuDrawerButtonListener();

// Set this listener to all the buttons
findViewById(R.id.menu_drawer_right).setOnClickListener(buttonsListener);
findViewById(R.id.menu_item_jump_to_top).setOnClickListener(buttonsListener);
findViewById(R.id.menu_item_refresh).setOnClickListener(buttonsListener);
findViewById(R.id.menu_item_newsfeed).setOnClickListener(buttonsListener);
findViewById(R.id.menu_items_notifications).setOnClickListener(buttonsListener);
findViewById(R.id.menu_item_messages).setOnClickListener(buttonsListener);
findViewById(R.id.menu_share_this).setOnClickListener(buttonsListener);
findViewById(R.id.menu_preferences).setOnClickListener(buttonsListener);
findViewById(R.id.menu_about).setOnClickListener(buttonsListener);
findViewById(R.id.menu_kill).setOnClickListener(buttonsListener);
}

/**
* Used to open the menu drawer
*/
Expand Down Expand Up @@ -245,22 +228,22 @@ private void loadPreferences() {
// Force or detect the site mode to load
if (mode.equalsIgnoreCase(FacebookPreferences.SITE_MODE_MOBILE)) {
// Force the webview config to mobile
setupFacebookWebViewConfig(true, true, false, false, false);
setupFacebookWebViewConfig(MOBILE);
} else if (mode.equalsIgnoreCase(FacebookPreferences.SITE_MODE_DESKTOP)) {
// Force the webview config to desktop mode
setupFacebookWebViewConfig(true, false, false, false, false);
setupFacebookWebViewConfig(DESKTOP);
} else if (mode.equalsIgnoreCase(FacebookPreferences.SITE_MODE_ZERO)) {
// Force the webview config to zero mode
setupFacebookWebViewConfig(false, true, false, true, false);
setupFacebookWebViewConfig(ZERO);
} else if (mode.equalsIgnoreCase(FacebookPreferences.SITE_MODE_BASIC)) {
// Force the webview to load the Basic HTML Mobile site
setupFacebookWebViewConfig(true, true, true, false, false);
setupFacebookWebViewConfig(BASIC);
} else if (mode.equalsIgnoreCase(FacebookPreferences.SITE_MODE_ONION)) {
// Force the webview to load Facebook via Tor (onion network)
setupFacebookWebViewConfig(true, true, false, false, true);
setupFacebookWebViewConfig(ONION);
} else {
// Do not force, allow us to auto-detect what mode to use
setupFacebookWebViewConfig(false, true, false, false, false);

}

// If we haven't shown the new menu drawer to the user, auto open it
Expand All @@ -278,35 +261,39 @@ private void loadPreferences() {
/**
* Configure this {@link com.danvelazco.fbwrapper.webview.FacebookWebView}
* with the appropriate preferences depending on the device configuration.<br />
* Use the 'force' flag to force the configuration to either mobile or desktop.
*
* @param force {@link boolean}
* whether to force the configuration or not,
* if false the 'mobile' flag will be ignored
* @param mobile {@link boolean}
* whether to use the mobile or desktop site.
* @param facebookZero {@link boolean}
* whether or not to use Facebook Zero
* @param domainType {@link int} Which URL type we are using.
*/
// TODO: time to fix this mess
private void setupFacebookWebViewConfig(boolean force, boolean mobile, boolean facebookBasic,
boolean facebookZero, boolean facebookOnion) {
if (force && !mobile) {
// Force the desktop site to load
mDomainToUse = INIT_URL_DESKTOP;
} else if (facebookZero) {
// If Facebook zero is set, use that
mDomainToUse = INIT_URL_FACEBOOK_ZERO;
} else if (facebookOnion) {
// If the Onion domain is set, use that
mDomainToUse = INIT_URL_FACEBOOK_ONION;
} else {
// Otherwise, just load the mobile site for all devices
mDomainToUse = INIT_URL_MOBILE;
private void setupFacebookWebViewConfig(int domainType) {
switch (domainType) {
case DESKTOP:
// Force the desktop site to load
mDomainToUse = INIT_URL_DESKTOP;
mDomainSuffix = INIT_URL_NORMAL_SUFFIX;
break;
case ZERO:
// If Facebook zero is set, use that
mDomainToUse = INIT_URL_FACEBOOK_ZERO;
mDomainSuffix = INIT_URL_NORMAL_SUFFIX;
break;
case ONION:
// If the Onion domain is set, use that
mDomainToUse = INIT_URL_FACEBOOK_ONION;
mDomainSuffix = INIT_URL_NORMAL_SUFFIX;
break;
case BASIC:
// Found a URL that can replace the Firefox for Android UA
mDomainToUse = INIT_URL_FACEBOOK_BASIC;
mDomainSuffix = INIT_URL_NORMAL_SUFFIX;
break;
case MOBILE:
mDomainSuffix = INIT_URL_MOBILE_SUFFIX;
// Otherwise, just load the mobile site for all devices
break;
}

// Set the user agent depending on config
setUserAgent(force, mobile, facebookBasic);
setUserAgent(domainType);
}

/**
Expand Down Expand Up @@ -335,55 +322,50 @@ private boolean isDeviceTablet() {
return isTablet;
}

/**
* Menu drawer button listener interface
*/
private class MenuDrawerButtonListener implements View.OnClickListener {
/**
* {@inheritDoc}
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.menu_item_jump_to_top:
public void drawerClick(int position) {
switch (position) {
case 0:
jumpToTop();
break;
case R.id.menu_item_refresh:
case 1:
refreshCurrentPage();
break;
case R.id.menu_item_newsfeed:
loadNewPage(mDomainToUse + "?sk=h_chr");
case 2:
loadNewPage(mDomainToUse + mDomainSuffix);
break;
case R.id.menu_items_notifications:
case 3:
loadNewPage(mDomainToUse + URL_PAGE_NOTIFICATIONS);
break;
case R.id.menu_item_messages:
case 4:
loadNewPage(mDomainToUse + URL_PAGE_MESSAGES);
break;
case R.id.menu_share_this:
case 5:
shareCurrentPage();
break;
case R.id.menu_preferences:
case 6:
startActivity(new Intent(FbWrapper.this, FacebookPreferences.class));

break;
case R.id.menu_about:
case 7:
showAboutAlert();
break;
case R.id.menu_kill:
case 8:
mWebViewContainer.removeView(mWebView);
destroyWebView();
finish();
break;
}
closeMenuDrawer();
}

}

/**
* Show an alert dialog with the information about the application.
*/
private void showAboutAlert() {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();


alertDialog.setTitle(getString(R.string.menu_about));
alertDialog.setMessage(getString(R.string.txt_about));
alertDialog.setIcon(R.drawable.ic_launcher);
Expand Down Expand Up @@ -417,4 +399,5 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* Base activity that uses a {@link FacebookWebView} to load the Facebook
* site in different formats. Here we can implement all the boilerplate code
* that has to do with loading the activity as well as lifecycle events.
* <p/>
* <p>
* See {@link #onActivityCreated()}
* See {@link #onWebViewInit(android.os.Bundle)}
* See {@link #onResumeActivity()}
Expand All @@ -82,9 +82,18 @@ public abstract class BaseFacebookWebViewActivity extends Activity implements
protected final static String INIT_URL_MOBILE = "https://m.facebook.com";
protected final static String INIT_URL_DESKTOP = "https://www.facebook.com";
protected final static String INIT_URL_FACEBOOK_ZERO = "https://0.facebook.com";
protected final static String INIT_URL_FACEBOOK_BASIC = "https://mbasic.facebook.com/";
protected final static String INIT_URL_FACEBOOK_ONION = "https://facebookcorewwwi.onion";
// Because Facebook's new mobile site needs the /home.php to properly force Most Recent.
protected final static String INIT_URL_NORMAL_SUFFIX = "?sk=h_chr";
protected final static String INIT_URL_MOBILE_SUFFIX = "/home.php?sk=h_chr";
protected final static String URL_PAGE_NOTIFICATIONS = "/notifications.php";
protected final static String URL_PAGE_MESSAGES = "/messages";
protected final static int ZERO = 0;
protected final static int BASIC = 1;
protected final static int DESKTOP = 2;
protected final static int MOBILE = 3;
protected final static int ONION = 4;

// URL for Sharing Links
// u = url & t = title
Expand All @@ -99,8 +108,6 @@ public abstract class BaseFacebookWebViewActivity extends Activity implements
// Mobile user agent (Mobile user agent from a Google Nexus 5 running Android 4.4.2
protected static final String USER_AGENT_MOBILE = "Mozilla/5.0 (Linux; Android 5.0; Nexus 5 Build/LRX21O) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36";
// Firefox for Android user agent, it brings up a basic version of the site. Halfway between touch site and zero site.
protected static final String USER_AGENT_BASIC = "Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0";

// Members
protected ConnectivityManager mConnectivityManager = null;
Expand Down Expand Up @@ -309,31 +316,24 @@ protected void restoreWebView(Bundle inState) {
}

/**
* Set the browser user agent to be used. If the user agent should be forced,
* make sure the 'force' param is set to true, otherwise the devices' default
* user agent will be used.
* Set the browser user agent to be used.
*
* @param force {@link boolean}
* true if we should force a custom user agent, false if not.
* Note, if this flag is false the default user agent will be
* used while disregarding the mobile {@link boolean} parameter
* @param mobile {@link boolean}
* true if we should use a custom user agent for mobile devices,
* false if not.
*/
protected void setUserAgent(boolean force, boolean mobile, boolean facebookBasic) {
if (force && mobile && !facebookBasic) {
* @param type {@link int} Which user agent to use.
*
*/
protected void setUserAgent(int type) {
if (type == MOBILE) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mWebSettings.setUserAgentString(USER_AGENT_MOBILE_OLD);
} else {
mWebSettings.setUserAgentString(USER_AGENT_MOBILE);
}
} else if (force && !mobile && !facebookBasic) {
} else if (type == DESKTOP) {
mWebSettings.setUserAgentString(USER_AGENT_DESKTOP);
} else if (force && mobile && facebookBasic) {
mWebSettings.setUserAgentString(USER_AGENT_BASIC);
} else {
mWebSettings.setUserAgentString(null);
}
// Basic, Onion, Auto and Zero do not need a user agent, so use the system default.
else {
mWebSettings.setUserAgentString(WebSettings.getDefaultUserAgent(this));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.danvelazco.fbwrapper.activity;

import android.app.ListFragment;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.danvelazco.fbwrapper.FbWrapper;
import com.danvelazco.fbwrapper.R;
import com.danvelazco.fbwrapper.util.MySimpleArrayAdapter;

/**
* A ListFragment that replaces the million OnClickListeners previously used in the Drawer.
*/
public class DrawerFragment extends ListFragment {
private String[] mPlanetTitles;
private int[] mIcons;



public DrawerFragment() {}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater.inflate(R.layout.drawer_fragment, container, false);

mPlanetTitles = getResources().getStringArray(R.array.drawer_items);
TypedArray ar = getResources().obtainTypedArray(R.array.drawer_item_icons);
int len = ar.length();
mIcons = new int[len];
for (int i = 0; i < len; i++)
mIcons[i] = ar.getResourceId(i, 0);

ar.recycle();
// Set the adapter for the list view
setListAdapter(new MySimpleArrayAdapter(getActivity(), mPlanetTitles, mIcons));
// Set the list's click listener

return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
getListView().setDividerHeight(0);
getListView().setDivider(new ColorDrawable(android.R.color.transparent));
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
((FbWrapper)getActivity()).drawerClick(position);
}

}
Loading