|
@@ -45,12 +45,12 @@ import android.webkit.WebView;
|
|
|
import android.widget.*;
|
|
|
import com.ortiz.touchview.TouchImageView;
|
|
|
import com.wefika.horizontalpicker.HorizontalPicker;
|
|
|
-import godau.fynn.dsbdirect.BuildConfig;
|
|
|
-import godau.fynn.dsbdirect.NewsQuery;
|
|
|
-import godau.fynn.dsbdirect.R;
|
|
|
-import godau.fynn.dsbdirect.Utility;
|
|
|
+import godau.fynn.dsbdirect.*;
|
|
|
+import godau.fynn.dsbdirect.activity.settings.MainSettingsActivity;
|
|
|
+import godau.fynn.dsbdirect.activity.settings.SettingsActivity;
|
|
|
import godau.fynn.dsbdirect.manager.DownloadManager;
|
|
|
import godau.fynn.dsbdirect.manager.FileManager;
|
|
|
+import godau.fynn.dsbdirect.manager.LoginManager;
|
|
|
import godau.fynn.dsbdirect.table.Adapter;
|
|
|
import godau.fynn.dsbdirect.table.Entry;
|
|
|
import godau.fynn.dsbdirect.table.Table;
|
|
@@ -60,19 +60,21 @@ import humanize.Humanize;
|
|
|
import humanize.time.TimeMillis;
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
-import org.json.JSONObject;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.Date;
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
private static final int REQUEST_LOGIN = 1;
|
|
|
|
|
|
private Table mCurrentTable;
|
|
|
- private String credentialsId;
|
|
|
- private String credentialsPass;
|
|
|
+
|
|
|
+ private Login login;
|
|
|
|
|
|
private Table[] mTables;
|
|
|
private Date mTimetabledate;
|
|
@@ -82,6 +84,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
private Utility u;
|
|
|
private FileManager mFileManager;
|
|
|
private DownloadManager mDownloadManager;
|
|
|
+ private LoginManager mLoginManager;
|
|
|
|
|
|
private ArrayList<Thread> mReaderThreads = new ArrayList<>();
|
|
|
|
|
@@ -114,8 +117,6 @@ public class MainActivity extends AppCompatActivity {
|
|
|
Log.d("DRAW", "rendering layout took " + (drawTimeEnd - drawTimeStart) + " milliseconds");
|
|
|
|
|
|
final SharedPreferences sharedPreferences = u.getSharedPreferences();
|
|
|
- credentialsId = sharedPreferences.getString("id", "");
|
|
|
- credentialsPass = sharedPreferences.getString("pass", "");
|
|
|
|
|
|
mParse = sharedPreferences.getBoolean("parse", true);
|
|
|
mMerge = sharedPreferences.getBoolean("merge", true);
|
|
@@ -132,6 +133,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
// Get managers
|
|
|
mFileManager = new FileManager(MainActivity.this);
|
|
|
mDownloadManager = new DownloadManager(this);
|
|
|
+ mLoginManager = new LoginManager(MainActivity.this);
|
|
|
|
|
|
int previousVersion = sharedPreferences.getInt("version", BuildConfig.VERSION_CODE);
|
|
|
|
|
@@ -141,11 +143,13 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
|
|
|
// Migrate Röntgen users from 2.0.3 (version code 17)
|
|
|
- if ((previousVersion <= 17) && (credentialsId.equals("219261") || u.getParserUserSetting().equals(Utility.ROENTGEN))) {
|
|
|
+ if ((previousVersion <= 17) && (sharedPreferences.getString("id", "").equals("219261") || u.getParserUserSetting().equals(Utility.ROENTGEN))) {
|
|
|
sharedPreferences.edit()
|
|
|
.putBoolean("login", false)
|
|
|
.putInt("version", BuildConfig.VERSION_CODE)
|
|
|
.remove("parser")
|
|
|
+ .remove("id")
|
|
|
+ .remove("pass")
|
|
|
.apply();
|
|
|
|
|
|
new AlertDialog.Builder(MainActivity.this)
|
|
@@ -155,7 +159,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
.setNeutralButton(R.string.contact_app_developer, new DialogInterface.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
- SettingsActivity.emailTheDev(MainActivity.this);
|
|
|
+ MainSettingsActivity.emailTheDev(MainActivity.this);
|
|
|
finish();
|
|
|
}
|
|
|
})
|
|
@@ -173,11 +177,20 @@ public class MainActivity extends AppCompatActivity {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Delete useless auth token from versions from 2.4.1 (version code 22)
|
|
|
+ // Delete useless auth token from versions up to 2.4.1 (version code 22)
|
|
|
if (previousVersion <= 22) {
|
|
|
sharedPreferences.edit().remove("token").apply();
|
|
|
}
|
|
|
|
|
|
+ // Migrate login from up to version 2.5.5 (version code 29)
|
|
|
+ if (previousVersion <= 29) {
|
|
|
+ mLoginManager.addLogin(new Login(
|
|
|
+ sharedPreferences.getString("id", ""),
|
|
|
+ sharedPreferences.getString("pass", "")
|
|
|
+ ));
|
|
|
+ sharedPreferences.edit().remove("pass").apply();
|
|
|
+ }
|
|
|
+
|
|
|
// Migrate users to most recent version by wiping news after update
|
|
|
if (previousVersion < BuildConfig.VERSION_CODE) {
|
|
|
Log.d("MIGRATION", "wiping news");
|
|
@@ -185,8 +198,9 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
|
|
|
// Start loading or show login screen
|
|
|
- boolean login = sharedPreferences.getBoolean("login", false);
|
|
|
- if (login && !credentialsId.isEmpty() && !credentialsPass.isEmpty()) {
|
|
|
+ if (mLoginManager.canLogin()) {
|
|
|
+ login = mLoginManager.getActiveLogin();
|
|
|
+
|
|
|
// Start loading
|
|
|
new Thread(new Runnable() {
|
|
|
@Override
|
|
@@ -235,10 +249,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
private void getTimetables() {
|
|
|
|
|
|
try {
|
|
|
- final JSONArray json = mDownloadManager.downloadTimetableList(credentialsId, credentialsPass);
|
|
|
-
|
|
|
- // Parse JSON
|
|
|
- mTables = Reader.readTableList(json);
|
|
|
+ mTables = mDownloadManager.downloadTimetableList(login);
|
|
|
|
|
|
// No content
|
|
|
if (mTables.length == 0) {
|
|
@@ -327,7 +338,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
- } catch (DownloadManager.UnexpectedResponseException | JSONException e) {
|
|
|
+ } catch (DownloadManager.UnexpectedResponseException e) {
|
|
|
runOnUiThread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
@@ -442,7 +453,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
// Create runnable
|
|
|
final ReaderRunnable readerRunnable = new ReaderRunnable(MainActivity.this, response,
|
|
|
- credentialsId, mReaderThreads
|
|
|
+ login.getId(), mReaderThreads
|
|
|
);
|
|
|
readerRunnable.addHandlers(
|
|
|
new Handler(new Handler.Callback() {
|
|
@@ -451,9 +462,21 @@ public class MainActivity extends AppCompatActivity {
|
|
|
// Display parsed entries
|
|
|
displayEntries(readerRunnable.getResult());
|
|
|
|
|
|
- if (u.getSharedPreferences().getBoolean(Utility.SUPER_SECRET_SETTING_SCHOOL_NAME_AS_WINDOW_TITLE, getResources().getBoolean(R.bool.school_name_as_window_title_default))) {
|
|
|
- setTitle(readerRunnable.getSchoolName());
|
|
|
+ String schoolName = readerRunnable.getSchoolName();
|
|
|
+ if (schoolName != null) {
|
|
|
+ // Display school name as window title if configured via preference, is debug build or multiple logins are configured
|
|
|
+ if (u.getSharedPreferences().getBoolean(Utility.SUPER_SECRET_SETTING_FORCE_SCHOOL_NAME_AS_WINDOW_TITLE, getResources().getBoolean(R.bool.school_name_as_window_title_default))
|
|
|
+ || mLoginManager.getLoginCount() > 1) {
|
|
|
+ setTitle(schoolName);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set login display name to school name if doesn't already have one
|
|
|
+ if (!mLoginManager.getActiveLogin().hasDisplayName()) {
|
|
|
+ mLoginManager.getActiveLogin().setDisplayName(schoolName);
|
|
|
+ mLoginManager.write();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
return false;
|
|
|
}
|
|
|
}),
|
|
@@ -689,30 +712,14 @@ public class MainActivity extends AppCompatActivity {
|
|
|
// Don't merge
|
|
|
mMerge = false;
|
|
|
|
|
|
- // Find every file
|
|
|
- final File[] files = mFileManager.listFiles();
|
|
|
+ // Find every file, sorted descending by last modified
|
|
|
+ final File[] files = mFileManager.getFilesSorted();
|
|
|
|
|
|
if (files.length == 0) {
|
|
|
// We're done
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Sort files descending by last modified
|
|
|
- Arrays.sort(files, new Comparator<File>() {
|
|
|
- @Override
|
|
|
- public int compare(File o1, File o2) {
|
|
|
- if (o1.lastModified() < o2.lastModified()) {
|
|
|
- return 1;
|
|
|
- } else if (o1.lastModified() == o2.lastModified()) {
|
|
|
- return 0;
|
|
|
- } else if (o1.lastModified() > o2.lastModified()) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- // We should never get here but whatever
|
|
|
- return 0;
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
// Read every file to a Table
|
|
|
mTables = new Table[files.length];
|
|
|
|
|
@@ -826,13 +833,20 @@ public class MainActivity extends AppCompatActivity {
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
getMenuInflater().inflate(R.menu.menu, menu);
|
|
|
|
|
|
- if (getResources().getBoolean(R.bool.reauthenticate_button)) {
|
|
|
- menu.add(Menu.NONE, 3, Menu.NONE, R.string.action_reauth);
|
|
|
- }
|
|
|
if (getResources().getBoolean(R.bool.news_check_button)) {
|
|
|
menu.add(Menu.NONE, 4, Menu.NONE, R.string.action_check_news);
|
|
|
}
|
|
|
|
|
|
+ // Add all inactive logins as a submenu if there is one
|
|
|
+ if (mLoginManager.getLoginCount() > 1) {
|
|
|
+ SubMenu loginMenu = menu.addSubMenu(R.string.action_switch_login);
|
|
|
+ for (Login l :
|
|
|
+ mLoginManager.getInactiveLogins()) {
|
|
|
+ loginMenu.add(Menu.NONE, Integer.parseInt(l.getId()), Menu.NONE, l.getDisplayName());
|
|
|
+ }
|
|
|
+ loginMenu.add(Menu.NONE, 5, Menu.NONE, R.string.action_add_login).setIcon(R.drawable.ic_add_black_24dp);
|
|
|
+ }
|
|
|
+
|
|
|
// Pass it around!
|
|
|
mMenu = menu;
|
|
|
|
|
@@ -855,7 +869,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
break;
|
|
|
case R.id.action_settings:
|
|
|
- Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
|
|
|
+ Intent settingsIntent = new Intent(MainActivity.this, MainSettingsActivity.class);
|
|
|
|
|
|
// Pass along whether plan contains html files and whether plan only consists of html files
|
|
|
if (mTables != null) {
|
|
@@ -877,15 +891,27 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
|
|
|
startActivityForResult(settingsIntent,
|
|
|
- SettingsActivity.class.getName().length() // I needed a constant number, so I chose something simple & easy to remember
|
|
|
+ MainSettingsActivity.class.getName().length() // I needed a constant number, so I chose something simple & easy to remember
|
|
|
);
|
|
|
break;
|
|
|
- case 3:
|
|
|
+ case 5:
|
|
|
startActivityForResult(new Intent(MainActivity.this, LoginActivity.class), REQUEST_LOGIN);
|
|
|
break;
|
|
|
case 4:
|
|
|
new Thread(new NewsQuery(MainActivity.this, mDownloadManager)).start();
|
|
|
+ break;
|
|
|
default:
|
|
|
+ // Find out whether this id fits any login id
|
|
|
+ for (Login l :
|
|
|
+ mLoginManager.getLogins()) {
|
|
|
+ if (item.getItemId() == Integer.parseInt(l.getId())) {
|
|
|
+ Log.d("LOGINSWITCH", "to login " + l.getDisplayName());
|
|
|
+ mLoginManager.setActiveLogin(l);
|
|
|
+ recreate();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// If we got here, the user's action was not recognized.
|
|
|
// Invoke the superclass to handle it.
|
|
|
return super.onOptionsItemSelected(item);
|
|
@@ -926,7 +952,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
} else if (resultCode == RESULT_CANCELED && requestCode == REQUEST_LOGIN) {
|
|
|
// Login was cancelled, quit if not debug
|
|
|
if (getResources().getBoolean(R.bool.authenticate_quit_on_cancel))
|
|
|
- finish();
|
|
|
+ finish();
|
|
|
}
|
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|