Merge pull request #437 from cemrich/replace-changeloglib

Replaced changeloglib
This commit is contained in:
David-Development 2015-11-13 09:28:39 -04:00
commit 2183516867
8 changed files with 267 additions and 40 deletions

@ -1 +0,0 @@
Subproject commit 8ca20dc53f8754256a277119c17f9a04556752b0

View file

@ -51,7 +51,6 @@ dependencies {
// You must install or update the Google Repository through the SDK manager to use this dependency.
// The Google Repository (separate from the corresponding library) can be found in the Extras category.
// compile 'com.google.android.gms:play-services:4.2.42'
compile project(':Changeloglib:ChangeLogLibrary')
compile project(':ownCloud-Account-Importer')
compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
@ -70,6 +69,7 @@ dependencies {
compile 'de.greenrobot:greendao-generator:2.0.0'
//compile 'org.freemarker:freemarker:2.3.23' //Required for DAO generation
compile 'org.apache.commons:commons-lang3:3.4'
compile 'com.github.gabrielemariotti.changeloglib:changelog:2.0.0'
testCompile 'org.robolectric:robolectric:3.0-rc3'
testCompile 'junit:junit:4.+'

View file

@ -21,51 +21,49 @@
package de.luhmer.owncloudnewsreader;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import it.gmariotti.changelibs.library.view.ChangeLogListView;
import java.io.IOException;
import java.util.Formatter;
import de.luhmer.owncloudnewsreader.async_tasks.DownloadChangelogTask;
import de.luhmer.owncloudnewsreader.view.ChangeLogFileListView;
/**
* Activity which displays a login screen to the user, offering registration as
* well.
* Displays current app version and changelog.
*/
public class VersionInfoDialogFragment extends DialogFragment {
@SuppressLint("SetJavaScriptEnabled")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// load views
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_version_info, null);
builder.setView(view).setTitle(getString(R.string.menu_About_Changelog));
View view = inflater.inflate(R.layout.dialog_version_info, null);
try {
PackageInfo pInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
String version = pInfo.versionName;
((TextView)view.findViewById(R.id.tv_androidAppVersion)).setText("You're using Version " + version);
ChangeLogFileListView clListView = (ChangeLogFileListView) view.findViewById(R.id.changelog_listview);
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.changeLogLoadingProgressBar);
TextView versionTextView = (TextView) view.findViewById(R.id.tv_androidAppVersion);
// build dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setView(view)
.setTitle(getString(R.string.menu_About_Changelog));
ChangeLogListView clListView = (ChangeLogListView) view.findViewById(R.id.changelog_listview);
clListView.setCallback(new ChangeLogListView.FinishedLoadingCallback() {
@Override
public void FinishedLoading() {
view.findViewById(R.id.changeLogLoadingProgressBar).setVisibility(View.GONE);
}
});
// set current version
versionTextView.setText(getVersionString());
} catch (Exception ex) {
ex.printStackTrace();
}
// load changelog into view
loadChangeLog(clListView, progressBar);
return builder.create();
}
@ -83,4 +81,37 @@ public class VersionInfoDialogFragment extends DialogFragment {
super.onStart();
}
private String getVersionString() {
String version = "?";
try {
PackageInfo pInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e){
e.printStackTrace();
}
Formatter formatter = new Formatter();
String versionString = getString(R.string.current_version);
return formatter.format(versionString, version).toString();
}
/**
* Loads changelog into the given view and hides progress bar when done.
*/
private void loadChangeLog(ChangeLogFileListView clListView, final ProgressBar progressBar) {
new DownloadChangelogTask(getContext(), clListView, new DownloadChangelogTask.Listener() {
@Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onError(IOException e) {
progressBar.setVisibility(View.GONE);
e.printStackTrace();
}
}).execute();
}
}

View file

@ -0,0 +1,167 @@
package de.luhmer.owncloudnewsreader.async_tasks;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.AsyncTask;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import de.luhmer.owncloudnewsreader.view.ChangeLogFileListView;
/**
* Downloads the owncloud news reader changelog from github, transforms it into xml
* and saves it as tempfile. This xml tempfile can be used for changeloglib library.
*/
public class DownloadChangelogTask extends AsyncTask<Void, Void, String> {
@SuppressWarnings("unused")
private static final String TAG = "DownloadChangelogTask";
private static final String README_URL = "https://raw.github.com/owncloud/News-Android-App/master/README.md";
private static final String FILE_NAME = "changelog.xml";
private Context mContext;
private ChangeLogFileListView mChangelogView;
private Listener mListener;
private IOException exception;
/**
* @param context
* @param changelogView this list view will be automatically filled when
* downloading and saving has finished
* @param listener called when task has finished or errors have been raised
*/
public DownloadChangelogTask(Context context,
ChangeLogFileListView changelogView,
Listener listener) {
mContext = context;
mChangelogView = changelogView;
mListener = listener;
}
@Override
protected String doInBackground(Void... params) {
String path = null;
try {
ArrayList<String> changelogArr = downloadReadme();
String xml = convertToXML(changelogArr);
path = saveToTempFile(xml, FILE_NAME);
} catch (IOException e) {
exception = e;
}
return path;
}
@Override
protected void onPostExecute(String filePath) {
if (exception != null) {
mListener.onError(exception);
return;
}
mChangelogView.loadFile(filePath);
mChangelogView.getAdapter().registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
mListener.onSuccess();
}
});
}
private ArrayList<String> downloadReadme() throws IOException {
ArrayList<String> changelogArr = new ArrayList<>();
URL url = new URL(README_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream isTemp = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(isTemp));
String inputLine;
while ((inputLine = in.readLine()) != null) {
changelogArr.add(inputLine.replace("<", "[").replace(">", "]"));
}
in.close();
} finally {
urlConnection.disconnect();
}
return changelogArr;
}
private String convertToXML(ArrayList<String> changelogArr) {
// use changelog section exclusively
int changelogStartIndex = changelogArr.indexOf("Updates") + 2;
changelogArr.subList(0, changelogStartIndex).clear();
changelogArr.add("");
// create xml nodes
StringBuilder builder = new StringBuilder();
builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
builder.append("<changelog bulletedList=\"true\">");
boolean versionStarted = false;
for (String line : changelogArr) {
if (line.startsWith("- ")) {
// change entry
builder.append("<changelogtext>");
builder.append(line.substring(2).trim());
builder.append("</changelogtext>");
} else if (line.equals("")) {
// version end
if (versionStarted) {
versionStarted = false;
builder.append("</changelogversion>");
}
} else if (!line.contains("---------------------")) {
// version start
versionStarted = true;
builder.append("<changelogversion versionName=\"" + line + "\">");
}
}
builder.append("</changelog>");
return builder.toString();
}
private String saveToTempFile(String content, String fileName) throws IOException {
File file = File.createTempFile(fileName, null, mContext.getCacheDir());
BufferedWriter out = new BufferedWriter(new FileWriter(file));
try {
out.write(content);
} finally {
out.close();
}
return "file://" + file.getAbsolutePath();
}
public interface Listener {
/**
* Called when ChangeLogFileListView instance has successfully been updated.
*/
void onSuccess();
/**
* Called when some error has been thrown during download, parsing or saving.
*/
void onError(IOException e);
}
}

View file

@ -0,0 +1,38 @@
package de.luhmer.owncloudnewsreader.view;
import android.content.Context;
import android.util.AttributeSet;
import it.gmariotti.changelibs.library.view.ChangeLogListView;
/**
* Thin wrapper around changeloglib to load local xml files by path
* after the view has already been instanciated.
*/
public class ChangeLogFileListView extends ChangeLogListView {
public ChangeLogFileListView(Context context) {
this(context, null);
}
public ChangeLogFileListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ChangeLogFileListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* @param path local xml path staring with "file://"
*/
public void loadFile(String path) {
mChangeLogFileResourceUrl = path;
super.initAdapter();
}
@Override
protected void initAdapter() {
// do nothing yet - will be called in loadFile()
}
}

View file

@ -1,15 +1,3 @@
<!--
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity"
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
@ -30,10 +18,10 @@
android:layout_gravity="center_vertical|center_horizontal"
android:indeterminate="true" />
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="it.gmariotti.changelibs.library.view.ChangeLogListView"
<de.luhmer.owncloudnewsreader.view.ChangeLogFileListView
android:id="@+id/changelog_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>

View file

@ -55,4 +55,7 @@
<color name="tintColorLight">@android:color/black</color>
<color name="tintColorDark">@android:color/white</color>
<!-- override libs -->
<color name="chglib_background_default_divider_color">#00000000</color>
</resources>

View file

@ -18,6 +18,7 @@
<item quantity="one">You\'ve reached the maximum size of %d item</item>
<item quantity="other">You\'ve reached the maximum size of %d items</item>
</plurals>
<string name="current_version">You\'re using Version %s</string>
<string name="widget_header">ownCloud News</string>
<plurals name="message_bar_new_articles_available">
<item quantity="one">%d new item available</item>