/*
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * 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 com.namelessdev.mpdroid;

import com.namelessdev.mpdroid.adapters.SeparatedListAdapter;
import com.namelessdev.mpdroid.adapters.SeparatedListDataBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AboutActivity extends Activity {

    private static final MPDApplication APP = MPDApplication.getInstance();

    public static String getVersionName(final Class<Activity> cls) {
        try {
            final ComponentName comp = new ComponentName(APP, cls);
            final PackageInfo pinfo = APP.getPackageManager()
                    .getPackageInfo(comp.getPackageName(), 0);
            return pinfo.versionName + " (" + pinfo.versionCode + ')';
        } catch (final PackageManager.NameNotFoundException ignored) {
            return null;
        }
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);

        final ListView listView = (ListView) findViewById(android.R.id.list);

        final LayoutInflater inflater = LayoutInflater.from(this);
        final View headerView = inflater.inflate(R.layout.about_header, null, false);
        final TextView versionInfo = (TextView) headerView.findViewById(R.id.text_version);
        versionInfo.setText(R.string.version);
        versionInfo.append(": " + getVersionName(Activity.class));

        listView.setHeaderDividersEnabled(false);
        listView.addHeaderView(headerView);

        String[] tmpStringArray;
        final List<Object> listItems = new ArrayList<>();
        listItems.add(getString(R.string.about_libraries));
        tmpStringArray = getResources().getStringArray(R.array.libraries_array);
        for (final String tmpString : tmpStringArray) {
            listItems.add(new AboutListItem(tmpString));
        }
        listItems.add(getString(R.string.about_authors));
        tmpStringArray = getResources().getStringArray(R.array.authors_array);
        for (final String tmpString : tmpStringArray) {
            listItems.add(new AboutListItem(tmpString));
        }

        listView.setAdapter(new SeparatedListAdapter(this, android.R.layout.simple_list_item_1,
                R.layout.list_separator, new SeparatedListDataBinder() {
            @Override
            public boolean isEnabled(final int position, final List<?> items, final Object item) {
                return false;
            }

            @Override
            public void onDataBind(final Context context, final View targetView,
                    final List<?> items,
                    final Object item, final int position) {
                ((TextView) targetView.findViewById(android.R.id.text1)).setText(item.toString());
            }
        }, listItems));
    }

    private static class AboutListItem {

        private final String mText;

        public AboutListItem(final String text) {
            super();
            mText = text;
        }

        @Override
        public String toString() {
            return mText;
        }
    }

}

@@ -974,16 +974,41 @@
 Bundle;%0A
+import android.util.Log;%0A
 import a
@@ -1298,24 +1298,80 @@
 nstance();%0A%0A
+    private static final String TAG = %22AboutActivity%22;%0A%0A
     public s
@@ -1423,24 +1423,60 @@
 ity%3E cls) %7B%0A
+        String versionName = null;%0A%0A
         try 
@@ -1576,17 +1576,23 @@
 geInfo p
-i
+ackageI
 nfo = AP
@@ -1692,17 +1692,30 @@
     
-return pi
+versionName = packageI
 nfo.
@@ -1736,17 +1736,23 @@
 %22 (%22 + p
-i
+ackageI
 nfo.vers
@@ -1830,15 +1830,9 @@
 ion 
-ignored
+e
 ) %7B%0A
@@ -1847,30 +1847,96 @@
     
-return null;%0A        %7D
+Log.e(TAG, %22Failed to get the version name.%22, e);%0A        %7D%0A%0A        return versionName;
 %0A   

Return at the end of the method.