/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * 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.kotcrab.vis.editor.module.editor;

import com.brsanthu.googleanalytics.AppViewHit;
import com.brsanthu.googleanalytics.ExceptionHit;
import com.brsanthu.googleanalytics.GoogleAnalytics;
import com.google.common.eventbus.Subscribe;
import com.kotcrab.vis.editor.App;
import com.kotcrab.vis.editor.event.ExceptionEvent;
import com.kotcrab.vis.editor.module.InjectModule;
import com.kotcrab.vis.editor.ui.toast.EnableAnalyticsToast;
import com.kotcrab.vis.editor.ui.toast.EnableAnalyticsToast.EnableAnalyticsToastListener;
import org.apache.commons.codec.binary.Base64;

/** @author Kotcrab */
public class AnalyticsModule extends EditorModule {
	public static final String ID = "VUEtNDMwODg5MjAtNQ==";

	@InjectModule ToastModule toastModule;
	@InjectModule GeneralSettingsModule generalSettings;

	private GoogleAnalytics analytics;

	@Override
	public void postInit () {
		if (generalSettings.getAnalyticsState() == AnalyticsState.SHOW_QUESTION) {
			toastModule.show(new EnableAnalyticsToast(new EnableAnalyticsToastListener() {
				@Override
				public void agreed () {
					generalSettings.setAnalyticsState(AnalyticsState.ENABLED);
					postAppStarted();
				}

				@Override
				public void disagreed () {
					generalSettings.setAnalyticsState(AnalyticsState.DISABLED);
				}
			}));
		} else {
			postAppStarted();
		}

		App.eventBus.register(this);
	}

	@Override
	public void dispose () {
		App.eventBus.unregister(this);
		if (analytics != null) analytics.close();
	}

	private void postAppStarted () {
		if (prepare()) {
			analytics.postAsync(new AppViewHit("VisEditor", App.VERSION, "Startup"));
		}
	}

	@Subscribe
	public void handleExceptionEvent (ExceptionEvent event) {
		if (prepare()) {
			analytics.postAsync(new ExceptionHit(event.throwable.getMessage(), true));
		}
	}

	private boolean prepare () {
		if (isAnalyticsEnabled() == false) return false;

		if (analytics == null) {
			analytics = new GoogleAnalytics(new String(Base64.decodeBase64(ID.getBytes())));
		}

		return true;
	}

	private boolean isAnalyticsEnabled () {
		return generalSettings.getAnalyticsState() == AnalyticsState.ENABLED;
	}

	public enum AnalyticsState {
		SHOW_QUESTION, ENABLED, DISABLED
	}
}

@@ -1176,16 +1176,106 @@
 ase64;%0A%0A
+import java.nio.ByteBuffer;%0Aimport java.nio.CharBuffer;%0Aimport java.nio.charset.Charset;%0A%0A
 /** @aut
@@ -2413,86 +2413,727 @@
 %0A%09%09%09
-analytics.postAsync(new ExceptionHit(event.throwable.getMessage()
+StackTraceElement%5B%5D elements = event.throwable.getStackTrace();%0A%09%09%09String fullMsg = event.throwable + %22 %22 + (elements.length %3E 0 ? elements%5B0%5D + %22 %22 : %22%22) + event.throwable.getMessage();%0A%09%09%09byte%5B%5D output = new byte%5B150%5D; //GA only allows 150 bytes%0A%09%09%09int returnValue = truncateUtf8(fullMsg, output);%0A%09%09%09String truncateMsg = new String(output, 0, returnValue);%0A%09%09%09analytics.postAsync(new ExceptionHit(truncateMsg, true));%0A%09%09%7D%0A%09%7D%0A%0A%09public static int truncateUtf8 (String input, byte%5B%5D output) %7B%0A%09%09ByteBuffer outBuf = ByteBuffer.wrap(output);%0A%09%09CharBuffer inBuf = CharBuffer.wrap(input.toCharArray());%0A%0A%09%09Charset utf8 = Charset.forName(%22UTF-8%22);%0A%09%09utf8.newEncoder().encode(inBuf, outBuf
 , true)
-)
 ;%0A%09%09
-%7D
+return outBuf.position();
 %0A%09%7D%0A

Save more exception details