From 2e6f29c60ce1b8635563d4b588bab6467ab00824 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 12 Nov 2016 21:41:28 +0200 Subject: [PATCH] -OkHttpClient initializes only once (small optimization) --- .../gr/thmmy/mthmmy/activities/BaseActivity.java | 16 +++++++++++++--- .../thmmy/mthmmy/activities/TopicActivity.java | 2 +- .../mthmmy/sections/recent/RecentFragment.java | 4 ++-- .../main/java/gr/thmmy/mthmmy/utils/Thmmy.java | 11 +++-------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java index f06396fd..23ff308d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java @@ -8,10 +8,13 @@ import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import okhttp3.CookieJar; +import okhttp3.OkHttpClient; public class BaseActivity extends AppCompatActivity { - private static boolean cookiesInit=false; //To initialize cookie stuff only once per app start + private static boolean init =false; //To initialize stuff only once per app start + + private static OkHttpClient client; protected static CookieJar cookieJar; protected static SharedPrefsCookiePersistor sharedPrefsCookiePersistor; @@ -19,11 +22,14 @@ public class BaseActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(!cookiesInit) + if(!init) { sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(BaseActivity.this); cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); - cookiesInit=true; + client = new OkHttpClient.Builder() + .cookieJar(cookieJar) + .build(); + init =true; } } @@ -36,4 +42,8 @@ public class BaseActivity extends AppCompatActivity { public static SharedPrefsCookiePersistor getSharedPrefsCookiePersistor() { return sharedPrefsCookiePersistor; } + + public static OkHttpClient getClient() { + return client; + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java index 003ec965..ee539c15 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java @@ -60,7 +60,7 @@ public class TopicActivity extends BaseActivity { recyclerView.setLayoutManager(new LinearLayoutManager(findViewById(R.id.list).getContext())); recyclerView.setAdapter(topicAdapter); - client = new OkHttpClient.Builder().build(); + client = BaseActivity.getClient(); new TopicTask().execute(extras.getString("TOPIC_URL")); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java index 69f7e100..0240a1db 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java @@ -15,6 +15,7 @@ import android.widget.RelativeLayout; import android.widget.Toast; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.activities.BaseActivity; import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.utils.CustomRecyclerView; @@ -88,8 +89,7 @@ public class RecentFragment extends Fragment sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER); - client = new OkHttpClient.Builder().build(); - + client = BaseActivity.getClient(); topicSummaries = new ArrayList<>(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java b/app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java index acabca99..e1a1d268 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java @@ -91,10 +91,7 @@ public class Thmmy .build(); } - OkHttpClient client = new OkHttpClient.Builder() - .cookieJar(BaseActivity.getCookieJar()) - .build(); - + OkHttpClient client = BaseActivity.getClient(); try { @@ -151,6 +148,7 @@ public class Thmmy } + //To maintain data between activities/ between activity state change (possibly temporary solution) public static class LoginData implements Parcelable { private int status; @@ -240,12 +238,9 @@ public class Thmmy //--------------------------------------LOGOUT-------------------------------------------------- - //Two options: (username, password, duration) or nothing - cookies public static int logout(HttpUrl logoutLink) { - OkHttpClient client = new OkHttpClient.Builder() - .cookieJar(BaseActivity.getCookieJar()) //cookies will be deleted - .build(); + OkHttpClient client = BaseActivity.getClient(); Request request = new Request.Builder() .url(logoutLink) .build();