Skip to content

Commit

Permalink
asdf
Browse files Browse the repository at this point in the history
  • Loading branch information
vvasuki committed Mar 31, 2017
1 parent fe3ba90 commit 864254f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# stardict-dictionary-updater

Play store link: https://play.google.com/store/apps/details?id=sanskritcode.sanskritdictionaryupdater

Ratings Certificate: https://iarcweb.azurewebsites.net/Dashboard/Certificate/74e40614-671c-421e-9969-1c80da21a267

# For code contributors
See comment in MainActivity.java for a rough understanding of the code.

Review notifications setup: https://support.google.com/googleplay/android-developer/answer/138230?hl=en

# For dictionary contributors
* Just open an issue in this project.

## To contribute new dictionary repositories
* We will just need to list your dictionary repository in <dictioanryIndices.md>. Open an issue in this project.
* Creating your dictionary repository:
* Just follow the pattern you observe in, say [this repo](<https://raw.githubusercontent.com/sanskrit-coders/stardict-sanskrit/master/sa-head/tars/tars.MD>).
* Note that the filename of your dictionary should have two parts, separated by __, as in `kRdanta-rUpa-mAlA__2016-02-20_23-22-27`.
* The first part should be the actual dictionary name, the second the timestamp.
* All stardict and other dictionary files should have names matching the dictionary name specified above.
3 changes: 0 additions & 3 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" />
Expand All @@ -91,7 +90,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/multi-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
Expand All @@ -101,7 +99,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ android {

defaultConfig {
applicationId "sanskritcode.sanskritdictionaryupdater"
minSdkVersion 16
minSdkVersion 17
targetSdkVersion 21
versionCode 20
versionName "2.8"
versionName "2.9"
multiDexEnabled = true
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package sanskritcode.sanskritdictionaryupdater;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
Expand Down Expand Up @@ -49,9 +51,21 @@ public class GetDictionariesActivity extends Activity {
private File dictDir;
private boolean allDone = false;

SharedPreferences sharedDictVersionStore;
SharedPreferences.Editor dictVersionEditor;

public static String[] getDictNameAndVersion(String fileName) {
// handle filenames of the type: kRdanta-rUpa-mAlA__2016-02-20_23-22-27.tar.gz
// Hence calling getBaseName twice.
return FilenameUtils.getBaseName(FilenameUtils.getBaseName(fileName)).split("__");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedDictVersionStore = getSharedPreferences(
getString(R.string.dict_version_store), Context.MODE_PRIVATE);
dictVersionEditor = sharedDictVersionStore.edit();
allDone = false;
setContentView(R.layout.activity_get_dictionaries);
topText = (TextView) findViewById(R.id.textView);
Expand All @@ -63,12 +77,12 @@ protected void onCreate(Bundle savedInstanceState) {
dictFailure = new ArrayList<Boolean>(Collections.nCopies(dictionariesSelectedLst.size(), false));

sdcard = Environment.getExternalStorageDirectory();
downloadsDir = new File (sdcard.getAbsolutePath() + "/Download/dicttars");
if(downloadsDir.exists()==false) {
downloadsDir = new File(sdcard.getAbsolutePath() + "/Download/dicttars");
if (downloadsDir.exists() == false) {
downloadsDir.mkdirs();
}
dictDir = new File (sdcard.getAbsolutePath() + "/dictdata");
if(dictDir.exists()==false) {
dictDir = new File(sdcard.getAbsolutePath() + "/dictdata");
if (dictDir.exists() == false) {
dictDir.mkdirs();
}

Expand All @@ -85,13 +99,13 @@ public void buttonPressed1(View v) {
}

protected void getDictionaries(int index) {
if(dictionariesSelectedLst.size() == 0) {
if (dictionariesSelectedLst.size() == 0) {
topText.setText("No dictionaries selected!");
topText.append(getString(R.string.txtTryAgain));
button.setText(R.string.proceed_button);
button.setEnabled(true);
} else {
if(index >= dictionariesSelectedLst.size()) {
if (index >= dictionariesSelectedLst.size()) {
extractDicts(0);
} else {
topText.setText("Getting " + dictionariesSelectedLst.get(index));
Expand All @@ -102,41 +116,45 @@ protected void getDictionaries(int index) {
}
}

protected void extractDicts(int index) {
if(index >= dictFiles.size()) {
topText.setText(getString(R.string.finalMessage));
List<String> dictNames = Lists.transform(dictionariesSelectedLst, new Function<String, String>() {
public String apply(String in) {
return FilenameUtils.getBaseName(in);
}
});
StringBuffer failures = new StringBuffer("");
for(int i = 0; i < dictNames.size(); i++) {
if(dictFailure.get(i)) {
failures.append("\n" + dictNames.get(i));
} else {
}
protected void whenAllDictsExtracted() {
topText.setText(getString(R.string.finalMessage));
List<String> dictNames = Lists.transform(dictionariesSelectedLst, new Function<String, String>() {
public String apply(String in) {
return FilenameUtils.getBaseName(in);
}
if(failures.length() > 0) topText.append("\n" + "Failed on:" + failures);
StringBuffer successes = new StringBuffer("");
for(int i = 0; i < dictNames.size(); i++) {
if(dictFailure.get(i)) {
} else {
successes.append("\n" + dictNames.get(i));
}
});
StringBuffer failures = new StringBuffer("");
for (int i = 0; i < dictNames.size(); i++) {
if (dictFailure.get(i)) {
failures.append("\n" + dictNames.get(i));
} else {
}
}
if (failures.length() > 0) topText.append("\n" + "Failed on:" + failures);
StringBuffer successes = new StringBuffer("");
for (int i = 0; i < dictNames.size(); i++) {
if (dictFailure.get(i)) {
} else {
successes.append("\n" + dictNames.get(i));
}
}
if (successes.length() > 0) topText.append("\n" + "Succeeded on:" + successes);

button.setEnabled(true);
button.setText(R.string.buttonValQuit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishAffinity();
System.exit(0);
}
if(successes.length() > 0) topText.append("\n" + "Succeeded on:" + successes);
});
return;
}

button.setEnabled(true);
button.setText(R.string.buttonValQuit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishAffinity();
System.exit(0);
}
});
return;
protected void extractDicts(int index) {
if (index >= dictFiles.size()) {
whenAllDictsExtracted();
} else {
String message1 = "Extracting " + dictionariesSelectedLst.get(index);
Log.d("DictExtracter", message1);
Expand All @@ -154,7 +172,7 @@ protected void downloadDict(final int index) {
public void onSuccess(int statusCode, Header[] headers, File response) {
dictFiles.add(fileName);
Log.i("Got dictionary: ", fileName);
dictFailure.set(index,false);
dictFailure.set(index, false);
getDictionaries(index + 1);
progressBar.setVisibility(View.GONE);
}
Expand All @@ -172,20 +190,31 @@ public void onFailure(int statusCode, Header[] headers, Throwable throwable, Fil
String message = "Failed to get " + fileName;
topText.setText(message);
Log.w("downloadDict", message + ":" + throwable.getStackTrace().toString());
dictFailure.set(index,true);
dictFailure.set(index, true);
getDictionaries(index + 1);
progressBar.setVisibility(View.GONE);
}
});
}

void storeDictVersion(String fileName) {
String[] filenameParts = getDictNameAndVersion(fileName);
final String dictName = filenameParts[0];
if (filenameParts.length > 1) {
String dictVersion = FilenameUtils.getBaseName(FilenameUtils.getBaseName(fileName)).split("__")[1];
dictVersionEditor.putString(dictName, dictVersion);
dictVersionEditor.commit();
} else {
Log.w("DictExtractor", "Not storing dictionary version for " + fileName);
}
}

protected class DictExtractor extends AsyncTask<Integer, Integer, Integer> {

protected void deleteTarFile(String sourceFile) {
String message4 = "Deleting " + sourceFile + " " + new File(sourceFile).delete();
// topText.append(message4);
Log.d("DictExtractor", message4);

}

@Override
Expand Down Expand Up @@ -248,7 +277,7 @@ protected Integer doInBackground(Integer... params) {

final byte[] buffer = new byte[50000];
TarArchiveEntry currentEntry = null;
while((currentEntry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
while ((currentEntry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
String destFile = FilenameUtils.concat(destDir, currentEntry.getName());
FileOutputStream fos = new FileOutputStream(destFile);
String message3 = "Destination: " + destFile;
Expand All @@ -260,11 +289,12 @@ protected Integer doInBackground(Integer... params) {
fos.close();
}
tarInput.close();
dictFailure.set(index,false);
dictFailure.set(index, false);
Log.d("DictExtractor", "success!");
storeDictVersion(fileName);
} catch (Exception e) {
Log.e("DictExtractor", "IOEx:", e);
dictFailure.set(index,true);
dictFailure.set(index, true);
}
deleteTarFile(sourceFile);
return index;
Expand All @@ -273,7 +303,7 @@ protected Integer doInBackground(Integer... params) {

@Override
public void onBackPressed() {
if(button.getText().toString() == getResources().getString(R.string.buttonValQuit)) {
if (button.getText().toString() == getResources().getString(R.string.buttonValQuit)) {
finish();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package sanskritcode.sanskritdictionaryupdater;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
Expand Down Expand Up @@ -40,21 +42,29 @@ public class GetUrlActivity extends Activity {
private TextView topText;
private Button button;

SharedPreferences sharedDictVersionStore;
private List<CheckBox> checkBoxes = new ArrayList<CheckBox>();

public static HashSet<String> dictionariesSelected = new HashSet<String>();
CompoundButton.OnCheckedChangeListener checkboxListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
dictionariesSelected.add(buttonView.getHint().toString());
} else {
dictionariesSelected.remove(buttonView.getHint().toString());
}
if (isChecked) {
dictionariesSelected.add(buttonView.getHint().toString());
} else {
dictionariesSelected.remove(buttonView.getHint().toString());
}
button.setEnabled(dictionariesSelected.size() > 0);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(getLocalClassName(), "whenActivityLoaded indexedDicts " + indexedDicts);

sharedDictVersionStore = getSharedPreferences(
getString(R.string.dict_version_store), Context.MODE_PRIVATE);

indexesSelected = MainActivity.indexesSelected;
indexedDicts = new LinkedHashMap<String, List<String>>();
dictionariesSelected = new HashSet<String>();
Expand Down Expand Up @@ -82,6 +92,35 @@ public void buttonPressed1(View v) {
startActivity(intent);
}

protected void selectCheckboxes() {
boolean currentVersionsKnown = (sharedDictVersionStore.getAll().size() > 0);
int autoUnselectedDicts = 0;
for(CheckBox cb : checkBoxes) {
// handle values: kRdanta-rUpa-mAlA -> 2016-02-20_23-22-27
String filename = cb.getHint().toString();
boolean proposedVersionNewer = true;

String[] dictnameParts = GetDictionariesActivity.getDictNameAndVersion(filename);
String dictname = dictnameParts[0];
if(dictnameParts.length > 1) {
String currentVersion = sharedDictVersionStore.getString(dictname, "0000");
String proposedVersion = dictnameParts[1];
proposedVersionNewer = (proposedVersion.compareTo(currentVersion) > 1);
}

if (proposedVersionNewer) {
cb.setChecked(true);
} else {
autoUnselectedDicts++;
}
}

String message = String.format(getString(R.string.autoUnselectedDicts), sharedDictVersionStore.getAll().size(), autoUnselectedDicts);
topText.append(message);
Log.d(ACTIVITY_NAME, topText.getText().toString());
Log.d(ACTIVITY_NAME, message);
}

protected class DictUrlGetter extends AsyncTask<String, Integer, Integer> {
private final String DICT_URL_GETTER = DictUrlGetter.class.getName();
private int dictsRetreieved = 0;
Expand Down Expand Up @@ -126,8 +165,7 @@ public Integer doInBackground(String... dictionaryListNames) {
@Override
protected void onPostExecute(Integer result) {
// retainOnlyOneDictForDebugging();
String message = R.string.added_n_dictionary_urls + dictsRetreieved + " " +
getString(R.string.download_dictionaries);
String message = String.format(getString(R.string.added_n_dictionary_urls), dictsRetreieved);
topText.setText(message);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
for (String indexName : indexedDicts.keySet()) {
Expand All @@ -146,14 +184,13 @@ protected void onPostExecute(Integer result) {
cb.setText(url.replaceAll(".*/", ""));
cb.setHint(url);
cb.setTextColor(Color.BLACK);
cb.setChecked(true);
dictionariesSelected.add(url);
layout.addView(cb, layout.getChildCount());
cb.setOnCheckedChangeListener(checkboxListener);
checkBoxes.add(cb);
}
}
selectCheckboxes();
button.setText(getString(R.string.proceed_button));
button.setEnabled(true);
// getDictionaries(0);
}

Expand Down
Loading

0 comments on commit 864254f

Please sign in to comment.