0

So, I started developing an app which is using ONE webview. The app has a sign in button, which, if you press it, directs you to the Steam login page of a website called csgolounge.com. I also implemented a navigation drawer, with which you should be able to navigate through the different menus of that website. Five hours ago, I came across the problem, that when I logged into Steam and switched via navigation drawer to another menu of that website (e.g. from www.csgolounge.com to www.csgolounge.com/myprofile), I'm not logged in anymore. The session also changes when I turn my mobile device! Until now, I couldn't figure it out and there was also no working solution for me here on stackoverflow!

I guess my problem is cookie related, so please help me :(

Here's the MainActivity:

public class HomeActivity extends Activity {

ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
public static WebView webview;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[9];

    drawerItem[0] = new ObjectDrawerItem(0, "My Profile");
    drawerItem[1] = new ObjectDrawerItem(0, "My Trades");
    drawerItem[2] = new ObjectDrawerItem(0, "My Offers");
    drawerItem[3] = new ObjectDrawerItem(0, "My Bets");
    drawerItem[4] = new ObjectDrawerItem(0, "Bookmarks");
    drawerItem[5] = new ObjectDrawerItem(0, "Search");
    drawerItem[6] = new ObjectDrawerItem(0, "Add Trade");
    drawerItem[7] = new ObjectDrawerItem(0, "Reddit");
    drawerItem[8] = new ObjectDrawerItem(0, "Rules");

    DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this,
            mDrawerLayout,
            R.drawable.ic_launcher,
            R.string.drawer_open,
            R.string.drawer_close
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
        }
        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    /** Buttons */
    final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.openDrawer(mDrawerList);
            } else {
                mDrawerLayout.closeDrawer(mDrawerList);
            }
        }
    });

    webview = (WebView) findViewById(R.id.webView);
    webview.setWebViewClient(new mWebViewClient());

    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setSavePassword(true);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("https://steamcommunity.com/openid/login?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=https%3A%2F%2Fcsgolounge.com%2Flogin&openid.realm=https%3A%2F%2Fcsgolounge.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select");
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

public static boolean itemSelected = false;
public static int itemSelectedNumber;

private void selectItem(int position) {
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    switch (position) {
        case 0:
            webview.loadUrl("http://www.csgolounge.com/myprofile");
            itemSelectedNumber = 0;
            break;
        case 1:
            webview.loadUrl("http://www.csgolounge.com/mytrades");
            itemSelectedNumber = 1;
            break;
        case 2:
            webview.loadUrl("http://www.csgolounge.com/myoffers");
            itemSelectedNumber = 2;
            break;
        case 3:
            webview.loadUrl("http://www.csgolounge.com");
            itemSelectedNumber = 3;
            break;
        case 4:
            webview.loadUrl("http://www.csgolounge.com/bookmarks");
            itemSelectedNumber = 4;
            break;
        case 5:
            webview.loadUrl("http://www.csgolounge.com/search");
            itemSelectedNumber = 5;
            break;
        case 6:
            webview.loadUrl("http://www.csgolounge.com/addtrade");
            itemSelectedNumber = 6;
            break;
        case 7:
            webview.loadUrl("http://www.reddit.com/r/csgolounge");
            itemSelectedNumber = 7;
            break;
        case 8:
            webview.loadUrl("http://www.csgolounge.com/rules");
            itemSelectedNumber = 8;
            break;

        default:
            break;
    }
    itemSelected = true;
    mDrawerLayout.closeDrawer(mDrawerList);
}

}

My LoginActivity, which just has a button, which, when you click on it, starts the HomeActivity:

public class LoginActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    final Button button = (Button) findViewById(R.id.btn_SignIn);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent logIn = new Intent(LoginActivity.this, HomeActivity.class);
            startActivity(logIn);
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

My WebViewClient(mViewClient) just overrides some functions to be able to block different elements from the csgolounge.com website when they are loading:

public class mWebViewClient extends WebViewClient {

public mWebViewClient() {
}

public boolean shouldOverrideUrlLoading(WebView webview, String s) {
    webview.loadUrl(s);
    return true;
}

public void onPageFinished(WebView view, String url){
    super.onPageFinished(view, url);
}

public void onLoadResource(WebView view, String url) {
    super.onLoadResource(view, url);
    if (HomeActivity.itemSelected == false && url.contains("csgolounge.com") && !url.contains("steamcommunity.com")) {
        view.loadUrl("javascript:(function() { " +
                "document.getElementsByTagName('section')[1].style.display = 'none'; " +
                //"document.getElementsByTagName('header')[0].style.display = 'none'; " +
                "document.getElementsByTagName('div')[4].style.display = 'none'; " +
                "document.getElementsByTagName('aside')[0].style.display = 'none';" +
                "})()");
    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 0) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 1) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 2) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 3) {
        view.loadUrl("javascript:(function() { " +
                "document.getElementsByTagName('section')[0].style.display = 'none'; " +
                "document.getElementsByTagName('header')[0].style.display = 'none'; " +
                "document.getElementsByTagName('div')[4].style.display = 'none'; " +
                "document.getElementsByTagName('aside')[0].style.display = 'none';" +
                "})()");
    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 4) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 5) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 6) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 7) {

    }
    else if (HomeActivity.itemSelected == true && HomeActivity.itemSelectedNumber == 8) {

    }
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    //Users will be notified in case there's an error (i.e. no internet connection)
    Toast.makeText(view.getContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}

}

ObjectDrawerItem Class:

public class ObjectDrawerItem {

public int icon;
public String name;

// Constructor.
public ObjectDrawerItem(int icon, String name) {

    this.icon = icon;
    this.name = name;
}

}

DrawerItemCustomAdapter Class:

public class DrawerItemCustomAdapter extends ArrayAdapter<ObjectDrawerItem> {

Context mContext;
int layoutResourceId;
ObjectDrawerItem data[] = null;

public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, ObjectDrawerItem[] data) {

    super(mContext, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItem = convertView;

    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    listItem = inflater.inflate(layoutResourceId, parent, false);

    ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
    TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

    ObjectDrawerItem folder = data[position];


    imageViewIcon.setImageResource(folder.icon);
    textViewName.setText(folder.name);

    return listItem;
}

}

HomeActivity Design: http://prntscr.com/62balt http://prntscr.com/62bb7u And text:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="false"
android:background="#FF535353">
<!-- The main content view -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity"
    android:weightSum="1">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="MYCSGOLOUNGE"
            android:id="@+id/text_Header"
            android:gravity="center"
            android:background="#ffff9836"
            android:editable="false"
            android:typeface="sans"
            android:layout_gravity="center_horizontal"
            android:textSize="24dp"
            android:visibility="visible"
            android:textColor="#ffffffff"
            android:textIsSelectable="false" />

        <ImageButton
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:layout_alignBottom="@+id/text_Header"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_menu" />

        <WebView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/webView"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/text_Header" />
    </RelativeLayout>

</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#ffff9836"/>

dahwN
  • 67
  • 1
  • 1
  • 8
  • Where is the code creating the web view? Could it be that cookieManager.removeAllCookie() is running when you select a menu item? – devnate Feb 07 '15 at 02:49
  • See this to solve the problem when rotating your phone: http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android – devnate Feb 07 '15 at 02:51
  • @devnate The code creating the webview is inside my onCreate method, I just navigate to the new url when I select a new navigation drawer item. – dahwN Feb 07 '15 at 15:37
  • If you are loading the url on the same webview, it should have the same cookies. I suspect you are somehow using a different one than you signed in (I don't understand what's going on with all your different fragments). I'd have to see all of your app's code to be able to help further. – devnate Feb 07 '15 at 19:25
  • @devnate I'll post everything right now! – dahwN Feb 07 '15 at 19:27
  • Also, `HomeActivity.fragment == new ProfileFragment()` probably isn't doing what you thing it is. You're probably looking for something like `HomeActivity.fragment instanceof ProfileFragment` – devnate Feb 07 '15 at 19:27
  • @devnate So, I've added all code :) – dahwN Feb 07 '15 at 20:27
  • Thanks, can I also see the main activity layout and one of the fragment's layout xml? – devnate Feb 07 '15 at 20:43
  • @devnate Added it at the bottom! – dahwN Feb 07 '15 at 21:12

1 Answers1

0

The fragment stuff threw me off, but it looks like it's because the URLs start with www so it thinks it's a different domain. Changing it to webview.loadUrl("http://csgolounge.com/myprofile"); fixes the issue for me.

devnate
  • 744
  • 5
  • 8
  • So, I completely removed the fragments from my app (I updated the code in my main post) and now implemented a boolean and an integer to check if an item and which item of the navigation drawer is selected, but I still have the same problem.. :( – dahwN Feb 07 '15 at 22:18
  • Alright I copied your code in so I could reproduce the issue and updated my solution. Sorry for the confusion. – devnate Feb 07 '15 at 23:51