Android Open Source - opentraining Change Log Dialog






From Project

Back to project page opentraining.

License

The source code is released under:

GNU General Public License

If you think the Android project opentraining listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
* (c) 2012 Martin van Zuilekom (http://martin.cubeactive.com)
*/*  w  w  w.  j  a  va  2  s  . c  o m*/
* 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.skubware.opentraining.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.text.format.DateFormat;
import android.util.Log;
import android.webkit.WebView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import de.skubware.opentraining.R;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Class to show a change log dialog
*/
public class ChangeLogDialog {
    private static final String TAG = "ChangeLogDialog";

    private final Context mContext;
    private String mStyle = "h1 { margin-left: 0px; font-size: 12pt; }"
            + "li { margin-left: 0px; font-size: 9pt; }"
            + "ul { padding-left: 30px; }"
            + ".summary { font-size: 9pt; color: #606060; display: block; clear: left; }"
            + ".date { font-size: 9pt; color: #606060; display: block; }";

    protected DialogInterface.OnDismissListener mOnDismissListener;

    public ChangeLogDialog(final Context context) {
        mContext = context;
    }

    protected Context getContext() {
        return mContext;
    }

    //Get the current app version
    private String getAppVersion() {
        String versionName = "";
        try {
            final PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (NameNotFoundException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return versionName;
    }

    //Parse a date string from the xml and format it using the local date format
    @SuppressLint("SimpleDateFormat")
    private String parseDate(final String dateString) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        try {
            final Date parsedDate = dateFormat.parse(dateString);
            return DateFormat.getDateFormat(getContext()).format(parsedDate);
        } catch (ParseException ignored) {
            //If there is a problem parsing the date just return the original string
            return dateString;
        }
    }

    //Parse a the release tag and appends it to the changelog builder
    private void parseReleaseTag(final StringBuilder changelogBuilder, final XmlPullParser resourceParser) throws XmlPullParserException, IOException {
        changelogBuilder.append("<h1>Release: ").append(resourceParser.getAttributeValue(null, "version")).append("</h1>");

        //Add date if available
        if (resourceParser.getAttributeValue(null, "date") != null) {
            changelogBuilder.append("<span class='date'>").append(parseDate(resourceParser.getAttributeValue(null, "date"))).append("</span>");
        }

        //Add summary if available
        if (resourceParser.getAttributeValue(null, "summary") != null) {
            changelogBuilder.append("<span class='summary'>").append(resourceParser.getAttributeValue(null, "summary")).append("</span>");
        }

        changelogBuilder.append("<ul>");

        //Parse child nodes
        int eventType = resourceParser.getEventType();
        while ((eventType != XmlPullParser.END_TAG) || (resourceParser.getName().equals("change"))) {
            if ((eventType == XmlPullParser.START_TAG) && (resourceParser.getName().equals("change"))) {
                eventType = resourceParser.next();
                changelogBuilder.append("<li>" + resourceParser.getText() + "</li>");
            }
            eventType = resourceParser.next();
        }
        changelogBuilder.append("</ul>");
    }

    //CSS style for the html
    private String getStyle() {
        return String.format("<style type=\"text/css\">%s</style>", mStyle);
    }

    public void setStyle(final String style) {
        mStyle = style;
    }

    public ChangeLogDialog setOnDismissListener(final DialogInterface.OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
        return this;
    }

    //Get the changelog in html code, this will be shown in the dialog's webview
    private String getHTMLChangelog(final int resourceId, final Resources resources, final int version) {
        boolean releaseFound = false;
        final StringBuilder changelogBuilder = new StringBuilder();
        changelogBuilder.append("<html><head>").append(getStyle()).append("</head><body>");
        final XmlResourceParser xml = resources.getXml(resourceId);
        try {
            int eventType = xml.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if ((eventType == XmlPullParser.START_TAG) && (xml.getName().equals("release"))) {
                    //Check if the version matches the release tag.
                    //When version is 0 every release tag is parsed.
                    final int versioncode = Integer.parseInt(xml.getAttributeValue(null, "versioncode"));
                    if ((version == 0) || (versioncode == version)) {
                        parseReleaseTag(changelogBuilder, xml);
                        releaseFound = true; //At lease one release tag has been parsed.
                    }
                }
                eventType = xml.next();
            }
        } catch (XmlPullParserException e) {
            Log.e(TAG, e.getMessage(), e);
            return "";
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
            return "";
        } finally {
            xml.close();
        }
        changelogBuilder.append("</body></html>");

        //Check if there was a release tag parsed, if not return an empty string.
        if (releaseFound) {
            return changelogBuilder.toString();
        } else {
            return "";
        }
    }

    //Returns change log in HTML format
    public String getHTML() {
     //TODO: Remove duplicate code with the method show()
        //Get resources
        final String packageName = mContext.getPackageName();
        final Resources resources;
        try {
            resources = mContext.getPackageManager().getResourcesForApplication(packageName);
        } catch (NameNotFoundException ignored) {
            return "";
        }

        //Create HTML change log
        return getHTMLChangelog(R.xml.changelog, resources, 0);
    }
    
    //Call to show the change log dialog
    public void show() {
        show(0);
    }

    protected void show(final int version) {
        //Get resources
        final String packageName = mContext.getPackageName();
        final Resources resources;
        try {
            resources = mContext.getPackageManager().getResourcesForApplication(packageName);
        } catch (NameNotFoundException ignored) {
            return;
        }

        //Get dialog title
        String title = resources.getString(R.string.title_changelog);
        title = String.format("%s v%s", title, getAppVersion());

        //Create html change log
        final String htmlChangelog = getHTMLChangelog(R.xml.changelog, resources, version);

        //Get button strings
        final String closeString = resources.getString(R.string.changelog_close);

        //Check for empty change log
        if (htmlChangelog.length() == 0) {
            //It seems like there is nothing to show, just bail out.
            return;
        }

        //Create web view and load html
        final WebView webView = new WebView(mContext);
        webView.loadDataWithBaseURL(null, htmlChangelog, "text/html", "utf-8", null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
                .setTitle(title)
                .setView(webView)
                .setPositiveButton(closeString, new Dialog.OnClickListener() {
                    public void onClick(final DialogInterface dialogInterface, final int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setOnCancelListener( new OnCancelListener() {

@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();  
}
});
        AlertDialog dialog = builder.create();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(final DialogInterface dialog) {
                if (mOnDismissListener != null) {
                    mOnDismissListener.onDismiss(dialog);
                }
            }
        });
        dialog.show();
    }

}




Java Source Code List

at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter.java
at.technikum.mti.fancycoverflow.FancyCoverFlowItemWrapper.java
at.technikum.mti.fancycoverflow.FancyCoverFlow.java
de.skubware.opentraining.activity.ChangeLogDialog.java
de.skubware.opentraining.activity.DisclaimerDialog.java
de.skubware.opentraining.activity.MainActivity.java
de.skubware.opentraining.activity.NavigationGalleryAdapter.java
de.skubware.opentraining.activity.SelectWorkoutDialogFragment.java
de.skubware.opentraining.activity.WhatsNewDialog.java
de.skubware.opentraining.activity.acra.ACRACrashReportMailer.java
de.skubware.opentraining.activity.acra.ACRAFeedbackMailer.java
de.skubware.opentraining.activity.acra.OpenTrainingApplication.java
de.skubware.opentraining.activity.acra.RequestExerciseUpdate.java
de.skubware.opentraining.activity.create_exercise.CreateExerciseActivity.java
de.skubware.opentraining.activity.create_exercise.CustomSpinner.java
de.skubware.opentraining.activity.create_exercise.DescriptionFragment.java
de.skubware.opentraining.activity.create_exercise.EditImageMetadataDialog.java
de.skubware.opentraining.activity.create_exercise.EquipmentDataFragment.java
de.skubware.opentraining.activity.create_exercise.ExerciseImageListAdapter.java
de.skubware.opentraining.activity.create_exercise.ImageFragment.java
de.skubware.opentraining.activity.create_exercise.MuscleDataFragment.java
de.skubware.opentraining.activity.create_exercise.NameFragment.java
de.skubware.opentraining.activity.create_exercise.SimpleDataFragment.java
de.skubware.opentraining.activity.create_exercise.SpinnerDataFragment.java
de.skubware.opentraining.activity.create_workout.DialogFilterMusclesAndEquipment.java
de.skubware.opentraining.activity.create_workout.DialogWorkoutOverviewFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseDetailOnGestureListener.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListAdapter.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListFragment.java
de.skubware.opentraining.activity.create_workout.SelectMuscleDialog.java
de.skubware.opentraining.activity.create_workout.SendExerciseFeedbackDialogFragment.java
de.skubware.opentraining.activity.create_workout.upload_exercise.ExerciseImage.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseImagesAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.WgerRestService.java
de.skubware.opentraining.activity.manage_workouts.RenameWorkoutDialogFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListFragment.java
de.skubware.opentraining.activity.settings.LicenseDialog.java
de.skubware.opentraining.activity.settings.SettingsActivity.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncResultReceiver.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncService.java
de.skubware.opentraining.activity.settings.sync.RestClient.java
de.skubware.opentraining.activity.settings.sync.SyncFinishedDialog.java
de.skubware.opentraining.activity.settings.sync.WgerImageDownloader.java
de.skubware.opentraining.activity.settings.sync.WgerJSONParser.java
de.skubware.opentraining.activity.start_training.DialogFragmentAddEntry.java
de.skubware.opentraining.activity.start_training.DialogFragmentTrainingEntryTable.java
de.skubware.opentraining.activity.start_training.FExDetailActivity.java
de.skubware.opentraining.activity.start_training.FExDetailFragment.java
de.skubware.opentraining.activity.start_training.FExListActivity.java
de.skubware.opentraining.activity.start_training.FExListAdapter.java
de.skubware.opentraining.activity.start_training.FExListFragment.java
de.skubware.opentraining.activity.start_training.RecoveryTimerManager.java
de.skubware.opentraining.activity.start_training.SwipeDismissListViewTouchListener.java
de.skubware.opentraining.activity.start_training.SwipeDismissTouchListener.java
de.skubware.opentraining.activity.start_training.TrainingEntryListAdapter.java
de.skubware.opentraining.basic.ActivationLevel.java
de.skubware.opentraining.basic.ExerciseTag.java
de.skubware.opentraining.basic.ExerciseType.java
de.skubware.opentraining.basic.FSet.java
de.skubware.opentraining.basic.FitnessExercise.java
de.skubware.opentraining.basic.IExercise.java
de.skubware.opentraining.basic.License.java
de.skubware.opentraining.basic.Muscle.java
de.skubware.opentraining.basic.SportsEquipment.java
de.skubware.opentraining.basic.TrainingEntry.java
de.skubware.opentraining.basic.Translatable.java
de.skubware.opentraining.basic.Workout.java
de.skubware.opentraining.db.Cache.java
de.skubware.opentraining.db.DataHelper.java
de.skubware.opentraining.db.DataProvider.java
de.skubware.opentraining.db.IDataProvider.java
de.skubware.opentraining.db.parser.AbstractJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTagJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTypeXMLParser.java
de.skubware.opentraining.db.parser.IParser.java
de.skubware.opentraining.db.parser.MuscleJSONParser.java
de.skubware.opentraining.db.parser.SportsEquipmentJSONParser.java
de.skubware.opentraining.db.parser.WorkoutXMLParser.java
de.skubware.opentraining.db.parser.XMLSaver.java
de.skubware.opentraining.db.rest.ExerciseImageGSONSerializer.java
de.skubware.opentraining.db.rest.ExerciseTypeGSONSerializer.java
de.skubware.opentraining.db.rest.LanguageGSONDeserializer.java
de.skubware.opentraining.db.rest.MuscleGSONDeserializer.java
de.skubware.opentraining.db.rest.ServerModel.java
de.skubware.opentraining.db.rest.SportsEquipmentGSONDeserializer.java
de.skubware.opentraining.exporter.HTMLExporter.java
de.skubware.opentraining.exporter.LaTeXExporter.java
de.skubware.opentraining.exporter.WorkoutExporter.java