Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
I hope it's ok now #155
Browse files Browse the repository at this point in the history
  • Loading branch information
indywidualny committed Apr 30, 2016
1 parent d748f6d commit b422a87
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.text.DateFormat;
import java.util.Date;

@SuppressWarnings("UnusedDeclaration")
public class MainActivity extends Activity {

// reference to this object
Expand Down Expand Up @@ -114,6 +115,10 @@ public class MainActivity extends Activity {
private static String userAgentDefault;
private static final String USER_AGENT_BASIC = "Mozilla/5.0 (Linux; U; Android 2.3.3; en-gb; " +
"Nexus S Build/GRI20) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
public static final String USER_AGENT_MESSENGER = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";

public static final String MESSENGER_URL = "https://www.messenger.com/login";

@Override
@SuppressLint("setJavaScriptEnabled")
Expand Down Expand Up @@ -302,8 +307,11 @@ else if (getResources().getConfiguration().orientation == Configuration.ORIENTAT
//noinspection ConstantConditions
if (getIntent().getExtras().getString("start_url") != null) {
String temp = getIntent().getExtras().getString("start_url");
if (!isFacebookZero || !isConnectedMobile)
if (!isFacebookZero || !isConnectedMobile) {
webViewUrl = temp;
if (webViewUrl != null && webViewUrl.equals(MESSENGER_URL))
webView.getSettings().setUserAgentString(MainActivity.USER_AGENT_MESSENGER);
}
// cancel all notifications if 'All notifications' button was clicked
if ("https://m.facebook.com/notifications".equals(temp))
NotificationsService.cancelAllNotifications();
Expand Down Expand Up @@ -789,12 +797,20 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)

// when a drawer item is clicked do instructions from below
private void selectItem(int position, String baseAddress) {
webView.stopLoading();
setUserAgent();

switch (position) {
case 0:
webView.loadUrl(baseAddress);
break;
case 1:
webView.loadUrl(baseAddress + "messages");
if (preferences.getBoolean("facebook_zero", false) && Connectivity.isConnectedMobile(this)) {
Toast.makeText(getApplicationContext(), getString(R.string.facebook_zero_active), Toast.LENGTH_SHORT).show();
break;
}
webView.getSettings().setUserAgentString(MainActivity.USER_AGENT_MESSENGER);
webView.loadUrl(MESSENGER_URL);
break;
case 2:
webView.loadUrl(baseAddress + "buddylist.php");
Expand Down Expand Up @@ -870,10 +886,7 @@ protected void onNewIntent(Intent intent) {
boolean isFacebookZero = preferences.getBoolean("facebook_zero", false);

// set the right user agent
if (preferences.getBoolean("basic_mode", false))
webView.getSettings().setUserAgentString(USER_AGENT_BASIC);
else
webView.getSettings().setUserAgentString(userAgentDefault);
setUserAgent();

/** get a subject and text and check if this is a link trying to be shared */
String sharedSubject = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
Expand Down Expand Up @@ -902,18 +915,24 @@ protected void onNewIntent(Intent intent) {

/** if opened by a notification or a shortcut */
try {
if (getIntent().getExtras().getString("start_url") != null)
if (getIntent().getExtras().getString("start_url") != null) {
webViewUrl = getIntent().getExtras().getString("start_url");
// cancel all notifications if 'All notifications' button was clicked
if ("https://m.facebook.com/notifications".equals(webViewUrl))
NotificationsService.cancelAllNotifications();
}
} catch (Exception ignored) {}

/** load a grabbed url instead of the current page */
if (isFacebookZero && isConnectedMobile)
Toast.makeText(getApplicationContext(), getString(R.string.facebook_zero_active), Toast.LENGTH_SHORT).show();
else
else {
if (webViewUrl != null && webViewUrl.equals(MESSENGER_URL))
webView.getSettings().setUserAgentString(MainActivity.USER_AGENT_MESSENGER);
else
setUserAgent();
webView.loadUrl(webViewUrl);
}

// notify when there is no internet connection
if (!Connectivity.isConnected(getApplicationContext()) && !preferences.getBoolean("offline_mode", false))
Expand All @@ -940,9 +959,13 @@ protected void onNewIntent(Intent intent) {
public void onBackPressed() {
if (inCustomView())
hideCustomView();
else if (mCustomView == null && webView.canGoBack())
else if (mCustomView == null && webView.canGoBack()) {
if (MyWebViewClient.currentlyLoadedPage.contains("messenger.com")) {
webView.getSettings().setUserAgentString(MainActivity.USER_AGENT_MESSENGER);
} else
setUserAgent();
webView.goBack();
else
} else
super.onBackPressed();
}

Expand Down Expand Up @@ -1095,8 +1118,20 @@ public void onClick(View view) {
dialog.show();
}

private void setUserAgent() {
// set the right user agent
if (preferences.getBoolean("basic_mode", false))
webView.getSettings().setUserAgentString(USER_AGENT_BASIC);
else
webView.getSettings().setUserAgentString(userAgentDefault);
}

public static Activity getMainActivity() {
return mainActivity;
}

public SwipeRefreshLayout getSwipeRefreshLayout() {
return swipeRefreshLayout;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void onCreate(Bundle savedInstanceState) {

Intent intent = new Intent();
Intent launchIntent = new Intent(this, MainActivity.class);
launchIntent.putExtra("start_url", "https://m.facebook.com/messages");
launchIntent.putExtra("start_url", MainActivity.MESSENGER_URL);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.messages));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_messages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,17 @@ public static void setWebviewReference(WebView wv) {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// really ugly but let's do it just to avoid rare crashes
try {
// handling external links as intents
if (Uri.parse(url).getHost().endsWith("messenger.com")) {
view.getSettings().setUseWideViewPort(false);
((MainActivity) MainActivity.getMainActivity()).getSwipeRefreshLayout().setEnabled(false);
return false;
} else {
view.getSettings().setUseWideViewPort(true);
((MainActivity) MainActivity.getMainActivity()).getSwipeRefreshLayout().setEnabled(true);
}

if (Uri.parse(url).getHost().endsWith("facebook.com")
|| Uri.parse(url).getHost().endsWith("mobile.facebook.com")
|| Uri.parse(url).getHost().endsWith("m.facebook.com")
|| Uri.parse(url).getHost().endsWith("h.facebook.com")
|| Uri.parse(url).getHost().endsWith("l.facebook.com")
Expand All @@ -89,6 +98,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
|| Uri.parse(url).getHost().endsWith("media.giphy.com"))) {
return false;
}

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
try {
view.getContext().startActivity(intent);
Expand Down Expand Up @@ -193,6 +203,13 @@ else if (Uri.parse(url).getHost().endsWith("0.facebook.com"))
"node.innerHTML = str; document.body.appendChild(node); } " +
"addStyleString('.img, ._5sgg, ._-_a, .widePic, .profile-icon{ display: none; }');");

// hide install messenger notice at messenger page
if (url.contains("messenger.com")) {
view.loadUrl("javascript:function addStyleString(str) { var node = document.createElement('style'); " +
"node.innerHTML = str; document.body.appendChild(node); } " +
"addStyleString('._s15{ display: none; }');");
}

// offline mode
if (preferences.getBoolean("offline_mode", false)) {
if (offline == null)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.android.tools.build:gradle:2.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit b422a87

Please sign in to comment.