Browse Source

Version 0.14: Added logout and some login/logout tweaks

pull/24/head
Ezerous 8 years ago
parent
commit
7d9d29ba77
  1. 2
      app/build.gradle
  2. 1
      app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java
  3. 13
      app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java
  4. 13
      app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java
  5. 62
      app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java
  6. 11
      app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java
  7. 58
      app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java
  8. 29
      app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java
  9. 9
      app/src/main/res/menu/menu_main.xml
  10. 1
      app/src/main/res/values/strings.xml

2
app/build.gradle

@ -9,7 +9,7 @@ android {
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 25 targetSdkVersion 25
versionCode 1 versionCode 1
versionName "0.12" versionName "0.14"
} }
buildTypes { buildTypes {
release { release {

1
app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java

@ -1,7 +1,6 @@
package gr.thmmy.mthmmy.activities; package gr.thmmy.mthmmy.activities;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView; import android.widget.TextView;
import gr.thmmy.mthmmy.BuildConfig; import gr.thmmy.mthmmy.BuildConfig;

13
app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java

@ -1,12 +1,13 @@
package gr.thmmy.mthmmy.activities; package gr.thmmy.mthmmy.activities;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import gr.thmmy.mthmmy.utils.Thmmy;
import okhttp3.CookieJar; import okhttp3.CookieJar;
import okhttp3.OkHttpClient; 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 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 CookieJar cookieJar;
protected static SharedPrefsCookiePersistor sharedPrefsCookiePersistor; protected static SharedPrefsCookiePersistor sharedPrefsCookiePersistor;
protected static Thmmy.LoginData loginData;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -29,6 +32,8 @@ public class BaseActivity extends AppCompatActivity {
client = new OkHttpClient.Builder() client = new OkHttpClient.Builder()
.cookieJar(cookieJar) .cookieJar(cookieJar)
.build(); .build();
loginData = new Thmmy.LoginData();
loginData.setStatus(0);
init =true; init =true;
} }
@ -46,4 +51,8 @@ public class BaseActivity extends AppCompatActivity {
public static OkHttpClient getClient() { public static OkHttpClient getClient() {
return client; return client;
} }
public void setLoginData(Thmmy.LoginData loginData) {
this.loginData = loginData;
}
} }

13
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.CERTIFICATE_ERROR;
import static gr.thmmy.mthmmy.utils.Thmmy.FAILED; 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.OTHER_ERROR;
import static gr.thmmy.mthmmy.utils.Thmmy.WRONG_PASSWORD; 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.WRONG_USER;
import static gr.thmmy.mthmmy.utils.Thmmy.authenticate; import static gr.thmmy.mthmmy.utils.Thmmy.login;
public class LoginActivity extends BaseActivity { public class LoginActivity extends BaseActivity {
private static final String TAG = "LoginActivity"; private static final String TAG = "LoginActivity";
@ -63,7 +63,7 @@ public class LoginActivity extends BaseActivity {
String password = inputPassword.getText().toString().trim(); String password = inputPassword.getText().toString().trim();
// login user // login user
try { try {
switch (new loginAsync().execute(username, password).get()) { switch (new LoginTask().execute(username, password).get()) {
case WRONG_USER: case WRONG_USER:
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Wrong username!", Toast.LENGTH_LONG) "Wrong username!", Toast.LENGTH_LONG)
@ -89,7 +89,7 @@ public class LoginActivity extends BaseActivity {
"Check your connection!", Toast.LENGTH_LONG) "Check your connection!", Toast.LENGTH_LONG)
.show(); .show();
break; break;
case OK: case LOGGED_IN:
progressDialog.dismiss(); progressDialog.dismiss();
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Login successful!", Toast.LENGTH_LONG) "Login successful!", Toast.LENGTH_LONG)
@ -129,11 +129,12 @@ public class LoginActivity extends BaseActivity {
btnLogin.setEnabled(true); btnLogin.setEnabled(true);
} }
private class loginAsync extends AsyncTask<String, Void, Integer> { private class LoginTask extends AsyncTask<String, Void, Integer> {
@Override @Override
protected Integer doInBackground(String... params) { protected Integer doInBackground(String... params) {
return authenticate(params[0], params[1]); setLoginData(login(params[0], params[1],"-1"));
return loginData.getStatus();
} }
@Override @Override

62
app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java

@ -1,26 +1,31 @@
package gr.thmmy.mthmmy.activities; package gr.thmmy.mthmmy.activities;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import org.jsoup.nodes.Document;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.data.TopicSummary;
import gr.thmmy.mthmmy.sections.recent.RecentFragment; import gr.thmmy.mthmmy.sections.recent.RecentFragment;
import gr.thmmy.mthmmy.utils.Thmmy;
public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener
{ {
private SectionsPagerAdapter mSectionsPagerAdapter; private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager; /** The {@link ViewPager} that will host the section contents.*/ private ViewPager mViewPager; /** The {@link ViewPager} that will host the section contents.*/
private Menu menu;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -50,7 +55,14 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
this.menu=menu;
getMenuInflater().inflate(R.menu.menu_main, menu); getMenuInflater().inflate(R.menu.menu_main, menu);
if(loginData.getStatus()== Thmmy.LOGGED_IN)
hideLogin();
else
hideLogout();
return true; return true;
} }
@ -59,15 +71,35 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
{ {
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.action_about) { if (id == R.id.action_about)
{
Intent i = new Intent(MainActivity.this, AboutActivity.class); Intent i = new Intent(MainActivity.this, AboutActivity.class);
startActivity(i); startActivity(i);
return true; return true;
} }
else if (id == R.id.action_logout)
new LogoutTask().execute();
return super.onOptionsItemSelected(item); 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 @Override
public void onFragmentInteraction(TopicSummary topicSummary) public void onFragmentInteraction(TopicSummary topicSummary)
{ {
@ -112,4 +144,30 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
return null; return null;
} }
} }
private class LogoutTask extends AsyncTask<Void, Void, Integer>
{
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();
}
}
} }

11
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.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
@ -14,10 +13,6 @@ import android.webkit.WebView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; 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.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
@ -26,7 +21,9 @@ import java.util.List;
import javax.net.ssl.SSLHandshakeException; 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.Request;
import okhttp3.Response; import okhttp3.Response;
@ -35,7 +32,6 @@ public class TopicActivity extends BaseActivity {
private CustomRecyclerView recyclerView; private CustomRecyclerView recyclerView;
private TopicAdapter topicAdapter; private TopicAdapter topicAdapter;
private ProgressBar progressBar; private ProgressBar progressBar;
private OkHttpClient client;
private List<Post> postsList; private List<Post> postsList;
@ -60,7 +56,6 @@ public class TopicActivity extends BaseActivity {
recyclerView.setLayoutManager(new LinearLayoutManager(findViewById(R.id.list).getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(findViewById(R.id.list).getContext()));
recyclerView.setAdapter(topicAdapter); recyclerView.setAdapter(topicAdapter);
client = BaseActivity.getClient();
new TopicTask().execute(extras.getString("TOPIC_URL")); new TopicTask().execute(extras.getString("TOPIC_URL"));
} }

58
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 title = recent.get(i).child(0).attr("title");
String lastUser = recent.get(i + 1).text(); String lastUser = recent.get(i + 1).text();
Pattern pattern = Pattern.compile("by (.*)"); Pattern pattern = Pattern.compile("\\b (.*)");
Matcher matcher = pattern.matcher(lastUser); Matcher matcher = pattern.matcher(lastUser);
if (matcher.find()) if (matcher.find())
lastUser = matcher.group(1); lastUser = matcher.group(1);
@ -302,62 +302,6 @@ public class RecentFragment extends Fragment
Log.e(TAG, "Parsing failed!"); Log.e(TAG, "Parsing failed!");
return false; 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;
// }
} }
} }

29
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 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"); 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_USER= 2;
public static final int WRONG_PASSWORD= 3; public static final int WRONG_PASSWORD= 3;
public static final int FAILED= 4; 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-------------------------------------------------- //-------------------------------------------LOGIN--------------------------------------------------
//Two options: (username, password, duration) or nothing - cookies //Two options: (username, password, duration) or nothing - cookies
public static LoginData login(String... strings) public static LoginData login(String... strings)
@ -106,7 +91,7 @@ public class Thmmy
setPersistentCookieSession(); setPersistentCookieSession();
loginData.setUsername(extractUserName(document)); loginData.setUsername(extractUserName(document));
loginData.setLogoutLink(HttpUrl.parse(logout.attr("href"))); loginData.setLogoutLink(HttpUrl.parse(logout.attr("href")));
loginData.setStatus(OK); loginData.setStatus(LOGGED_IN);
} }
else else
{ {
@ -238,11 +223,11 @@ public class Thmmy
//--------------------------------------LOGOUT-------------------------------------------------- //--------------------------------------LOGOUT--------------------------------------------------
public static int logout(HttpUrl logoutLink) public static int logout(LoginData loginData)
{ {
OkHttpClient client = BaseActivity.getClient(); OkHttpClient client = BaseActivity.getClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(logoutLink) .url(loginData.getLogoutLink())
.build(); .build();
try { try {
@ -250,10 +235,12 @@ public class Thmmy
Document document = Jsoup.parse(response.body().string()); Document document = Jsoup.parse(response.body().string());
Elements login = document.select("[value=Login]"); Elements login = document.select("[value=Login]");
((PersistentCookieJar) BaseActivity.getCookieJar()).clear();
if(!login.isEmpty()) if(!login.isEmpty())
{ {
Log.i("Logout", "Logout successful"); Log.i("Logout", "Logout successful");
return OK; loginData.setStatus(LOGGED_OUT);
return LOGGED_OUT;
} }
else else
{ {

9
app/src/main/res/menu/menu_main.xml

@ -6,10 +6,17 @@
android:id="@+id/action_login" android:id="@+id/action_login"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/login" android:title="@string/login"
android:visible="false"
app:showAsAction="never" /> app:showAsAction="never" />
<item <item
android:id="@+id/action_about" android:id="@+id/action_logout"
android:orderInCategory="200" android:orderInCategory="200"
android:title="@string/logout"
android:visible="false"
app:showAsAction="never" />
<item
android:id="@+id/action_about"
android:orderInCategory="300"
android:title="@string/about" android:title="@string/about"
app:showAsAction="never" /> app:showAsAction="never" />
</menu> </menu>

1
app/src/main/res/values/strings.xml

@ -14,4 +14,5 @@
<string name="logo">logo</string> <string name="logo">logo</string>
<string name="profile_name">Name</string> <string name="profile_name">Name</string>
<string name="login">Login</string> <string name="login">Login</string>
<string name="logout">Logout</string>
</resources> </resources>

Loading…
Cancel
Save