Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/v1.0-alpha.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
southwood committed Apr 14, 2015
2 parents 5c0e011 + cfaa7c8 commit 27705ce
Show file tree
Hide file tree
Showing 130 changed files with 4,881 additions and 3,426 deletions.
124 changes: 73 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
# Application Insights for Android

This project provides an Android SDK for Application Insights. [Application Insights](http://azure.microsoft.com/en-us/services/application-insights/) is a service that allows developers to keep their applications available, performing, and succeeding. This module allows you to send telemetry of various kinds (events, traces, exceptions, etc.) to the Application Insights service where your data can be visualized in the Azure Portal.

[ ![Download](https://api.bintray.com/packages/appinsights-android/maven/AppInsights-Android/images/download.svg) ](https://bintray.com/appinsights-android/maven/AppInsights-Android/_latestVersion)

# Application Insights for Android (1.0-Alpha.5)

This project provides an Android SDK for Application Insights. [Application Insights](http://azure.microsoft.com/en-us/services/application-insights/) is a service that allows developers to keep their applications available, performing, and succeeding. This module allows you to send telemetry of various kinds (events, traces, exceptions, etc.) to the Application Insights service where your data can be visualized in the Azure Portal.

**Release Notes**
* The sdk can now differentiate between handled and unhandeled exceptions (aka crashes).
* Updated example app
* Changed internal package structure
* Updated and cleaner internal architecture of the sdk
* Improved performance because of new architecture.


**Breaking Changes**

* Setup and start of the Application Insights SDK are now done using the new umbrella class `AppInsights` instead of `TelemetryClient`.

## Setup ##



**Add a compile dependency for the SDK**

Per-module
Per-module

```java
dependencies {
compile 'com.microsoft.azure:applicationinsights-android:+'
}
}
```

**Configure the instrumentation key and add permissions**

>Please see the "[Getting an Application Insights Instrumentation Key](https://github.com/Microsoft/AppInsights-Home/wiki#getting-an-application-insights-instrumentation-key)" section of the wiki for more information on acquiring a key.
Please see the "[Getting an Application Insights Instrumentation Key](https://github.com/Microsoft/AppInsights-Home/wiki#getting-an-application-insights-instrumentation-key)" section of the wiki for more information on acquiring a key.

Plase add the two permissions for `INTERNET` and `ACCESS_NETWORK_STATE` into your app's `AndroidManifest.xml` as well as the property for your instrumentation key as follows. Replace `${AI_INSTRUMENTATION_KEY}` with your instrumentation key or the variable leave it and use gradle.properties to set it.

AndroidManifest.xml
```xml
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
Expand All @@ -32,15 +44,19 @@ AndroidManifest.xml
android:name="com.microsoft.applicationinsights.instrumentationKey"
android:value="${AI_INSTRUMENTATION_KEY}" />
</application>
</manifest>
```
</manifest>
```

**Optional: load instrumentation key from gradle**

~/.gradle/gradle.properties

```java
ai_instrumentation_key=<KEY_PLACEHOLDER>
```
Top-level build file
```

Top-level gradle build file

```java
android {
buildTypes {
Expand All @@ -49,28 +65,53 @@ android {
}
}
}
```



```

**Optional: set instrumentation key in code**

It is also possible to set the instrumentation key of your app in code. This will override the one you might have set in your gradle or manifest file. Setting the instrumentation key programmatically can be done while setting up AppInsights:

```java
AppInsights.setup(this, "<YOUR-IKEY-GOES-HERE>");
AppInsights.start();
```

## Usage ##

Add the following import to your apps root activity

```java
import com.microsoft.applicationinsights.TelemetryClient;
```

And add
```java
AppInsights.setup(this);
AppInsights.start();
```

in the activity's `onCreate`-callback.

A typicall onCreate-method looks like this.

```java
public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
//... other initialization code ...//


TelemetryClient client = TelemetryClient.getInstance(this);
client.trackTrace("example trace");
client.trackEvent("example event");
client.trackException(new Exception("example error"));
client.trackMetric("example metric", 1);
AppInsights.setup(this);
//... other initialization code ...//
AppInsights.start();

// track telemetry data
TelemetryClient client = TelemetryClient.getInstance();
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("property1", "my custom property");
client.trackEvent("sample event", properties);
client.trackTrace("sample trace");
client.trackMetric("sample metric", 3);
}
}
```
Expand All @@ -79,42 +120,23 @@ public class MyActivity extends Activity {

> Note: this only works in Android SDK version 15 and up (Ice Cream Sandwich+)
**Extend Application and register for life cycle callbacks**
**Register for life cycle callbacks**

MyApplication.java
```java
import com.microsoft.applicationinsights.ApplicationLifeCycleEventTracking;
```
```java
public class MyApplication extends Application {
In order to register for lifecycle events, you only have to do the following call

@Override
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void onCreate() {
super.onCreate();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
registerActivityLifecycleCallbacks(LifeCycleTracking.getInstance());
}
}
}
```
AndroidManifest.xml
```xml
<manifest>
<application android:name="MyApplication"></application>
</manifest>
```java
// track activity lifecycle / session states
AppInsights.enableActivityTracking(this.getApplication());
```


Please note, that this will only work after `AppInsights.start()`has been called. Furthermore, the telemetry feature must not be disabled.

## Documentation ##

http://microsoft.github.io/AppInsights-Android/


[http://microsoft.github.io/AppInsights-Android/]()

## Contributing ##


**Development environment**

* Install <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" target="_blank">JDK 1.8</a>
Expand Down
1 change: 0 additions & 1 deletion app-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name="MyApplication"
android:theme="@style/AppTheme" >
<activity
android:name=".ItemListActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;


Expand All @@ -12,7 +12,7 @@
* activity is only used on handset devices. On tablet-size devices,
* item details are presented side-by-side with a list of items
* in a {@link ItemListActivity}.
* <p/>
* <p>
* This activity is mostly just a 'shell' activity containing nothing
* more than a {@link ItemDetailFragment}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.view.ViewGroup;
import android.widget.TextView;


import com.microsoft.applicationinsights.appsample.dummy.DummyContent;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.microsoft.applicationinsights.TelemetryClient;

import com.microsoft.applicationinsights.AppInsights;

/**
* An activity representing a list of Items. This activity
Expand All @@ -14,11 +13,11 @@
* lead to a {@link ItemDetailActivity} representing
* item details. On tablets, the activity presents the list of items and
* item details side-by-side using two vertical panes.
* <p/>
* <p>
* The activity makes heavy use of fragments. The list of items is a
* {@link ItemListFragment} and the item details
* (if present) is a {@link ItemDetailFragment}.
* <p/>
* <p>
* This activity also implements the required
* {@link ItemListFragment.Callbacks} interface
* to listen for item selections.
Expand Down Expand Up @@ -51,12 +50,11 @@ protected void onCreate(Bundle savedInstanceState) {
.setActivateOnItemClick(true);
}

TelemetryClient client = TelemetryClient.getInstance(this);
client.trackTrace("example trace");
client.trackEvent("example event");
client.trackException(new Exception("example error"));
client.trackMetric("example metric", 1);
client.flush();
AppInsights.setup(this);
AppInsights.start();

// track activity lifecycle (note this only needs to be done once per application)
AppInsights.enableActivityTracking(this.getApplication());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.microsoft.applicationinsights.AppInsights;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.appsample.dummy.DummyContent;

import java.util.ArrayList;

/**
* A list fragment representing a list of Items. This fragment
* also supports tablet devices by allowing list items to be given an
* 'activated' state upon selection. This helps indicate which item is
* currently being viewed in a {@link ItemDetailFragment}.
* <p/>
* <p>
* Activities containing this fragment MUST implement the {@link Callbacks}
* interface.
*/
Expand Down Expand Up @@ -77,10 +80,10 @@ public void onCreate(Bundle savedInstanceState) {

// TODO: replace with a real list adapter.
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
}

@Override
Expand All @@ -89,7 +92,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {

// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
Expand Down Expand Up @@ -117,12 +120,56 @@ public void onDetach() {
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
TelemetryClient client = TelemetryClient.getInstance();

switch (position) {
case 0:
client.trackEvent("something wicked this way comes");
break;
case 1:
client.trackTrace("something wicked this way comes");
break;
case 2:

ArrayList<Object> myList = new ArrayList<Object>();
try{
Object test = myList.get(2);
}catch(Exception e){
client.trackHandledException(e);
}
break;
case 3:
crashMe1();
break;
case 4:
AppInsights.INSTANCE.sendPendingData();
break;

default:
break;
}

if(position == 1) {

}

// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
}

private void crashMe1() {
crashMe2();
}

private void crashMe2() {
crashMe3();
}

private void crashMe3() {
throw new RuntimeException("oh no!");
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand All @@ -140,8 +187,8 @@ public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}

private void setActivatedPosition(int position) {
Expand Down
Loading

0 comments on commit 27705ce

Please sign in to comment.