it.tidalwave.bluebill.mobile.android.util.Analytics Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: Analytics.java,v a9640977ac37 2010/08/07 19:02:37 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.util;
import java.io.File;
import javax.annotation.Nonnull;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.openide.util.NbBundle;
import it.tidalwave.bluebill.mobile.android.about.AboutActivity;
import android.os.Build;
import com.eaio.uuid.UUID;
import it.tidalwave.mobile.io.MasterFileSystem;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.util.logging.Logger;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.CheckForNull;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public final class Analytics
{
private static final String CLASS = Analytics.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@Nonnull
public static URL createURL (final @Nonnull String string)
throws IOException
{
try
{
final StringBuilder buffer = new StringBuilder(string);
buffer.append(string.contains("?") ? "&" : "?");
buffer.append("installId=").append(encode(getInstallId()));
buffer.append("&blueBillVersion=").append(encode(NbBundle.getMessage(AboutActivity.class, "title")));
buffer.append("&osVersion=").append(encode(System.getProperty("os.version")));
buffer.append("&javaVersion=").append(encode(System.getProperty("java.version")));
buffer.append("&userLanguage=").append(encode(System.getProperty("user.language")));
buffer.append("&userRegion=").append(encode(System.getProperty("user.region")));
buffer.append("&board=").append(encode(Build.BOARD));
buffer.append("&brand=").append(encode(Build.BRAND));
buffer.append("&device=").append(encode(Build.DEVICE));
buffer.append("&model=").append(encode(Build.MODEL));
buffer.append("&product=").append(encode(Build.PRODUCT));
buffer.append("&versionIncremental=").append(encode(Build.VERSION.INCREMENTAL));
buffer.append("&versionRelease=").append(encode(Build.VERSION.RELEASE));
buffer.append("&versionSDK=").append(encode(Build.VERSION.SDK));
return new URL(buffer.toString());
}
catch (UnsupportedEncodingException e)
{
throw new MalformedURLException(e.toString());
}
}
@Nonnull
private static String encode (final @Nonnull String string)
throws UnsupportedEncodingException
{
return (string == null) ? "" : URLEncoder.encode(string, "UTF-8");
}
@CheckForNull
private static String getInstallId()
throws IOException
{
final File file = Locator.find(MasterFileSystem.class).getInternalFileSystem().getFile("installId");
System.err.println("installId file: " + file.getAbsolutePath());
String installId = null;
try
{
final BufferedReader br = new BufferedReader(new FileReader(file));
installId = br.readLine();
br.close();
}
catch (IOException e)
{
installId = new UUID().toString();
logger.info("Cannot find an installId: created %s", installId);
final PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.println(installId);
pw.close();
}
return installId;
}
}