This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
463 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
app/src/main/java/org/nv95/openmanga/activities/MalConfigActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package org.nv95.openmanga.activities; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.preference.PreferenceFragment; | ||
import android.preference.PreferenceManager; | ||
import android.support.annotation.Nullable; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ProgressBar; | ||
|
||
import org.nv95.openmanga.R; | ||
import org.nv95.openmanga.providers.MALProvider; | ||
import org.nv95.openmanga.utils.AnimUtils; | ||
|
||
/** | ||
* Created by unravel22 on 26.02.17. | ||
*/ | ||
|
||
public class MalConfigActivity extends BaseAppActivity implements View.OnClickListener { | ||
|
||
private ProgressBar mProgressBar; | ||
private EditText mEditTextLogin; | ||
private EditText mEditTextPassword; | ||
private View mBlockLogin; | ||
private Button mButtonAuth; | ||
@Nullable | ||
private SettingsFragment mSettingsFragment; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_malconf); | ||
setSupportActionBar(R.id.toolbar); | ||
enableHomeAsUp(); | ||
|
||
mProgressBar = (ProgressBar) findViewById(R.id.progressBar); | ||
mEditTextLogin = (EditText) findViewById(R.id.editTextLogin); | ||
mEditTextPassword = (EditText) findViewById(R.id.editTextPassword); | ||
mButtonAuth = (Button) findViewById(R.id.buttonAuth); | ||
mBlockLogin = findViewById(R.id.blockLogin); | ||
|
||
mButtonAuth.setOnClickListener(this); | ||
|
||
if (PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).contains("mal.password")) { | ||
mSettingsFragment = new SettingsFragment(); | ||
getFragmentManager().beginTransaction() | ||
.add(R.id.content, mSettingsFragment) | ||
.commit(); | ||
} else { | ||
mBlockLogin.setVisibility(View.VISIBLE); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClick(View v) { | ||
new AuthTask().startLoading(mEditTextLogin.getText().toString(), mEditTextPassword.getText().toString()); | ||
} | ||
|
||
private class AuthTask extends LoaderTask<String,Void,Boolean> { | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
super.onPreExecute(); | ||
AnimUtils.crossfade(null, mProgressBar); | ||
mButtonAuth.setEnabled(false); | ||
mEditTextLogin.setEnabled(false); | ||
mEditTextPassword.setEnabled(false); | ||
} | ||
|
||
@Override | ||
protected Boolean doInBackground(String... params) { | ||
return MALProvider.checkCredentials(params[0], params[1]); | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Boolean aBoolean) { | ||
super.onPostExecute(aBoolean); | ||
if (aBoolean) { | ||
AnimUtils.crossfade(mBlockLogin, null); | ||
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) | ||
.edit() | ||
.putString("mal.login", mEditTextLogin.getText().toString()) | ||
.putString("mal.password", mEditTextPassword.getText().toString()) | ||
.apply(); | ||
mSettingsFragment = new SettingsFragment(); | ||
getFragmentManager().beginTransaction() | ||
.add(R.id.content, mSettingsFragment) | ||
.commit(); | ||
} else { | ||
AnimUtils.crossfade(mProgressBar, null); | ||
mButtonAuth.setEnabled(true); | ||
mEditTextLogin.setEnabled(true); | ||
mEditTextPassword.setEnabled(true); | ||
mEditTextPassword.setError(getString(R.string.auth_failed)); | ||
mEditTextPassword.requestFocus(); | ||
} | ||
} | ||
} | ||
|
||
public static class SettingsFragment extends PreferenceFragment { | ||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
Activity activity = getActivity(); | ||
addPreferencesFromResource(R.xml.pref_mal); | ||
} | ||
|
||
@Override | ||
public void onActivityCreated(Bundle savedInstanceState) { | ||
super.onActivityCreated(savedInstanceState); | ||
Context context = getActivity(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
app/src/main/java/org/nv95/openmanga/providers/MALProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package org.nv95.openmanga.providers; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.support.annotation.Nullable; | ||
import android.support.annotation.WorkerThread; | ||
import android.text.TextUtils; | ||
|
||
import org.nv95.openmanga.items.MangaInfo; | ||
import org.nv95.openmanga.utils.AppHelper; | ||
import org.nv95.openmanga.utils.NetworkUtils; | ||
import org.nv95.openmanga.utils.XmlUtils; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.net.URLEncoder; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by unravel22 on 26.02.17. | ||
*/ | ||
|
||
public class MALProvider { | ||
|
||
@Nullable | ||
private static SoftReference<MALProvider> sInstance = null; | ||
|
||
private final String mLogin; | ||
private final String mPassword; | ||
|
||
public static MALProvider getInstance(Context context) { | ||
MALProvider instance = null; | ||
if (sInstance != null) { | ||
instance = sInstance.get(); | ||
} | ||
if (instance == null) { | ||
instance = new MALProvider(context); | ||
} | ||
return instance; | ||
} | ||
|
||
public MALProvider(Context context) { | ||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); | ||
mLogin = preferences.getString("mal.login", null); | ||
mPassword = preferences.getString("mal.password", null); | ||
} | ||
|
||
public boolean isAuthorized() { | ||
return !TextUtils.isEmpty(mLogin) && !TextUtils.isEmpty(mPassword); | ||
} | ||
|
||
@Nullable | ||
@WorkerThread | ||
public List<MALManga> findEquals(MangaInfo subject) { | ||
try { | ||
String name = AppHelper.isEnglish(subject.name) || TextUtils.isEmpty(subject.subtitle) ? subject.name : subject.subtitle; | ||
return search(name); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
@WorkerThread | ||
public List<MALManga> search(String query) throws Exception { | ||
return convertResults(XmlUtils.convertNodesFromXml( | ||
NetworkUtils.getRaw("https://myanimelist.net/api/manga/search.xml?q=" + URLEncoder.encode(query, "UTF-8"), mLogin, mPassword) | ||
)); | ||
} | ||
|
||
private static List<MALManga> convertResults(Document xml) { | ||
ArrayList<MALManga> result = new ArrayList<>(); | ||
NodeList entries = xml.getElementsByTagName("entry"); | ||
Node o; | ||
MALManga item; | ||
for (int i=0; i<entries.getLength();i++) { | ||
o = entries.item(i); | ||
if (o instanceof Element) { | ||
try { | ||
item = new MALManga(); | ||
item.id = Integer.parseInt(XmlUtils.getElementValue((Element) o, "id")); | ||
item.title = XmlUtils.getElementValue((Element) o, "title"); | ||
item.english = XmlUtils.getElementValue((Element) o, "english"); | ||
item.synopsis = XmlUtils.getElementValue((Element) o, "synopsis"); | ||
item.score = (int) (Double.parseDouble(XmlUtils.getElementValue((Element) o, "score")) * 100); | ||
result.add(item); | ||
} catch (Exception ignored) { | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static boolean checkCredentials(String login, String password) { | ||
try { | ||
return !TextUtils.isEmpty(NetworkUtils.getRaw("https://myanimelist.net/api/account/verify_credentials.xml", | ||
login, password)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
public static class MALManga { | ||
public int id; | ||
public String title; | ||
public String english; | ||
public String synopsis; | ||
public int score; | ||
} | ||
|
||
@Nullable | ||
public static Boolean getConfig(Context context, String key, boolean defValue) { | ||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); | ||
if (!prefs.contains("mal.password")) { | ||
return null; | ||
} | ||
return prefs.getBoolean("mal." + key, defValue); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/org/nv95/openmanga/services/MALSyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.nv95.openmanga.services; | ||
|
||
import android.app.IntentService; | ||
import android.content.Intent; | ||
import android.support.annotation.Nullable; | ||
|
||
import org.nv95.openmanga.providers.HistoryProvider; | ||
import org.nv95.openmanga.providers.MALProvider; | ||
|
||
/** | ||
* Created by unravel22 on 26.02.17. | ||
*/ | ||
|
||
public class MALSyncService extends IntentService { | ||
|
||
private MALProvider mProvider; | ||
|
||
public MALSyncService() { | ||
super(MALSyncService.class.getName()); | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
mProvider = new MALProvider(this); | ||
} | ||
|
||
@Override | ||
protected void onHandleIntent(@Nullable Intent intent) { | ||
try { | ||
mProvider.findEquals(HistoryProvider.getInstance(this).getLast()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.