Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2.0.0 #39

Open
wants to merge 37 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
94f4412
Rename library module to ckChangeLog-core
cketti Feb 18, 2016
1c3e60e
Extract dialog code to separate module
cketti Feb 18, 2016
8f6183d
Make ReleaseItem a top-level class
cketti Feb 18, 2016
2af8329
Clean up the code a bit
cketti Feb 18, 2016
507d9d2
Remove white spaces and line breaks from changes text.
johnjohndoe Mar 1, 2016
0169c57
Re-use newInstance constuctor.
johnjohndoe Mar 1, 2016
6cd83f6
Extract method to clean text of change item
cketti Mar 24, 2016
111ee98
Merge pull request #37 into version_2
cketti Mar 24, 2016
f97ba78
Merge pull request #38 from johnjohndoe/new-instance
cketti Mar 24, 2016
cd8d027
Update build tools etc.
cketti Jul 14, 2016
0f42533
Extract core functionality into multiple classes
cketti Oct 23, 2016
33da70d
Add tests
cketti Nov 4, 2016
5ff4e47
Make API to get change log nicer
cketti Nov 4, 2016
faab3ed
Make XmlParser more strict
cketti Nov 9, 2016
726a00a
Extract library version to variable
cketti Feb 11, 2017
26d7801
Move POM metadata to root project's build.gradle
cketti Feb 12, 2017
14e3cfa
Dynamically write string resource with library version
cketti Feb 12, 2017
82a42ba
Update copyright notices
cketti Mar 24, 2017
2637881
Move change log from README to separate file
cketti Mar 24, 2017
94eb31c
Extract code to generate the change log HTML to separate class
cketti Mar 24, 2017
3b4f673
Expose the used ChangeLog instance to users of DialogChangeLog
cketti Mar 24, 2017
5cae803
Clean up and add more JavaDoc
cketti Mar 24, 2017
ee69936
Update Change Log with migration instructions
cketti Mar 24, 2017
1752fac
Update README
cketti Mar 24, 2017
e6d9ee8
Add Maven Central snapshot repository
cketti Mar 24, 2017
5e61cbd
Make XmlParser return an unmodifiable list
cketti Nov 9, 2017
30dcb54
Update build tools
cketti Mar 8, 2018
0b40bcf
Update test dependencies
cketti Mar 8, 2018
2af0af4
Change package name to de.cketti.changelog
cketti May 12, 2018
445a0ae
Update dependencies
cketti May 12, 2018
36c48f8
Clean up code to remove some warnings
cketti May 12, 2018
c0db1c4
Move change log files from res/xml/ to res/raw/
cketti May 12, 2018
89238f7
Add "date" attribute
cketti May 12, 2018
ce3de32
Rename ckChangeLog-dialog to ckChangeLog-legacy-dialog
cketti May 12, 2018
7c9f838
Make writeCurrentVersion() update internal state
cketti May 13, 2018
c4ff306
Update copyright headers
cketti May 13, 2018
d483d5d
Update README and CHANGELOG
cketti May 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
buildscript {
repositories {
mavenCentral()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:2.2.2'
}
}

allprojects {
repositories {
mavenCentral()
}
}

Expand Down
6 changes: 5 additions & 1 deletion ckChangeLog-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ android {
defaultConfig {
versionName "1.2.2"

minSdkVersion 4
minSdkVersion 14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a technical reason for raising the minSdkVersion for ckChangeLog-core and ckChangeLog-dialog here? I changed it back to 4 and compiled locally and could run my app without an issue.

}
}

dependencies {
compile 'com.android.support:support-annotations:25.0.0'
}

project.ext {
pom = [
group: "de.cketti.library.changelog",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2015 cketti and contributors
* Copyright (C) 2012-2016 cketti and contributors
* https://github.com/cketti/ckChangeLog/graphs/contributors
*
* Portions Copyright (C) 2012 Martin van Zuilekom (http://martin.cubeactive.com)
Expand Down Expand Up @@ -33,23 +33,15 @@
*/
package de.cketti.library.changelog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.XmlResourceParser;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.SparseArray;


/**
Expand All @@ -63,6 +55,8 @@ public final class ChangeLog {


private final Context context;
private final SharedPreferences preferences;
private final ChangeLogProvider changeLogProvider;
private int lastVersionCode;
private int currentVersionCode;
private String currentVersionName;
Expand All @@ -89,17 +83,29 @@ public static ChangeLog newInstance(Context context) {
*
*/
public static ChangeLog newInstance(Context context, SharedPreferences preferences) {
ChangeLog changeLog = new ChangeLog(context);
changeLog.init(preferences);
ChangeLogProvider masterChangeLogProvider = new ResourceChangeLogProvider(context, R.xml.changelog_master);
ChangeLogProvider localizedChangeLogProvider = new ResourceChangeLogProvider(context, R.xml.changelog);
ChangeLogProvider changeLogProvider = new MergedChangeLogProvider(
masterChangeLogProvider, localizedChangeLogProvider);

return newInstance(context, preferences, changeLogProvider);
}

public static ChangeLog newInstance(Context context, SharedPreferences preferences,
ChangeLogProvider changeLogProvider) {
ChangeLog changeLog = new ChangeLog(context, preferences, changeLogProvider);
changeLog.init();

return changeLog;
}

private ChangeLog(Context context) {
private ChangeLog(Context context, SharedPreferences preferences, ChangeLogProvider changeLogProvider) {
this.context = context;
this.preferences = preferences;
this.changeLogProvider = changeLogProvider;
}

private void init(SharedPreferences preferences) {
private void init() {
lastVersionCode = preferences.getInt(VERSION_KEY, NO_VERSION);

try {
Expand Down Expand Up @@ -177,153 +183,24 @@ public boolean isFirstRunEver() {
* </p>
*/
public void writeCurrentVersion() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(VERSION_KEY, currentVersionCode);

// TODO: Update preferences from a background thread
editor.commit();
editor.apply();
}

/**
* Returns the merged change log.
* Returns the change log.
*
* @param full
* If this is {@code true} the full change log is returned. Otherwise only changes for
* versions newer than the last version are returned.
*
* @return A sorted {@code List} containing {@link ReleaseItem}s representing the (partial)
* change log.
*
* @see #getChangeLogComparator()
*/
public List<ReleaseItem> getChangeLog(boolean full) {
SparseArray<ReleaseItem> masterChangelog = getMasterChangeLog(full);
SparseArray<ReleaseItem> changelog = getLocalizedChangeLog(full);

List<ReleaseItem> mergedChangeLog = new ArrayList<ReleaseItem>(masterChangelog.size());

for (int i = 0, len = masterChangelog.size(); i < len; i++) {
int key = masterChangelog.keyAt(i);

// Use release information from localized change log and fall back to the master file
// if necessary.
ReleaseItem release = changelog.get(key, masterChangelog.get(key));

mergedChangeLog.add(release);
}

Collections.sort(mergedChangeLog, getChangeLogComparator());

return mergedChangeLog;
}

private SparseArray<ReleaseItem> getMasterChangeLog(boolean full) {
return readChangeLogFromResource(R.xml.changelog_master, full);
}

private SparseArray<ReleaseItem> getLocalizedChangeLog(boolean full) {
return readChangeLogFromResource(R.xml.changelog, full);
}

private SparseArray<ReleaseItem> readChangeLogFromResource(int resId, boolean full) {
XmlResourceParser xml = context.getResources().getXml(resId);
try {
return readChangeLog(xml, full);
} finally {
xml.close();
}
}

private SparseArray<ReleaseItem> readChangeLog(XmlPullParser xml, boolean full) {
SparseArray<ReleaseItem> result = new SparseArray<ReleaseItem>();

try {
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && xml.getName().equals(ReleaseTag.NAME)) {
if (parseReleaseTag(xml, full, result)) {
// Stop reading more elements if this entry is not newer than the last
// version.
break;
}
}
eventType = xml.next();
}
} catch (XmlPullParserException e) {
Log.e(LOG_TAG, e.getMessage(), e);
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}

return result;
}

private boolean parseReleaseTag(XmlPullParser xml, boolean full,
SparseArray<ReleaseItem> changelog) throws XmlPullParserException, IOException {

String version = xml.getAttributeValue(null, ReleaseTag.ATTRIBUTE_VERSION);

int versionCode;
try {
String versionCodeStr = xml.getAttributeValue(null, ReleaseTag.ATTRIBUTE_VERSION_CODE);
versionCode = Integer.parseInt(versionCodeStr);
} catch (NumberFormatException e) {
versionCode = NO_VERSION;
}

if (!full && versionCode <= lastVersionCode) {
return true;
}

int eventType = xml.getEventType();
List<String> changes = new ArrayList<String>();
while (eventType != XmlPullParser.END_TAG || xml.getName().equals(ChangeTag.NAME)) {
if (eventType == XmlPullParser.START_TAG && xml.getName().equals(ChangeTag.NAME)) {
eventType = xml.next();
String text = cleanText(xml.getText());
changes.add(text);
}
eventType = xml.next();
}

ReleaseItem release = new ReleaseItem(versionCode, version, changes);
changelog.put(versionCode, release);

return false;
}

private String cleanText(String text) {
return text.trim().replaceAll("\\s+", " ");
}

private Comparator<ReleaseItem> getChangeLogComparator() {
return new Comparator<ReleaseItem>() {
@Override
public int compare(ReleaseItem lhs, ReleaseItem rhs) {
if (lhs.versionCode < rhs.versionCode) {
return 1;
} else if (lhs.versionCode > rhs.versionCode) {
return -1;
} else {
return 0;
}
}
};
}


private interface ChangeLogTag {
String NAME = "changelog";
}

private interface ReleaseTag {
String NAME = "release";
String ATTRIBUTE_VERSION = "version";
String ATTRIBUTE_VERSION_CODE = "versioncode";
}

private interface ChangeTag {
String NAME = "change";
return full ?
changeLogProvider.getChangeLog() :
changeLogProvider.getChangeLogSince(lastVersionCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2012-2016 cketti and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.cketti.library.changelog;


import java.util.List;


public interface ChangeLogProvider {
List<ReleaseItem> getChangeLog();
List<ReleaseItem> getChangeLogSince(int lastVersionCode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2012-2016 cketti and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.cketti.library.changelog;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.util.SparseArray;

import static de.cketti.library.changelog.Preconditions.checkNotNull;


public final class MergedChangeLogProvider implements ChangeLogProvider {
private final ChangeLogProvider masterChangeLogProvider;
private final ChangeLogProvider localizedChangeLogProvider;


public MergedChangeLogProvider(ChangeLogProvider masterChangeLogProvider,
ChangeLogProvider localizedChangeLogProvider) {
this.masterChangeLogProvider = checkNotNull(masterChangeLogProvider, "masterChangeLogProvider == null");
this.localizedChangeLogProvider = checkNotNull(localizedChangeLogProvider,
"localizedChangeLogProvider == null");
}

@Override
public List<ReleaseItem> getChangeLog() {
List<ReleaseItem> masterChangeLog = masterChangeLogProvider.getChangeLog();
List<ReleaseItem> localizedChangeLog = localizedChangeLogProvider.getChangeLog();
return merge(masterChangeLog, localizedChangeLog);
}

@Override
public List<ReleaseItem> getChangeLogSince(int lastVersionCode) {
List<ReleaseItem> masterChangeLog = masterChangeLogProvider.getChangeLogSince(lastVersionCode);
List<ReleaseItem> localizedChangeLog = localizedChangeLogProvider.getChangeLogSince(lastVersionCode);
return merge(masterChangeLog, localizedChangeLog);
}

private List<ReleaseItem> merge(List<ReleaseItem> masterChangeLog, List<ReleaseItem> localizedChangeLog) {
SparseArray<ReleaseItem> localizedChangeLogMap = new SparseArray<>();
for (ReleaseItem releaseItem : localizedChangeLog) {
localizedChangeLogMap.put(releaseItem.versionCode, releaseItem);
}

List<ReleaseItem> mergedChangeLog = new ArrayList<>(masterChangeLog.size());
for (int i = 0, len = masterChangeLog.size(); i < len; i++) {
ReleaseItem masterReleaseItem = masterChangeLog.get(i);
int key = masterReleaseItem.versionCode;

// Use release information from localized change log and fall back to the master file if necessary.
ReleaseItem release = localizedChangeLogMap.get(key, masterReleaseItem);

mergedChangeLog.add(release);
}

Collections.sort(mergedChangeLog, new VersionCodeComparator());

return mergedChangeLog;
}


class VersionCodeComparator implements Comparator<ReleaseItem> {
@Override
public int compare(ReleaseItem lhs, ReleaseItem rhs) {
if (lhs.versionCode < rhs.versionCode) {
return 1;
} else if (lhs.versionCode > rhs.versionCode) {
return -1;
} else {
return 0;
}
}
}
}
Loading