|
36 | 36 | import org.mozilla.vrbrowser.utils.InternalPages; |
37 | 37 |
|
38 | 38 | import java.util.ArrayList; |
| 39 | +import java.net.URI; |
| 40 | +import java.net.URISyntaxException; |
| 41 | +import java.util.ArrayDeque; |
| 42 | +import java.util.Deque; |
39 | 43 | import java.util.HashMap; |
40 | 44 | import java.util.LinkedHashMap; |
41 | 45 | import java.util.LinkedList; |
@@ -743,13 +747,55 @@ public int getUaMode() { |
743 | 747 | return mCurrentSession.getSettings().getUserAgentMode(); |
744 | 748 | } |
745 | 749 |
|
| 750 | + private static final String M_PREFIX = "m."; |
| 751 | + private static final String MOBILE_PREFIX = "mobile."; |
| 752 | + |
| 753 | + private String checkForMobileSite(String aUri) { |
| 754 | + String result = null; |
| 755 | + URI uri; |
| 756 | + try { |
| 757 | + uri = new URI(aUri); |
| 758 | + } catch (URISyntaxException e) { |
| 759 | + Log.d(LOGTAG, "Error parsing URL: " + aUri + " " + e.getMessage()); |
| 760 | + return null; |
| 761 | + } |
| 762 | + String authority = uri.getAuthority(); |
| 763 | + if (authority == null) { |
| 764 | + return null; |
| 765 | + } |
| 766 | + authority = authority.toLowerCase(); |
| 767 | + String foundPrefix = null; |
| 768 | + if (authority.startsWith(M_PREFIX)) { |
| 769 | + foundPrefix= M_PREFIX; |
| 770 | + } else if (authority.startsWith(MOBILE_PREFIX)) { |
| 771 | + foundPrefix = MOBILE_PREFIX; |
| 772 | + } |
| 773 | + if (foundPrefix != null) { |
| 774 | + try { |
| 775 | + uri = new URI(uri.getScheme(), authority.substring(foundPrefix.length()), uri.getPath(), uri.getQuery(), uri.getFragment()); |
| 776 | + result = uri.toString(); |
| 777 | + } catch (URISyntaxException e) { |
| 778 | + Log.d(LOGTAG, "Error dropping mobile prefix from: " + aUri + " " + e.getMessage()); |
| 779 | + } |
| 780 | + } |
| 781 | + return result; |
| 782 | + } |
| 783 | + |
746 | 784 | public void setUaMode(int mode) { |
747 | 785 | if (mCurrentSession != null) { |
748 | 786 | SessionState state = mSessions.get(mCurrentSession.hashCode()); |
749 | 787 | if (state != null && state.mSettings.getUserAgentMode() != mode) { |
750 | 788 | state.mSettings.setUserAgentMode(mode); |
751 | 789 | mCurrentSession.getSettings().setUserAgentMode(mode); |
752 | | - mCurrentSession.reload(); |
| 790 | + String overrideUri = null; |
| 791 | + if (mode == GeckoSessionSettings.USER_AGENT_MODE_DESKTOP) { |
| 792 | + state.mSettings.setViewportMode(GeckoSessionSettings.VIEWPORT_MODE_DESKTOP); |
| 793 | + overrideUri = checkForMobileSite(state.mUri); |
| 794 | + } else { |
| 795 | + state.mSettings.setViewportMode(GeckoSessionSettings.VIEWPORT_MODE_MOBILE); |
| 796 | + } |
| 797 | + mCurrentSession.getSettings().setViewportMode(state.mSettings.getViewportMode()); |
| 798 | + mCurrentSession.loadUri(overrideUri != null ? overrideUri : state.mUri, GeckoSession.LOAD_FLAGS_BYPASS_CACHE | GeckoSession.LOAD_FLAGS_REPLACE_HISTORY); |
753 | 799 | } |
754 | 800 | } |
755 | 801 | } |
|
0 commit comments