Skip to content

Commit

Permalink
Change clearing app cookies method.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdryomov committed Jul 31, 2014
1 parent 7131086 commit de77d39
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/main/java/org/amahi/anywhere/activity/ServerAppActivity.java
Expand Up @@ -36,6 +36,7 @@
import org.amahi.anywhere.server.model.ServerApp;
import org.amahi.anywhere.util.Android;
import org.amahi.anywhere.util.Intents;
import org.amahi.anywhere.util.Preferences;
import org.amahi.anywhere.util.ViewDirector;

import java.util.Locale;
Expand All @@ -54,13 +55,30 @@ protected void onCreate(Bundle savedInstanceState) {

setUpInjections();

setUpAppHistory();
setUpApp(savedInstanceState);
}

private void setUpInjections() {
AmahiApplication.from(this).inject(this);
}

private void setUpAppHistory() {
String previousAppHost = Preferences.with(this).getLatestOpenedAppHost();
String currentAppHost = getApp().getHost();

if (!previousAppHost.equals(currentAppHost)) {
tearDownAppState();
}
}

private void tearDownAppState() {
CookieSyncManager.createInstance(this);

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}

private void setUpApp(Bundle state) {
setUpAppWebAgent();
setUpAppWebClient();
Expand Down Expand Up @@ -186,16 +204,14 @@ protected void onDestroy() {
getWebView().destroy();

if (isFinishing()) {
tearDownAppWebViewState();
tearDownAppHistory();
}
}

private void tearDownAppWebViewState() {
CookieSyncManager.createInstance(this);
private void tearDownAppHistory() {
String currentAppHost = getApp().getHost();

CookieManager cookieManager = CookieManager.getInstance();

cookieManager.removeAllCookie();
Preferences.with(this).setLatestOpenedAppHost(currentAppHost);
}

private static final class AppWebClient extends WebViewClient
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/amahi/anywhere/util/Preferences.java
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2014 Amahi
*
* This file is part of Amahi.
*
* Amahi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Amahi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Amahi. If not, see <http ://www.gnu.org/licenses/>.
*/

package org.amahi.anywhere.util;

import android.content.Context;
import android.content.SharedPreferences;

public final class Preferences
{
private static final class Locations
{
private Locations() {
}

public static final String STATE = "state";
}

private static final class Keys
{
private Keys() {
}

public static final String LATEST_OPENED_APP_HOST = "latest_opened_app_host";
}

private static final class Defaults
{
private Defaults() {
}

public static final String STRING = "";
}

private final SharedPreferences preferences;

public static Preferences with(Context context) {
return new Preferences(context);
}

private Preferences(Context context) {
this.preferences = context.getSharedPreferences(Locations.STATE, Context.MODE_PRIVATE);
}

public String getLatestOpenedAppHost() {
return getString(Keys.LATEST_OPENED_APP_HOST);
}

private String getString(String key) {
return preferences.getString(key, Defaults.STRING);
}

public void setLatestOpenedAppHost(String appHost) {
setString(Keys.LATEST_OPENED_APP_HOST, appHost);
}

private void setString(String key, String value) {
preferences.edit().putString(key, value).apply();
}
}

0 comments on commit de77d39

Please sign in to comment.