diff --git a/app/build.gradle b/app/build.gradle index 522d12b7..fe6aeb90 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 16 targetSdkVersion 25 versionCode 1 - versionName "0.12" + versionName "0.14" } buildTypes { release { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java index 2c31a71a..bb457ced 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java @@ -1,7 +1,6 @@ package gr.thmmy.mthmmy.activities; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import gr.thmmy.mthmmy.BuildConfig; 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 23ff308d..483787e8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java @@ -1,12 +1,13 @@ package gr.thmmy.mthmmy.activities; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; +import gr.thmmy.mthmmy.utils.Thmmy; import okhttp3.CookieJar; import okhttp3.OkHttpClient; @@ -14,10 +15,12 @@ public class BaseActivity extends AppCompatActivity { private static boolean init =false; //To initialize stuff only once per app start - private static OkHttpClient client; + protected static OkHttpClient client; protected static CookieJar cookieJar; protected static SharedPrefsCookiePersistor sharedPrefsCookiePersistor; + protected static Thmmy.LoginData loginData; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -29,6 +32,8 @@ public class BaseActivity extends AppCompatActivity { client = new OkHttpClient.Builder() .cookieJar(cookieJar) .build(); + loginData = new Thmmy.LoginData(); + loginData.setStatus(0); init =true; } @@ -46,4 +51,8 @@ public class BaseActivity extends AppCompatActivity { public static OkHttpClient getClient() { return client; } + + public void setLoginData(Thmmy.LoginData loginData) { + this.loginData = loginData; + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java index 73760677..4647a7a8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java @@ -16,11 +16,11 @@ import gr.thmmy.mthmmy.R; import static gr.thmmy.mthmmy.utils.Thmmy.CERTIFICATE_ERROR; import static gr.thmmy.mthmmy.utils.Thmmy.FAILED; -import static gr.thmmy.mthmmy.utils.Thmmy.OK; +import static gr.thmmy.mthmmy.utils.Thmmy.LOGGED_IN; import static gr.thmmy.mthmmy.utils.Thmmy.OTHER_ERROR; import static gr.thmmy.mthmmy.utils.Thmmy.WRONG_PASSWORD; import static gr.thmmy.mthmmy.utils.Thmmy.WRONG_USER; -import static gr.thmmy.mthmmy.utils.Thmmy.authenticate; +import static gr.thmmy.mthmmy.utils.Thmmy.login; public class LoginActivity extends BaseActivity { private static final String TAG = "LoginActivity"; @@ -63,7 +63,7 @@ public class LoginActivity extends BaseActivity { String password = inputPassword.getText().toString().trim(); // login user try { - switch (new loginAsync().execute(username, password).get()) { + switch (new LoginTask().execute(username, password).get()) { case WRONG_USER: Toast.makeText(getApplicationContext(), "Wrong username!", Toast.LENGTH_LONG) @@ -89,7 +89,7 @@ public class LoginActivity extends BaseActivity { "Check your connection!", Toast.LENGTH_LONG) .show(); break; - case OK: + case LOGGED_IN: progressDialog.dismiss(); Toast.makeText(getApplicationContext(), "Login successful!", Toast.LENGTH_LONG) @@ -129,11 +129,12 @@ public class LoginActivity extends BaseActivity { btnLogin.setEnabled(true); } - private class loginAsync extends AsyncTask { + private class LoginTask extends AsyncTask { @Override protected Integer doInBackground(String... params) { - return authenticate(params[0], params[1]); + setLoginData(login(params[0], params[1],"-1")); + return loginData.getStatus(); } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java index b7e53aee..3194dff4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java @@ -1,26 +1,31 @@ package gr.thmmy.mthmmy.activities; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; +import org.jsoup.nodes.Document; + import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.sections.recent.RecentFragment; +import gr.thmmy.mthmmy.utils.Thmmy; public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener { private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; /** The {@link ViewPager} that will host the section contents.*/ + private Menu menu; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -50,7 +55,14 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. + this.menu=menu; getMenuInflater().inflate(R.menu.menu_main, menu); + + if(loginData.getStatus()== Thmmy.LOGGED_IN) + hideLogin(); + else + hideLogout(); + return true; } @@ -59,15 +71,35 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF { int id = item.getItemId(); - if (id == R.id.action_about) { + if (id == R.id.action_about) + { Intent i = new Intent(MainActivity.this, AboutActivity.class); startActivity(i); return true; } + else if (id == R.id.action_logout) + new LogoutTask().execute(); return super.onOptionsItemSelected(item); } + private void hideLogin() + { + MenuItem login = menu.findItem(R.id.action_login); + MenuItem logout = menu.findItem(R.id.action_logout); + login.setVisible(false); + logout.setVisible(true); + } + + private void hideLogout() + { + MenuItem login = menu.findItem(R.id.action_login); + MenuItem logout = menu.findItem(R.id.action_logout); + login.setVisible(true); + logout.setVisible(false); + } + + @Override public void onFragmentInteraction(TopicSummary topicSummary) { @@ -112,4 +144,30 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF return null; } } + + + private class LogoutTask extends AsyncTask + { + protected Integer doInBackground(Void... voids) + { + return Thmmy.logout(loginData); + } + + + protected void onPreExecute() { + //TODO: a progressbar maybe? + } + + protected void onPostExecute(Integer result) + { + if(result==Thmmy.LOGGED_OUT) + hideLogout(); + else + hideLogin(); + } + + + + + } } 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 ee539c15..b153e47f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java @@ -3,7 +3,6 @@ package gr.thmmy.mthmmy.activities; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; @@ -14,10 +13,6 @@ import android.webkit.WebView; import android.widget.ProgressBar; import android.widget.TextView; -import gr.thmmy.mthmmy.R; -import gr.thmmy.mthmmy.data.Post; -import gr.thmmy.mthmmy.utils.CustomRecyclerView; - import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -26,7 +21,9 @@ import java.util.List; import javax.net.ssl.SSLHandshakeException; -import okhttp3.OkHttpClient; +import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.data.Post; +import gr.thmmy.mthmmy.utils.CustomRecyclerView; import okhttp3.Request; import okhttp3.Response; @@ -35,7 +32,6 @@ public class TopicActivity extends BaseActivity { private CustomRecyclerView recyclerView; private TopicAdapter topicAdapter; private ProgressBar progressBar; - private OkHttpClient client; private List postsList; @@ -60,7 +56,6 @@ public class TopicActivity extends BaseActivity { recyclerView.setLayoutManager(new LinearLayoutManager(findViewById(R.id.list).getContext())); recyclerView.setAdapter(topicAdapter); - 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 0240a1db..30c0431c 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 @@ -272,7 +272,7 @@ public class RecentFragment extends Fragment String title = recent.get(i).child(0).attr("title"); String lastUser = recent.get(i + 1).text(); - Pattern pattern = Pattern.compile("by (.*)"); + Pattern pattern = Pattern.compile("\\b (.*)"); Matcher matcher = pattern.matcher(lastUser); if (matcher.find()) lastUser = matcher.group(1); @@ -302,62 +302,6 @@ public class RecentFragment extends Fragment Log.e(TAG, "Parsing failed!"); return false; } - - // TODO: replace parse function with this when a method to get recent TOPICS and not POSTS becomes available -// private boolean parse(String document) throws XmlPullParserException, IOException { -// String text = null, tagName, link = null, title = null, poster = null,dateTime = null; -// boolean posterFlag = false; -// XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); -// factory.setNamespaceAware(true); -// XmlPullParser xpp = factory.newPullParser(); -// -// xpp.setInput( new StringReader(document) ); -// int eventType = xpp.getEventType(); -// topicSummaries.clear(); -// -// while (eventType != XmlPullParser.END_DOCUMENT) { -// tagName = xpp.getName(); -// switch (eventType) { -// case XmlPullParser.START_TAG: -// if (tagName.equals("poster")) -// posterFlag=true; -// break; -// -// case XmlPullParser.TEXT: -// text = xpp.getText(); -// break; -// -// case XmlPullParser.END_TAG: -// switch (tagName) -// { -// case "recent-post": -// topicSummaries.add(new TopicSummary(link, title, poster, dateTime)); -// break; -// case "name": -// if(posterFlag) { -// poster = text; -// posterFlag = false; -// } -// break; -// case "link": -// link = text; -// break; -// case "time": -// dateTime = text; -// break; -// case "subject": -// title = text; -// break; -// } -// break; -// -// default: -// break; -// } -// eventType = xpp.next(); -// } -// return true; -// } } } 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 e1a1d268..c00d94aa 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java @@ -32,7 +32,8 @@ public class Thmmy private static final HttpUrl loginUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=login2"); private static final HttpUrl indexUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php"); - public static final int OK = 1; + public static final int LOGGED_OUT = 0; + public static final int LOGGED_IN = 1; public static final int WRONG_USER= 2; public static final int WRONG_PASSWORD= 3; public static final int FAILED= 4; @@ -42,22 +43,6 @@ public class Thmmy - - public static int authenticate(String username, String password) - { - LoginData loginData = login(username, password, "60"); - - int status = loginData.getStatus(); - if (status==OK) - { - logout(loginData.getLogoutLink()); - return OK; - } - return status; - - } - - //-------------------------------------------LOGIN-------------------------------------------------- //Two options: (username, password, duration) or nothing - cookies public static LoginData login(String... strings) @@ -106,7 +91,7 @@ public class Thmmy setPersistentCookieSession(); loginData.setUsername(extractUserName(document)); loginData.setLogoutLink(HttpUrl.parse(logout.attr("href"))); - loginData.setStatus(OK); + loginData.setStatus(LOGGED_IN); } else { @@ -238,11 +223,11 @@ public class Thmmy //--------------------------------------LOGOUT-------------------------------------------------- - public static int logout(HttpUrl logoutLink) + public static int logout(LoginData loginData) { OkHttpClient client = BaseActivity.getClient(); Request request = new Request.Builder() - .url(logoutLink) + .url(loginData.getLogoutLink()) .build(); try { @@ -250,10 +235,12 @@ public class Thmmy Document document = Jsoup.parse(response.body().string()); Elements login = document.select("[value=Login]"); + ((PersistentCookieJar) BaseActivity.getCookieJar()).clear(); if(!login.isEmpty()) { Log.i("Logout", "Logout successful"); - return OK; + loginData.setStatus(LOGGED_OUT); + return LOGGED_OUT; } else { diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 7a6c2d23..7e6293a2 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -6,10 +6,17 @@ android:id="@+id/action_login" android:orderInCategory="100" android:title="@string/login" + android:visible="false" app:showAsAction="never" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bb9b066d..4f89287c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,4 +14,5 @@ logo Name Login + Logout