├── library
├── gradle.properties
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── uphyca
│ │ └── galette
│ │ ├── LongFieldBuilder.java
│ │ ├── StringFieldBuilder.java
│ │ ├── Fields.java
│ │ ├── ContextProvider.java
│ │ ├── TrackerProvider.java
│ │ ├── SendScreenView.java
│ │ ├── SendAppView.java
│ │ ├── FieldBuilder.java
│ │ ├── HitInterceptor.java
│ │ ├── SendEvent.java
│ │ ├── ScreenViewBuilderDelegate.java
│ │ ├── EventBuilderDelegate.java
│ │ └── GAlette.java
├── consumer-rules.pro
└── build.gradle
├── plugin
├── gradle.properties
├── src
│ └── main
│ │ ├── resources
│ │ └── META-INF
│ │ │ └── gradle-plugins
│ │ │ ├── galette.properties
│ │ │ └── com.uphyca.galette.properties
│ │ └── groovy
│ │ └── com
│ │ └── uphyca
│ │ └── galette
│ │ └── GAlettePlugin.groovy
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── tests
├── src
│ ├── main
│ │ ├── ic_launcher-web.png
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── uphyca
│ │ │ └── galette
│ │ │ └── tests
│ │ │ └── TestActivity.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── uphyca
│ │ └── galette
│ │ └── tests
│ │ ├── HitInterceptorTest.java
│ │ └── GAletteTest.java
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── proguard-rules.txt
├── build.gradle
├── gradlew.bat
└── gradlew
├── example
├── src
│ └── main
│ │ ├── ic_launcher-web.png
│ │ ├── res
│ │ ├── drawable-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── styles.xml
│ │ │ └── dimens.xml
│ │ ├── values-w820dp
│ │ │ └── dimens.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── java
│ │ └── com
│ │ │ └── uphyca
│ │ │ └── example
│ │ │ └── galette
│ │ │ ├── MyApplication.java
│ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── proguard-rules.txt
├── build.gradle
├── gradlew.bat
└── gradlew
├── instrumentation
├── gradle.properties
├── src
│ ├── test
│ │ └── java
│ │ │ ├── android
│ │ │ ├── content
│ │ │ │ ├── Context.java
│ │ │ │ └── ContextWrapper.java
│ │ │ ├── support
│ │ │ │ └── v4
│ │ │ │ │ └── app
│ │ │ │ │ ├── FragmentActivity.java
│ │ │ │ │ └── Fragment.java
│ │ │ ├── app
│ │ │ │ ├── Fragment.java
│ │ │ │ ├── Application.java
│ │ │ │ ├── Activity.java
│ │ │ │ └── Service.java
│ │ │ └── view
│ │ │ │ ├── View.java
│ │ │ │ └── ContextThemeWrapper.java
│ │ │ └── com
│ │ │ └── uphyca
│ │ │ └── galette
│ │ │ ├── ContextProvider.java
│ │ │ ├── SendEvent.java
│ │ │ ├── SendScreenView.java
│ │ │ ├── SendAppView.java
│ │ │ ├── GAlette.java
│ │ │ └── GAletteInstrumentationTest.java
│ └── main
│ │ └── java
│ │ └── com
│ │ └── uphyca
│ │ └── galette
│ │ └── GAletteInstrumentation.java
└── build.gradle
├── .gitignore
├── settings.gradle
├── RELEASING.md
├── gradle.properties
├── wercker.yml
├── CHANGELOG.md
├── gradlew.bat
├── gradlew
├── README.md
└── LICENSE
/library/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=GAlette
2 | POM_ARTIFACT_ID=galette
3 | POM_PACKAGING=aar
4 |
--------------------------------------------------------------------------------
/plugin/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=GAlette Plugin
2 | POM_ARTIFACT_ID=galette-plugin
3 | POM_PACKAGING=jar
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/tests/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/tests/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/example/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/example/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/tests/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/tests/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/example/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/example/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/plugin/src/main/resources/META-INF/gradle-plugins/galette.properties:
--------------------------------------------------------------------------------
1 | implementation-class=com.uphyca.galette.GAlettePlugin
2 |
3 |
--------------------------------------------------------------------------------
/instrumentation/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=GAlette Instrumentation
2 | POM_ARTIFACT_ID=galette-instrumentation
3 | POM_PACKAGING=jar
4 |
--------------------------------------------------------------------------------
/plugin/src/main/resources/META-INF/gradle-plugins/com.uphyca.galette.properties:
--------------------------------------------------------------------------------
1 | implementation-class=com.uphyca.galette.GAlettePlugin
2 |
3 |
--------------------------------------------------------------------------------
/example/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/example/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/example/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/example/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uPhyca/GAlette/HEAD/example/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | GAlette-example
5 |
6 |
7 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/content/Context.java:
--------------------------------------------------------------------------------
1 | package android.content;
2 |
3 | /**
4 | * Stub
5 | */
6 | public abstract class Context {
7 | public abstract Context getApplicationContext();
8 | }
9 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/support/v4/app/FragmentActivity.java:
--------------------------------------------------------------------------------
1 | package android.support.v4.app;
2 |
3 | import android.app.Activity;
4 |
5 | /**
6 | * Stub
7 | */
8 | public class FragmentActivity extends Activity {
9 | }
10 |
--------------------------------------------------------------------------------
/library/consumer-rules.pro:
--------------------------------------------------------------------------------
1 | -keepclassmembernames class * {
2 | @com.uphyca.galette.SendScreenView *;
3 | @com.uphyca.galette.SendEvent *;
4 | }
5 |
6 | -keepclassmembers class * implements com.uphyca.galette.FieldBuilder {
7 | ();
8 | }
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/example/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/tests/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/example/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/example/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .classpath
2 | .project
3 | .settings
4 | eclipsebin
5 |
6 | bin
7 | gen
8 | build
9 | out
10 | lib
11 |
12 | target
13 | pom.xml.*
14 | release.properties
15 |
16 | .idea
17 | *.iml
18 | classes
19 |
20 | obj
21 |
22 | .gradle
23 | local.properties
24 |
25 | .DS_Store
26 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/app/Fragment.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | /**
4 | * Stub
5 | */
6 | public class Fragment {
7 |
8 | private Activity mActivity;
9 |
10 | public Activity getActivity() {
11 | return mActivity;
12 | }
13 |
14 | public void setActivity(Activity activity) {
15 | mActivity = activity;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/view/View.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import android.content.Context;
4 |
5 | /**
6 | * Stub
7 | */
8 | public class View {
9 |
10 | private Context mContext;
11 |
12 | public View(Context context) {
13 | mContext = context;
14 | }
15 |
16 | public Context getContext() {
17 | return mContext;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/example/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/support/v4/app/Fragment.java:
--------------------------------------------------------------------------------
1 | package android.support.v4.app;
2 |
3 | /**
4 | * Stub
5 | */
6 | public class Fragment {
7 |
8 | private FragmentActivity mActivity;
9 |
10 | public FragmentActivity getActivity() {
11 | return mActivity;
12 | }
13 |
14 | public void setActivity(FragmentActivity activity) {
15 | mActivity = activity;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/app/Application.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | import android.content.Context;
4 | import android.content.ContextWrapper;
5 |
6 | /**
7 | * Stub
8 | */
9 | public class Application extends ContextWrapper {
10 |
11 | public Application() {
12 | super(null);
13 | }
14 |
15 | public void attach(Context context) {
16 | attachBaseContext(context);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include 'library'
2 | project(':library').name = 'galette'
3 |
4 | include 'instrumentation'
5 | project(':instrumentation').name = 'galette-instrumentation'
6 |
7 | include 'plugin'
8 | project(':plugin').name = 'galette-plugin'
9 |
10 | //include 'tests'
11 | //project(':tests').name = 'tests'
12 | //
13 | //include 'example'
14 | //project(':example').name = 'example'
15 |
16 | project(':').name = 'galette-parent'
17 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/app/Activity.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | import android.view.ContextThemeWrapper;
4 |
5 | /**
6 | * Stub
7 | */
8 | public class Activity extends ContextThemeWrapper {
9 |
10 | private Application mApplication;
11 |
12 | public Application getApplication() {
13 | return mApplication;
14 | }
15 |
16 | public void attach(Application application) {
17 | mApplication = application;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/app/Service.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | import android.content.ContextWrapper;
4 |
5 | /**
6 | * Stub
7 | */
8 | public class Service extends ContextWrapper {
9 |
10 | private Application mApplication;
11 |
12 | public Service() {
13 | super(null);
14 | }
15 |
16 | public Application getApplication() {
17 | return mApplication;
18 | }
19 |
20 | public void setApplication(Application application) {
21 | mApplication = application;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/tests/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/content/ContextWrapper.java:
--------------------------------------------------------------------------------
1 | package android.content;
2 |
3 | /**
4 | * Stub
5 | */
6 | public class ContextWrapper extends Context {
7 |
8 | private Context mBase;
9 |
10 | public ContextWrapper(Context base) {
11 | mBase = base;
12 | }
13 |
14 | protected void attachBaseContext(Context base) {
15 | if (mBase != null) {
16 | throw new IllegalStateException("Base context already set");
17 | }
18 | mBase = base;
19 | }
20 |
21 | @Override
22 | public Context getApplicationContext() {
23 | return mBase.getApplicationContext();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/RELEASING.md:
--------------------------------------------------------------------------------
1 | Release Process
2 | ===============
3 |
4 | 1. Update the `CHANGELOG.md` file with relevant info and date.
5 | 2. Update version number in `gradle.properties` file.
6 | 3. Update version number in `README.md` file.
7 | 4. Commit: `git commit -am "Prepare version X.Y.Z."`
8 | 5. Tag: `git tag -a X.Y.Z -m "Version X.Y.Z"`
9 | 6. Push: `git push && git push --tags`
10 | 7. Release: `./gradlew clean check assemble uploadArchives`
11 | 8. Update version number in `gradle.properties` file to next "SNAPSHOT" version.
12 | 9. Commit: `git commit -am "Prepare next development version."`
13 | 10. Push: `git push`
14 | 11. Write a script for steps 2 - 11. *(Optional)*
15 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/example/proguard-rules.txt:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Applications/Android Studio.app/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the ProGuard
5 | # include property in project.properties.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
--------------------------------------------------------------------------------
/tests/proguard-rules.txt:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Applications/Android Studio.app/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the ProGuard
5 | # include property in project.properties.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | VERSION_NAME=0.9.17-SNAPSHOT
2 | GROUP=com.uphyca.galette
3 |
4 | POM_DESCRIPTION=Annotation-triggered tracking along with Google Analytics.
5 | POM_URL=https://github.com/uPhyca/GAlette
6 | POM_SCM_URL=https://github.com/uPhyca/GAlette
7 | POM_SCM_CONNECTION=scm:git@github.com/uPhyca/GAlette.git
8 | POM_SCM_DEV_CONNECTION=scm:git@github.com:uPhyca/GAlette.git
9 | POM_LICENCE_NAME=The Apache Software License, Version 2.0
10 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
11 | POM_LICENCE_DIST=repo
12 | POM_DEVELOPER_ID=uphyca
13 | POM_DEVELOPER_NAME=uPhyca Inc.
14 | POM_ORGANIZATION_NAME=uPhyca Inc.
15 | POM_ORGANIZATION_URL=http://www.uphyca.com/
16 |
17 | POM_ARTIFACT_ID = galette
18 |
19 | org.gradle.jvmargs=-XX:MaxPermSize=512m
20 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/android/view/ContextThemeWrapper.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import android.content.Context;
4 | import android.content.ContextWrapper;
5 |
6 | /**
7 | * Stub
8 | */
9 | public class ContextThemeWrapper extends ContextWrapper {
10 |
11 | private int mThemeResource;
12 | private Context mBase;
13 |
14 | public ContextThemeWrapper() {
15 | super(null);
16 | }
17 |
18 | public ContextThemeWrapper(Context base, int themeres) {
19 | super(base);
20 | mBase = base;
21 | mThemeResource = themeres;
22 | }
23 |
24 | @Override
25 | protected void attachBaseContext(Context newBase) {
26 | super.attachBaseContext(newBase);
27 | mBase = newBase;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/LongFieldBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | public abstract class LongFieldBuilder implements FieldBuilder {
20 | }
21 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/StringFieldBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | public abstract class StringFieldBuilder implements FieldBuilder {
20 | }
21 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/com/uphyca/galette/ContextProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import android.content.Context;
20 |
21 | /**
22 | * Stub
23 | */
24 | public interface ContextProvider {
25 |
26 | Context get();
27 | }
28 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/Fields.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | /**
20 | * Represents field names
21 | */
22 | public enum Fields {
23 | SCREEN_NAME,
24 | CATEGORY,
25 | ACTION,
26 | LABEL,
27 | VALUE
28 | }
29 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/ContextProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import android.content.Context;
20 |
21 | /**
22 | * Provides instances of Context.
23 | */
24 | public interface ContextProvider {
25 |
26 | /**
27 | * Provides instances of Context.
28 | *
29 | * @return a Context instance
30 | */
31 | Context get();
32 | }
33 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/com/uphyca/galette/SendEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.Target;
22 |
23 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 |
25 | /**
26 | * Stub
27 | */
28 | @Target(ElementType.METHOD)
29 | @Retention(RUNTIME)
30 | public @interface SendEvent {
31 | }
32 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/com/uphyca/galette/SendScreenView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.Target;
22 |
23 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 |
25 | /**
26 | * Stub
27 | */
28 | @Target(ElementType.METHOD)
29 | @Retention(RUNTIME)
30 | public @interface SendScreenView {
31 | }
32 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/com/uphyca/galette/SendAppView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.Target;
22 |
23 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 |
25 | /**
26 | * Stub
27 | */
28 | @Target(ElementType.METHOD)
29 | @Retention(RUNTIME)
30 | @Deprecated
31 | public @interface SendAppView {
32 | }
33 |
--------------------------------------------------------------------------------
/wercker.yml:
--------------------------------------------------------------------------------
1 | box: wercker/android
2 | # Build definition
3 | build:
4 | # The steps that will be executed on build
5 | steps:
6 | - script:
7 | name: show base information
8 | code: |
9 | ./gradlew -v
10 | echo $ANDROID_HOME
11 | echo $ANDROID_SDK_VERSION
12 | echo $ANDROID_BUILD_TOOLS
13 | echo $ANDROID_UPDATE_FILTER
14 | echo $ANDROID_NDK_HOME
15 | - android-sdk-update:
16 | filter: platform-tools,build-tools-22.0.1,android-22,sysimg-22,extra-android-m2repository,extra-google-m2repository
17 | - script:
18 | name: build and install
19 | code: |
20 | ./gradlew --full-stacktrace -q --project-cache-dir=$WERCKER_CACHE_DIR build install
21 | - setup-android-emulator:
22 | target: android-22
23 | - script:
24 | name: connectedCheck
25 | code: |
26 | ./gradlew --project-cache-dir=$WERCKER_CACHE_DIR connectedAndroidTest
27 | after-steps:
28 | # Use the build results
29 | - script:
30 | name: inspect build result
31 | code: |
32 | ls -la GettingStarted/build/apk/
33 | cp GettingStarted/build/apk/*.apk $WERCKER_REPORT_ARTIFACTS_DIR
34 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/com/uphyca/galette/GAlette.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette;
2 |
3 | import android.content.Context;
4 |
5 | import java.lang.reflect.Method;
6 |
7 | /**
8 | * Stub
9 | */
10 | public class GAlette {
11 |
12 | interface IGAlette {
13 | void sendAppView(Object target, Context context, Method method, Object[] arguments);
14 |
15 | void sendEvent(Object target, Context context, Method method, Object[] arguments);
16 |
17 | void sendScreenView(Object target, Context context, Method method, Object[] arguments);
18 | }
19 |
20 | private static IGAlette sIGAlette;
21 |
22 | public static void setIGAlette(IGAlette IGAlette) {
23 | sIGAlette = IGAlette;
24 | }
25 |
26 | static void sendAppView(Object target, Context context, Method method, Object[] arguments) {
27 | sIGAlette.sendAppView(target, context, method, arguments);
28 | }
29 |
30 | static void sendScreenView(Object target, Context context, Method method, Object[] arguments) {
31 | sIGAlette.sendScreenView(target, context, method, arguments);
32 | }
33 |
34 | static void sendEvent(Object target, Context context, Method method, Object[] arguments) {
35 | sIGAlette.sendEvent(target, context, method, arguments);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/TrackerProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import com.google.android.gms.analytics.Tracker;
20 |
21 | /**
22 | * Provides instances of Tracker.
23 | * The application should implement this interface and returns the arbitrary Tracker instances.
24 | * Google Analytics SDK v4 for Android - Getting Started
25 | */
26 | public interface TrackerProvider {
27 |
28 | /**
29 | * Provides instances of Tracker.
30 | *
31 | * @param trackerName Identifier of the tracker. null if unspecified.
32 | * @return a Tracker instance
33 | */
34 | Tracker getByName(String trackerName);
35 | }
36 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/SendScreenView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.Target;
22 |
23 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 |
25 | /**
26 | * Screens - Android SDK v4 _ Analytics for Android _ Google Developers
27 | */
28 | @Target(ElementType.METHOD)
29 | @Retention(RUNTIME)
30 | public @interface SendScreenView {
31 |
32 | String trackerName() default "";
33 |
34 | /**
35 | * The name of an application screen.
36 | */
37 | String screenName() default "";
38 |
39 | Class extends FieldBuilder> screenNameBuilder() default StringFieldBuilder.class;
40 | }
41 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/SendAppView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.Target;
22 |
23 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 |
25 | /**
26 | * Screens - Android SDK v4 _ Analytics for Android _ Google Developers
27 | *
28 | * @deprecated Use {@link SendScreenView} instead.
29 | */
30 | @Target(ElementType.METHOD)
31 | @Retention(RUNTIME)
32 | @Deprecated
33 | public @interface SendAppView {
34 |
35 | String trackerName() default "";
36 |
37 | /**
38 | * The name of an application screen.
39 | */
40 | String screenName();
41 |
42 | Class extends FieldBuilder> screenNameBuilder() default StringFieldBuilder.class;
43 | }
44 |
--------------------------------------------------------------------------------
/example/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | google()
4 | jcenter()
5 | mavenCentral()
6 | mavenLocal()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:4.1.2'
10 | classpath 'com.uphyca.galette:galette-plugin:0.9.17-SNAPSHOT'
11 | }
12 | }
13 |
14 | apply plugin: 'com.android.application'
15 | apply plugin: 'com.uphyca.galette'
16 |
17 | repositories {
18 | google()
19 | jcenter()
20 | mavenCentral()
21 | mavenLocal()
22 | }
23 |
24 | def isWercker = "true".equals(System.getenv("WERCKER"))
25 | def preDexEnabled = "true".equals(System.getProperty("pre-dex", "true"))
26 |
27 | android {
28 | compileSdkVersion 30
29 | buildToolsVersion "30.0.3"
30 |
31 | compileOptions {
32 | encoding = "UTF-8"
33 | sourceCompatibility JavaVersion.VERSION_1_7
34 | targetCompatibility JavaVersion.VERSION_1_7
35 | }
36 |
37 | dexOptions {
38 | // Skip pre-dexing when running on Wercker CI or when disabled via -Dpre-dex=false.
39 | preDexLibraries = preDexEnabled && !isWercker
40 | }
41 |
42 | defaultConfig {
43 | minSdkVersion 15
44 | targetSdkVersion 30
45 | versionCode 1
46 | versionName '0.9'
47 | }
48 |
49 | buildTypes {
50 | release {
51 | minifyEnabled false
52 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
53 | }
54 | }
55 | }
56 |
57 | wrapper {
58 | gradleVersion = '6.8.2'
59 | distributionType = Wrapper.DistributionType.ALL
60 | }
61 |
--------------------------------------------------------------------------------
/instrumentation/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | mavenCentral()
5 | }
6 |
7 | dependencies {
8 | classpath 'org.gradle.api.plugins:gradle-nexus-plugin:0.3'
9 | }
10 | }
11 |
12 | apply plugin: 'java'
13 | apply plugin: 'maven'
14 | apply plugin: 'nexus'
15 |
16 | targetCompatibility = JavaVersion.VERSION_1_7
17 | sourceCompatibility = JavaVersion.VERSION_1_7
18 |
19 | group = GROUP
20 | version = VERSION_NAME
21 |
22 | dependencies {
23 | repositories {
24 | jcenter()
25 | mavenCentral()
26 | }
27 |
28 | implementation "org.ow2.asm:asm-all:${asmVersion}"
29 |
30 | testImplementation 'junit:junit:4.11'
31 | testImplementation 'org.mockito:mockito-all:1.10.19'
32 | testImplementation 'org.easytesting:fest-assert-core:2.0M10'
33 | }
34 |
35 | modifyPom {
36 | project {
37 | name "GAlette Instrumentation"
38 | description POM_DESCRIPTION
39 | url POM_URL
40 | inceptionYear '2014'
41 |
42 | scm {
43 | url POM_SCM_URL
44 | connection POM_SCM_CONNECTION
45 | developerConnection POM_SCM_DEV_CONNECTION
46 | }
47 |
48 | licenses {
49 | license {
50 | name POM_LICENCE_NAME
51 | url POM_LICENCE_URL
52 | distribution POM_LICENCE_DIST
53 | }
54 | }
55 |
56 | developers {
57 | developer {
58 | id POM_DEVELOPER_ID
59 | name POM_DEVELOPER_NAME
60 | }
61 | }
62 |
63 | organization {
64 | name POM_ORGANIZATION_NAME
65 | url POM_ORGANIZATION_URL
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/tests/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | google()
4 | jcenter()
5 | mavenCentral()
6 | mavenLocal()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:4.1.2'
10 | classpath 'com.uphyca.galette:galette-plugin:0.9.17-SNAPSHOT'
11 | }
12 | }
13 |
14 | repositories {
15 | google()
16 | }
17 |
18 | apply plugin: 'com.android.library'
19 | apply plugin: 'com.uphyca.galette'
20 |
21 | def isWercker = "true".equals(System.getenv("WERCKER"))
22 | def preDexEnabled = "true".equals(System.getProperty("pre-dex", "true"))
23 |
24 | android {
25 | compileSdkVersion 30
26 | buildToolsVersion "30.0.3"
27 |
28 | dexOptions {
29 | // Skip pre-dexing when running on Wercker CI or when disabled via -Dpre-dex=false.
30 | preDexLibraries = preDexEnabled && !isWercker
31 | }
32 |
33 | defaultConfig {
34 | minSdkVersion 9
35 | targetSdkVersion 30
36 | versionCode 1
37 | }
38 |
39 | buildTypes {
40 | release {
41 | minifyEnabled false
42 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
43 | }
44 | }
45 | }
46 |
47 | repositories {
48 | jcenter()
49 | mavenCentral()
50 | mavenLocal()
51 | }
52 |
53 | dependencies {
54 | androidTestImplementation ("org.mockito:mockito-core:1.10.19")
55 | androidTestImplementation "com.google.dexmaker:dexmaker:1.2"
56 | androidTestImplementation ("com.google.dexmaker:dexmaker-mockito:1.2") { exclude group: 'org.mockito' }
57 | androidTestImplementation "com.squareup:fest-android:1.0.8@aar"
58 | }
59 |
60 | wrapper {
61 | gradleVersion = '6.8.2'
62 | distributionType = Wrapper.DistributionType.ALL
63 | }
64 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/FieldBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.reflect.Method;
20 |
21 | /**
22 | * Helper class for building field value.
23 | * The Constructor should be no-arguments and public.
24 | *
25 | * @param type of the field.
26 | * @see SendAppView#screenNameBuilder()
27 | * @see SendEvent#categoryBuilder()
28 | * @see SendEvent#actionBuilder()
29 | * @see SendEvent#labelBuilder()
30 | * @see SendEvent#valueBuilder()
31 | */
32 | public interface FieldBuilder {
33 |
34 | /**
35 | * @param fields the name of the field
36 | * @param fieldValue the value that is specified in annotations
37 | * @param declaredObject the instance on which the method was invoked
38 | * @param method the method invoked on the instance
39 | * @param arguments an array of objects containing the parameters passed to the method, or null if no arguments are expected. Primitive types are boxed.
40 | * @return field value
41 | */
42 | T build(Fields fields, T fieldValue, Object declaredObject, Method method, Object[] arguments);
43 | }
44 |
--------------------------------------------------------------------------------
/plugin/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | mavenCentral()
5 | }
6 |
7 | dependencies {
8 | classpath 'org.gradle.api.plugins:gradle-nexus-plugin:0.3'
9 | }
10 | }
11 |
12 | apply plugin: 'groovy'
13 | apply plugin: 'maven'
14 | apply plugin: 'nexus'
15 |
16 | targetCompatibility = JavaVersion.VERSION_1_7
17 | sourceCompatibility = JavaVersion.VERSION_1_7
18 |
19 | group = GROUP
20 | version = VERSION_NAME
21 |
22 | dependencies {
23 | repositories {
24 | jcenter()
25 | mavenCentral()
26 | }
27 |
28 | implementation gradleApi()
29 | implementation localGroovy()
30 | implementation 'com.android.tools.build:gradle:1.3.1'
31 | }
32 |
33 | install {
34 | repositories.mavenInstaller {
35 | pom.artifactId = 'galette-plugin'
36 | }
37 | }
38 |
39 | uploadArchives {
40 | repositories.mavenDeployer {
41 | pom.artifactId = 'galette-plugin'
42 | }
43 | }
44 |
45 | modifyPom {
46 | project {
47 | name "GAlette Plugin"
48 | description POM_DESCRIPTION
49 | url POM_URL
50 | inceptionYear '2014'
51 |
52 | scm {
53 | url POM_SCM_URL
54 | connection POM_SCM_CONNECTION
55 | developerConnection POM_SCM_DEV_CONNECTION
56 | }
57 |
58 | licenses {
59 | license {
60 | name POM_LICENCE_NAME
61 | url POM_LICENCE_URL
62 | distribution POM_LICENCE_DIST
63 | }
64 | }
65 |
66 | developers {
67 | developer {
68 | id POM_DEVELOPER_ID
69 | name POM_DEVELOPER_NAME
70 | }
71 | }
72 |
73 | organization {
74 | name POM_ORGANIZATION_NAME
75 | url POM_ORGANIZATION_URL
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/example/src/main/java/com/uphyca/example/galette/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.example.galette;
2 |
3 | import android.app.Application;
4 | import android.os.Build;
5 |
6 | import com.google.android.gms.analytics.GoogleAnalytics;
7 | import com.google.android.gms.analytics.Logger;
8 | import com.google.android.gms.analytics.Tracker;
9 | import com.uphyca.galette.HitInterceptor;
10 | import com.uphyca.galette.TrackerProvider;
11 |
12 | public class MyApplication extends Application implements TrackerProvider, HitInterceptor.Provider {
13 |
14 | private Tracker mTracker;
15 |
16 | private HitInterceptor hitInterceptor = new HitInterceptor() {
17 | @Override
18 | public void onEvent(EventFacade eventFacade) {
19 | eventFacade.setCustomDimension(1, Build.MODEL);
20 | }
21 |
22 | @Override
23 | public void onScreenView(ScreenViewFacade screenViewFacade) {
24 | screenViewFacade.setCustomDimension(1, Build.MODEL);
25 | }
26 | };
27 |
28 | @Override
29 | public void onCreate() {
30 | super.onCreate();
31 | GoogleAnalytics ga = GoogleAnalytics.getInstance(this);
32 | ga.setLocalDispatchPeriod(1);
33 |
34 | // To enable debug logging on a device run:
35 | // adb shell setprop log.tag.GAv4 DEBUG
36 | // adb logcat -s GAv4
37 |
38 | // Logger is deprecated. To enable debug logging, please run:
39 | // adb shell setprop log.tag.GAv4 DEBUG
40 | // ga.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
41 |
42 | mTracker = ga.newTracker("SET-YOUR-TRACKING-ID");
43 | }
44 |
45 | @Override
46 | public Tracker getByName(String trackerName) {
47 | return mTracker;
48 | }
49 |
50 | @Override
51 | public HitInterceptor getHitInterceptor(String trackerName) {
52 | return hitInterceptor;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/HitInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette;
2 |
3 | import com.google.android.gms.analytics.ecommerce.Product;
4 | import com.google.android.gms.analytics.ecommerce.ProductAction;
5 | import com.google.android.gms.analytics.ecommerce.Promotion;
6 |
7 | import java.util.Map;
8 |
9 | /**
10 | * Intercept every event before it is executed in order to add additional data.
11 | */
12 | public interface HitInterceptor {
13 |
14 | interface Provider {
15 | HitInterceptor getHitInterceptor(String trackerName);
16 | }
17 |
18 | interface HitFacade {
19 |
20 | T setCustomDimension(int index, String dimension);
21 |
22 | T setAll(Map params);
23 |
24 | T setPromotionAction(String action);
25 |
26 | T setProductAction(ProductAction action);
27 |
28 | T setNewSession();
29 |
30 | T addPromotion(Promotion promotion);
31 |
32 | T setCustomMetric(int index, float metric);
33 |
34 | T setCampaignParamsFromUrl(String utmParams);
35 |
36 | T set(String paramName, String paramValue);
37 |
38 | T addProduct(Product product);
39 |
40 | T setNonInteraction(boolean nonInteraction);
41 |
42 | T addImpression(Product product, String impressionList);
43 | }
44 |
45 | interface ScreenViewFacade extends HitFacade {
46 | }
47 |
48 | interface EventFacade extends HitFacade {
49 |
50 | EventFacade setCategory(String category);
51 |
52 | EventFacade setLabel(String label);
53 |
54 | EventFacade setAction(String action);
55 |
56 | EventFacade setValue(long value);
57 | }
58 |
59 | /**
60 | * Intercept event.
61 | *
62 | * @param event
63 | */
64 | void onEvent(EventFacade event);
65 |
66 | /**
67 | * Intercept screen view.
68 | *
69 | * @param screenView
70 | */
71 | void onScreenView(ScreenViewFacade screenView);
72 | }
73 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/SendEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.Target;
22 |
23 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
24 |
25 | /**
26 | * Event Tracking - Android SDK v4 _ Analytics for Android _ Google Developers
27 | */
28 | @Target(ElementType.METHOD)
29 | @Retention(RUNTIME)
30 | public @interface SendEvent {
31 |
32 | String trackerName() default "";
33 |
34 | /**
35 | * The event category
36 | */
37 | String category() default "";
38 |
39 | /**
40 | * The event action
41 | */
42 | String action() default "";
43 |
44 | /**
45 | * The event label
46 | */
47 | String label() default "";
48 |
49 | /**
50 | * The event value
51 | */
52 | long value() default Long.MIN_VALUE;
53 |
54 | Class extends FieldBuilder> categoryBuilder() default StringFieldBuilder.class;
55 |
56 | Class extends FieldBuilder> actionBuilder() default StringFieldBuilder.class;
57 |
58 | Class extends FieldBuilder> labelBuilder() default StringFieldBuilder.class;
59 |
60 | Class extends FieldBuilder> valueBuilder() default LongFieldBuilder.class;
61 | }
62 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | google()
4 | }
5 | dependencies {
6 | classpath 'com.android.tools.build:gradle:4.1.2'
7 | classpath 'org.gradle.api.plugins:gradle-nexus-plugin:0.3'
8 | }
9 | }
10 |
11 | apply plugin: 'com.android.library'
12 |
13 | repositories {
14 | google()
15 | }
16 |
17 | sourceSets {
18 | main { compileClasspath += configurations.provided }
19 | }
20 |
21 | def isWercker = "true".equals(System.getenv("WERCKER"))
22 | def preDexEnabled = "true".equals(System.getProperty("pre-dex", "true"))
23 |
24 | android {
25 | compileSdkVersion 22
26 | buildToolsVersion "30.0.3"
27 |
28 | dexOptions {
29 | // Skip pre-dexing when running on Wercker CI or when disabled via -Dpre-dex=false.
30 | preDexLibraries = preDexEnabled && !isWercker
31 | }
32 |
33 | defaultConfig {
34 | targetSdkVersion 22
35 | minSdkVersion 9
36 | consumerProguardFiles file('consumer-rules.pro')
37 | }
38 |
39 | compileOptions {
40 | encoding = "UTF-8"
41 | sourceCompatibility JavaVersion.VERSION_1_7
42 | targetCompatibility JavaVersion.VERSION_1_7
43 | }
44 |
45 | lintOptions {
46 | abortOnError false
47 | }
48 | }
49 |
50 | dependencies {
51 | implementation "com.google.android.gms:play-services-analytics:${playServicesVersion}"
52 | }
53 |
54 | task install(type: Exec, dependsOn: assemble) {
55 | executable = 'mvn'
56 | args = [
57 | "install:install-file",
58 | "-DgroupId=com.uphyca.galette",
59 | "-DartifactId=galette",
60 | "-Dversion=${VERSION_NAME}",
61 | "-Dpackaging=aar",
62 | "-Dfile=build/outputs/aar/galette-release.aar"
63 | ]
64 | }
65 |
66 | apply from: 'https://raw.githubusercontent.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
67 | // taken from https://github.com/chrisbanes/gradle-mvn-push/issues/39
68 | afterEvaluate { project ->
69 | androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
70 | }
71 |
72 |
--------------------------------------------------------------------------------
/example/src/main/java/com/uphyca/example/galette/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.example.galette;
18 |
19 | import android.app.Activity;
20 | import android.os.Bundle;
21 | import android.view.View;
22 | import com.uphyca.galette.Fields;
23 | import com.uphyca.galette.LongFieldBuilder;
24 | import com.uphyca.galette.SendEvent;
25 | import com.uphyca.galette.SendScreenView;
26 |
27 | import java.lang.reflect.Method;
28 |
29 | public class MainActivity extends Activity {
30 |
31 | private int mClickCount;
32 |
33 | /**
34 | * Annotate arbitrary method to track
35 | */
36 | @Override
37 | @SendScreenView(screenName = "main")
38 | protected void onCreate(Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 | setContentView(R.layout.activity_main);
41 |
42 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 | onButtonClicked(++mClickCount);
46 | }
47 | });
48 | }
49 |
50 | /**
51 | * Annotate arbitrary method to track
52 | */
53 | @SendEvent(category = "button", action = "click", label = "times", valueBuilder = ClickCountValueBuilder.class)
54 | private void onButtonClicked(int count) {
55 | // Do something
56 | }
57 |
58 | /**
59 | * Custom field builder to build click count as value
60 | */
61 | public static class ClickCountValueBuilder extends LongFieldBuilder {
62 | @Override
63 | public Long build(Fields fields, Long fieldValue, Object declaredObject, Method method, Object[] arguments) {
64 | return ((Integer) arguments[0]).longValue();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/tests/src/androidTest/java/com/uphyca/galette/tests/HitInterceptorTest.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette.tests;
2 |
3 | import android.app.Application;
4 | import android.content.Intent;
5 | import android.test.ActivityUnitTestCase;
6 |
7 | import com.google.android.gms.analytics.HitBuilders;
8 | import com.google.android.gms.analytics.Tracker;
9 | import com.uphyca.galette.HitInterceptor;
10 | import com.uphyca.galette.TrackerProvider;
11 |
12 | import org.mockito.InOrder;
13 | import org.mockito.Mock;
14 | import org.mockito.Mockito;
15 | import org.mockito.MockitoAnnotations;
16 |
17 | import java.io.File;
18 |
19 | import static org.mockito.Matchers.any;
20 | import static org.mockito.Mockito.mock;
21 | import static org.mockito.Mockito.verify;
22 |
23 | public class HitInterceptorTest extends ActivityUnitTestCase {
24 |
25 | public class MockApplication extends Application implements TrackerProvider, HitInterceptor.Provider {
26 | @Override
27 | public Tracker getByName(String trackerName) {
28 | return mTracker;
29 | }
30 |
31 | @Override
32 | public HitInterceptor getHitInterceptor(String s) {
33 | return mHitInterceptor;
34 | }
35 | }
36 |
37 | Tracker mTracker;
38 |
39 | HitInterceptor mHitInterceptor;
40 |
41 | public HitInterceptorTest() {
42 | super(TestActivity.class);
43 | }
44 |
45 | @Override
46 | public void setUp() throws Exception {
47 | super.setUp();
48 | ensureDexmakerCacheDir();
49 | mTracker = mock(Tracker.class);
50 | mHitInterceptor = mock(HitInterceptor.class);
51 | setApplication(new MockApplication());
52 | }
53 |
54 | private void ensureDexmakerCacheDir() {
55 | File cacheDir = getInstrumentation().getTargetContext()
56 | .getCacheDir();
57 | System.setProperty("dexmaker.dexcache", cacheDir.toString());
58 | }
59 |
60 | public void testSendScreenView() throws Exception {
61 | startActivity(new Intent(Intent.ACTION_MAIN), null, null);
62 | verify(mHitInterceptor).onScreenView(any(HitInterceptor.ScreenViewFacade.class));
63 | }
64 |
65 | public void testSendEvent() throws Exception {
66 | TestActivity testActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
67 | testActivity.onButtonClick();
68 | verify(mHitInterceptor).onEvent(any(HitInterceptor.EventFacade.class));
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | Change Log
2 | ==========
3 |
4 | Version 0.9.16 *(2016-08-01)*
5 | ----------------------------
6 |
7 | - Updated: Google Play Services 9.2.1
8 |
9 |
10 | Version 0.9.15 *(2016-02-24)*
11 | ----------------------------
12 |
13 | - Fix bugs
14 | - Build error with "Unsupported major.minor version 52.0" on Java 1.7
15 |
16 |
17 | Version 0.9.14 *(2016-01-07)*
18 | ----------------------------
19 |
20 | - Updated: Google Play Services 8.4.0
21 |
22 |
23 | Version 0.9.13 *(2015-11-02)*
24 | ----------------------------
25 |
26 | - Updated: Google Play Services 8.1.0
27 |
28 |
29 | Version 0.9.12 *(2015-06-12)*
30 | ----------------------------
31 |
32 | - Updated: Google Play Services 7.5.0
33 | - New @SendScreenView annotation, @SendAppView is now deperacated
34 | - New HitInterceptor to send arbitary values
35 |
36 |
37 | Version 0.9.11 *(2015-05-25)*
38 | ----------------------------
39 |
40 | - Updated: Google Play Services 7.3.0
41 |
42 |
43 | Version 0.9.10 *(2015-03-11)*
44 | ----------------------------
45 |
46 | - Support Android Gradle Plugin 1.1
47 |
48 |
49 | Version 0.9.9 *(2014-12-17)*
50 | ----------------------------
51 |
52 | - Fix deployment problem
53 |
54 |
55 | Version 0.9.8 *(2014-12-17)*
56 | ----------------------------
57 |
58 | - Updated: Google Play Services 6.5
59 |
60 |
61 | Version 0.9.7 *(2014-06-02)*
62 | ----------------------------
63 |
64 | - Fix: runtime error occurs using with view
65 |
66 |
67 | Version 0.9.6 *(2014-05-31)*
68 | ----------------------------
69 |
70 | - Fix: runtime error occurs using with support-v4 fragment
71 |
72 |
73 | Version 0.9.5 *(2014-05-28)*
74 | ----------------------------
75 |
76 | - New: support inner class
77 | - performance improvement
78 |
79 |
80 | Version 0.9.4 *(2014-05-23)*
81 | ----------------------------
82 |
83 | - New: Support Android Gradle Plugin 0.10
84 | - Updated: Google Play Services 4.4
85 |
86 |
87 | Version 0.9.3 *(2014-05-20)*
88 | ----------------------------
89 |
90 | - Fix build problem via Gradle daemon
91 |
92 |
93 | Version 0.9.2 *(2014-05-20)*
94 | ----------------------------
95 |
96 | - Fix VFY error.
97 | - Change configurations, see README's Getting Started
98 |
99 | Version 0.9.1 *(2014-05-14)*
100 | ----------------------------
101 |
102 | - Accepts views to annotate.
103 |
104 |
105 | Version 0.9.0 *(2014-05-13)*
106 | ----------------------------
107 |
108 | - Initial release.
109 |
--------------------------------------------------------------------------------
/tests/src/main/java/com/uphyca/galette/tests/TestActivity.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette.tests;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import com.uphyca.galette.*;
6 |
7 | import java.lang.reflect.Method;
8 |
9 | public class TestActivity extends Activity {
10 |
11 | @SendScreenView(screenName = "screenName")
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | }
16 |
17 | @SendScreenView(screenName = "screenName", screenNameBuilder = ScreenNameBuilder.class)
18 | void screenNameWithBuilders() {
19 | }
20 |
21 | @SendEvent(category = "category", action = "action")
22 | @SuppressWarnings("unused")
23 | void onButtonClick() {
24 | }
25 |
26 | @SendEvent(category = "category", action = "action", label = "label", value = 1L)
27 | @SuppressWarnings("unused")
28 | void onButtonClickWithOptionalFields() {
29 | }
30 |
31 | @SendEvent(category = "category", action = "action", categoryBuilder = CategoryBuilder.class, actionBuilder = ActionBuilder.class, labelBuilder = LabelBuilder.class, valueBuilder = ValueBuilder.class)
32 | @SuppressWarnings("unused")
33 | void onButtonClickWithBuilders() {
34 | }
35 |
36 | public static class ScreenNameBuilder extends StringFieldBuilder {
37 | @Override
38 | public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
39 | return "screenNameBuilder";
40 | }
41 | }
42 |
43 | public static class CategoryBuilder extends StringFieldBuilder {
44 | @Override
45 | public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
46 | return "categoryBuilder";
47 | }
48 | }
49 |
50 | public static class ActionBuilder extends StringFieldBuilder {
51 | @Override
52 | public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
53 | return "actionBuilder";
54 | }
55 | }
56 |
57 | public static class LabelBuilder extends StringFieldBuilder {
58 | @Override
59 | public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
60 | return "labelBuilder";
61 | }
62 | }
63 |
64 | public static class ValueBuilder extends LongFieldBuilder {
65 | @Override
66 | public Long build(Fields fields, Long fieldValue, Object declaredObject, Method method, Object[] arguments) {
67 | return Long.MAX_VALUE;
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/example/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
18 |
19 |
22 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
36 |
37 |
39 |
42 |
43 |
44 |
45 |
46 |
50 |
51 |
53 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/example/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/tests/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/tests/src/androidTest/java/com/uphyca/galette/tests/GAletteTest.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette.tests;
2 |
3 | import android.app.Application;
4 | import android.content.Intent;
5 | import android.test.ActivityUnitTestCase;
6 | import com.google.android.gms.analytics.HitBuilders;
7 | import com.google.android.gms.analytics.Tracker;
8 | import com.uphyca.galette.TrackerProvider;
9 | import org.mockito.InOrder;
10 | import org.mockito.Mock;
11 | import org.mockito.Mockito;
12 | import org.mockito.MockitoAnnotations;
13 |
14 | import java.io.File;
15 |
16 | import static org.mockito.Mockito.mock;
17 | import static org.mockito.Mockito.verify;
18 |
19 | public class GAletteTest extends ActivityUnitTestCase {
20 |
21 | public class MockApplication extends Application implements TrackerProvider {
22 | @Override
23 | public Tracker getByName(String trackerName) {
24 | return mTracker;
25 | }
26 | }
27 |
28 | Tracker mTracker;
29 |
30 | public GAletteTest() {
31 | super(TestActivity.class);
32 | }
33 |
34 | @Override
35 | public void setUp() throws Exception {
36 | super.setUp();
37 | ensureDexmakerCacheDir();
38 | mTracker = mock(Tracker.class);
39 | setApplication(new MockApplication());
40 | }
41 |
42 | private void ensureDexmakerCacheDir() {
43 | File cacheDir = getInstrumentation().getTargetContext()
44 | .getCacheDir();
45 | System.setProperty("dexmaker.dexcache", cacheDir.toString());
46 | }
47 |
48 | public void testSendScreenView() throws Exception {
49 | startActivity(new Intent(Intent.ACTION_MAIN), null, null);
50 | verify(mTracker).setScreenName("screenName");
51 | verify(mTracker).send(new HitBuilders.AppViewBuilder().build());
52 | }
53 |
54 | public void testSendScreenViewWithBuilders() throws Exception {
55 | TestActivity testActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
56 | testActivity.screenNameWithBuilders();
57 |
58 | InOrder inOrder = Mockito.inOrder(mTracker, mTracker);
59 |
60 | inOrder.verify(mTracker).setScreenName("screenName");
61 | inOrder.verify(mTracker).send(new HitBuilders.AppViewBuilder().build());
62 |
63 | inOrder.verify(mTracker).setScreenName("screenNameBuilder");
64 | inOrder.verify(mTracker).send(new HitBuilders.AppViewBuilder().build());
65 | }
66 |
67 | public void testSendEvent() throws Exception {
68 | TestActivity testActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
69 | testActivity.onButtonClick();
70 | verify(mTracker).send(new HitBuilders.EventBuilder().setCategory("category").setAction("action").build());
71 | }
72 |
73 | public void testSendEventWithOptionalFields() throws Exception {
74 | TestActivity testActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
75 | testActivity.onButtonClickWithOptionalFields();
76 | verify(mTracker).send(new HitBuilders.EventBuilder().setCategory("category").setAction("action").setLabel("label").setValue(1L).build());
77 | }
78 |
79 | public void testSendEventWithBuilders() throws Exception {
80 | TestActivity testActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
81 | testActivity.onButtonClickWithBuilders();
82 | verify(mTracker).send(new HitBuilders.EventBuilder().setCategory("categoryBuilder").setAction("actionBuilder").setLabel("labelBuilder").setValue(Long.MAX_VALUE).build());
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/ScreenViewBuilderDelegate.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette;
2 |
3 | import com.google.android.gms.analytics.HitBuilders;
4 | import com.google.android.gms.analytics.ecommerce.Product;
5 | import com.google.android.gms.analytics.ecommerce.ProductAction;
6 | import com.google.android.gms.analytics.ecommerce.Promotion;
7 |
8 | import java.util.Map;
9 |
10 | class ScreenViewBuilderDelegate implements HitInterceptor.ScreenViewFacade {
11 |
12 | private final HitBuilders.ScreenViewBuilder builder;
13 |
14 | public ScreenViewBuilderDelegate(HitBuilders.ScreenViewBuilder builder) {
15 | this.builder = builder;
16 | }
17 |
18 | /**
19 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setCustomDimension(int, String)
20 | */
21 | @Override
22 | public HitInterceptor.ScreenViewFacade setCustomDimension(int index, String dimension) {
23 | builder.setCustomDimension(index, dimension);
24 | return this;
25 | }
26 |
27 | /**
28 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setAll(Map)
29 | */
30 | @Override
31 | public HitInterceptor.ScreenViewFacade setAll(Map params) {
32 | builder.setAll(params);
33 | return this;
34 | }
35 |
36 | /**
37 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setPromotionAction(String)
38 | */
39 | @Override
40 | public HitInterceptor.ScreenViewFacade setPromotionAction(String action) {
41 | builder.setPromotionAction(action);
42 | return this;
43 | }
44 |
45 | /**
46 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setProductAction(ProductAction)
47 | */
48 | @Override
49 | public HitInterceptor.ScreenViewFacade setProductAction(ProductAction action) {
50 | builder.setProductAction(action);
51 | return this;
52 | }
53 |
54 | /**
55 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setNewSession()
56 | */
57 | @Override
58 | public HitInterceptor.ScreenViewFacade setNewSession() {
59 | builder.setNewSession();
60 | return this;
61 | }
62 |
63 | /**
64 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#addPromotion(Promotion)
65 | */
66 | @Override
67 | public HitInterceptor.ScreenViewFacade addPromotion(Promotion promotion) {
68 | builder.addPromotion(promotion);
69 | return this;
70 | }
71 |
72 | /**
73 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setCustomMetric(int, float)
74 | */
75 | @Override
76 | public HitInterceptor.ScreenViewFacade setCustomMetric(int index, float metric) {
77 | builder.setCustomMetric(index, metric);
78 | return this;
79 | }
80 |
81 | /**
82 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setCampaignParamsFromUrl(String)
83 | */
84 | @Override
85 | public HitInterceptor.ScreenViewFacade setCampaignParamsFromUrl(String utmParams) {
86 | builder.setCampaignParamsFromUrl(utmParams);
87 | return this;
88 | }
89 |
90 | /**
91 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#set(String, String)
92 | */
93 | @Override
94 | public HitInterceptor.ScreenViewFacade set(String paramName, String paramValue) {
95 | builder.set(paramName, paramValue);
96 | return this;
97 | }
98 |
99 | /**
100 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#addProduct(Product)
101 | */
102 | @Override
103 | public HitInterceptor.ScreenViewFacade addProduct(Product product) {
104 | builder.addProduct(product);
105 | return this;
106 | }
107 |
108 | /**
109 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#setNonInteraction(boolean)
110 | */
111 | @Override
112 | public HitInterceptor.ScreenViewFacade setNonInteraction(boolean nonInteraction) {
113 | builder.setNonInteraction(nonInteraction);
114 | return this;
115 | }
116 |
117 | /**
118 | * @see com.google.android.gms.analytics.HitBuilders.HitBuilder#addImpression(Product, String)
119 | */
120 | @Override
121 | public HitInterceptor.ScreenViewFacade addImpression(Product product, String impressionList) {
122 | builder.addImpression(product, impressionList);
123 | return this;
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/plugin/src/main/groovy/com/uphyca/galette/GAlettePlugin.groovy:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette
2 |
3 | import org.gradle.api.GradleException
4 | import com.android.build.gradle.AppPlugin
5 | import com.android.build.gradle.LibraryPlugin
6 | import org.gradle.api.Plugin
7 | import org.gradle.api.Project
8 | import org.gradle.api.tasks.compile.JavaCompile
9 |
10 | class GAlettePlugin implements Plugin {
11 |
12 | @Override
13 | void apply(Project project) {
14 | final def variants
15 | def plugin
16 | if (project.plugins.hasPlugin(AppPlugin)) {
17 | variants = project.android.applicationVariants
18 | plugin = project.plugins.getPlugin(AppPlugin)
19 | } else if (project.plugins.hasPlugin(LibraryPlugin)) {
20 | variants = project.android.libraryVariants
21 | plugin = project.plugins.getPlugin(LibraryPlugin)
22 | } else {
23 | throw new GradleException("The 'android' or 'android-library' plugin is required.")
24 | }
25 |
26 | project.configurations.create('galette')
27 |
28 | def analyticsVersion = '9.2.1'
29 | def galetteVersion = getPluginVersion(project, "com.uphyca.galette", "galette-plugin")
30 | def agpVersion = getPluginVersion(project, "com.android.tools.build", "gradle")
31 | def agpMajorVersion = Integer.parseInt(agpVersion.split("\\.")[0])
32 |
33 | if (5 <= agpMajorVersion) {
34 | project.dependencies {
35 | implementation "com.google.android.gms:play-services-analytics:${analyticsVersion}"
36 | implementation "com.uphyca.galette:galette:${galetteVersion}"
37 | galette "com.uphyca.galette:galette-instrumentation:${galetteVersion}"
38 | }
39 | } else {
40 | // 以前のバージョンとの互換のため compile を使用
41 | project.dependencies {
42 | compile "com.google.android.gms:play-services-analytics:${analyticsVersion}"
43 | compile "com.uphyca.galette:galette:${galetteVersion}"
44 | galette "com.uphyca.galette:galette-instrumentation:${galetteVersion}"
45 | }
46 | }
47 |
48 | variants.all { variant ->
49 | JavaCompile javaCompile
50 | if (variant.hasProperty('javaCompileProvider')) {
51 | javaCompile = variant.javaCompileProvider.get()
52 | } else if (variant.hasProperty('javaCompiler')) {
53 | javaCompile = variant.javaCompiler
54 | } else {
55 | javaCompile = variant.javaCompile
56 | }
57 | javaCompile.doLast {
58 | def classpath = project.files()
59 |
60 | if (project.android.properties['bootClasspath']) {
61 | // 0.10
62 | project.android.bootClasspath.each { classpath += project.files(it) }
63 | } else if (plugin.properties['runtimeJarList']) {
64 | // 0.9
65 | plugin.runtimeJarList.each { classpath += project.files(it) }
66 | }
67 | classpath += javaCompile.classpath
68 | classpath += project.configurations["galette"]
69 | classpath += project.files(javaCompile.destinationDir)
70 |
71 | def uris = classpath.collect {
72 | it.toURI().toURL()
73 | }
74 |
75 | def loader = new URLClassLoader(uris as URL[], (ClassLoader) null)
76 | def instClass = loader.loadClass("com.uphyca.galette.GAletteInstrumentation")
77 | def inst = instClass.newInstance()
78 | inst.processFiles(javaCompile.destinationDir)
79 | }
80 | }
81 | }
82 |
83 | String getPluginVersion(Project project, String group, String name) {
84 | def Project targetProject = project
85 | while (targetProject != null) {
86 | def version
87 | targetProject.buildscript.configurations.classpath.resolvedConfiguration.firstLevelModuleDependencies.each {
88 | e ->
89 | if (e.moduleGroup.equals(group) && e.moduleName.equals(name)) {
90 | version = e.moduleVersion
91 | }
92 | }
93 | if (version != null) {
94 | return version
95 | }
96 | targetProject = targetProject.parent
97 | }
98 | return null
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/EventBuilderDelegate.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette;
2 |
3 | import com.google.android.gms.analytics.HitBuilders;
4 | import com.google.android.gms.analytics.ecommerce.Product;
5 | import com.google.android.gms.analytics.ecommerce.ProductAction;
6 | import com.google.android.gms.analytics.ecommerce.Promotion;
7 |
8 | import java.util.Map;
9 |
10 | class EventBuilderDelegate implements HitInterceptor.EventFacade {
11 |
12 | private final HitBuilders.EventBuilder builder;
13 |
14 | public EventBuilderDelegate(HitBuilders.EventBuilder builder) {
15 | this.builder = builder;
16 | }
17 |
18 | /**
19 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setCategory(String)
20 | */
21 | @Override
22 | public HitInterceptor.EventFacade setCategory(String category) {
23 | builder.setCategory(category);
24 | return this;
25 | }
26 |
27 | /**
28 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setLabel(String)
29 | */
30 | @Override
31 | public HitInterceptor.EventFacade setLabel(String label) {
32 | builder.setLabel(label);
33 | return this;
34 | }
35 |
36 | /**
37 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setCustomDimension(int, String)
38 | */
39 | @Override
40 | public HitInterceptor.EventFacade setCustomDimension(int index, String dimension) {
41 | builder.setCustomDimension(index, dimension);
42 | return this;
43 | }
44 |
45 | /**
46 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setAll(Map)
47 | */
48 | @Override
49 | public HitInterceptor.EventFacade setAll(Map params) {
50 | builder.setAll(params);
51 | return this;
52 | }
53 |
54 | /**
55 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setPromotionAction(String)
56 | */
57 | @Override
58 | public HitInterceptor.EventFacade setPromotionAction(String action) {
59 | builder.setPromotionAction(action);
60 | return this;
61 | }
62 |
63 | /**
64 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setProductAction(ProductAction)
65 | */
66 | @Override
67 | public HitInterceptor.EventFacade setProductAction(ProductAction action) {
68 | builder.setProductAction(action);
69 | return this;
70 | }
71 |
72 | /**
73 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setValue(long)
74 | */
75 | @Override
76 | public HitInterceptor.EventFacade setValue(long value) {
77 | builder.setValue(value);
78 | return this;
79 | }
80 |
81 | /**
82 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setNewSession()
83 | */
84 | @Override
85 | public HitInterceptor.EventFacade setNewSession() {
86 | builder.setNewSession();
87 | return this;
88 | }
89 |
90 | /**
91 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#addPromotion(Promotion)
92 | */
93 | @Override
94 | public HitInterceptor.EventFacade addPromotion(Promotion promotion) {
95 | builder.addPromotion(promotion);
96 | return this;
97 | }
98 |
99 | /**
100 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setCustomMetric(int, float)
101 | */
102 | @Override
103 | public HitInterceptor.EventFacade setCustomMetric(int index, float metric) {
104 | builder.setCustomMetric(index, metric);
105 | return this;
106 | }
107 |
108 | /**
109 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setAction(String)
110 | */
111 | @Override
112 | public HitInterceptor.EventFacade setAction(String action) {
113 | builder.setAction(action);
114 | return this;
115 | }
116 |
117 | /**
118 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setCampaignParamsFromUrl(String)
119 | */
120 | @Override
121 | public HitInterceptor.EventFacade setCampaignParamsFromUrl(String utmParams) {
122 | builder.setCampaignParamsFromUrl(utmParams);
123 | return this;
124 | }
125 |
126 | /**
127 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#set(String, String)
128 | */
129 | @Override
130 | public HitInterceptor.EventFacade set(String paramName, String paramValue) {
131 | builder.set(paramName, paramValue);
132 | return this;
133 | }
134 |
135 | /**
136 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#addProduct(Product)
137 | */
138 | @Override
139 | public HitInterceptor.EventFacade addProduct(Product product) {
140 | builder.addProduct(product);
141 | return this;
142 | }
143 |
144 | /**
145 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#setNonInteraction(boolean)
146 | */
147 | @Override
148 | public HitInterceptor.EventFacade setNonInteraction(boolean nonInteraction) {
149 | builder.setNonInteraction(nonInteraction);
150 | return this;
151 | }
152 |
153 | /**
154 | * @see com.google.android.gms.analytics.HitBuilders.EventBuilder#addImpression(Product, String)
155 | */
156 | @Override
157 | public HitInterceptor.EventFacade addImpression(Product product, String impressionList) {
158 | builder.addImpression(product, impressionList);
159 | return this;
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/example/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/tests/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## DEPRECATED
2 | The plugin only works for com.android.tools.build:gradle:1.x
3 |
4 | [](https://app.wercker.com/project/bykey/da37f33da36130c5cda62f25542f0640)
5 |
6 |
7 | # GAlette
8 |
9 | Annotation-triggered tracking along with Google Analytics for Android.
10 |
11 |
12 | ```java
13 | @SendEvent(category = "HelloWorld", action = "sayHello", label="%1$s")
14 | String sayHello (String name) {
15 | return format("Hello, %s.", name);
16 | }
17 | ```
18 |
19 | # Before you begin
20 |
21 | Before using GAlette, make sure you have done following instructions described in https://developers.google.com/analytics/devguides/collection/android/v4/
22 | - Before you begin
23 | - Updating AndroidManifest.xml
24 | - Initialize Trackers
25 | - Create a configuration XML file
26 |
27 |
28 | # Getting Started
29 |
30 |
31 | Add the GAlette plugin to your `buildscript`'s `dependencies` section and apply the plugin:
32 | ```groovy
33 | buildscript {
34 | repositories {
35 | ...
36 | mavenCentral()
37 | }
38 | dependencies {
39 | ...
40 | classpath 'com.uphyca.galette:galette-plugin:0.9.16'
41 | }
42 | }
43 |
44 | ...
45 | apply plugin: 'com.uphyca.galette'
46 | ```
47 |
48 |
49 | Implements TrackerProvider to your Application class
50 | ```java
51 | public class MyApplication extends Application implements TrackerProvider {
52 |
53 | private Tracker mTracker;
54 |
55 | @Override
56 | public void onCreate() {
57 | super.onCreate();
58 | GoogleAnalytics ga = GoogleAnalytics.getInstance(this);
59 | mTracker = ga.newTracker(R.xml.your_tracker_resource);
60 | }
61 |
62 | @Override
63 | public Tracker getByName(String trackerName) {
64 | return mTracker;
65 | }
66 | }
67 | ```
68 |
69 | Declare your application in AndroidManifest.xml.
70 |
71 | ```xml
72 |
73 |
75 | ...
76 |
77 |
79 |
80 | ...
81 |
82 |
83 |
84 | ```
85 |
86 | ## Send Screen View
87 |
88 | Annotate @SendScreenView to your methods to track appView
89 |
90 | ```java
91 | public class MainActivity extends Activity {
92 |
93 | @Override
94 | @SendScreenView(screenName = "main")
95 | protected void onCreate(Bundle savedInstanceState) {
96 | ...
97 | }
98 | }
99 | ```
100 |
101 | ## Measuring Events
102 |
103 | Annotate @SendEvent to your methods to track event
104 |
105 | ```java
106 | public class MainActivity extends Activity {
107 |
108 | @Override
109 | protected void onCreate(Bundle savedInstanceState) {
110 | ...
111 |
112 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
113 | @Override
114 | @SendEvent(category = "button", action = "click")
115 | public void onClick(View v) {
116 | onButtonClicked();
117 | }
118 | });
119 | }
120 |
121 | }
122 | ```
123 |
124 | ## Advanced Usage
125 |
126 | ### String Templates
127 |
128 | Use string template to apply method parameters
129 |
130 | ```java
131 | public class MainActivity extends Activity {
132 |
133 | private int mClickCount;
134 |
135 | @Override
136 | protected void onCreate(Bundle savedInstanceState) {
137 | ...
138 |
139 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
140 | @Override
141 | public void onClick(View v) {
142 | onButtonClicked(++mClickCount);
143 | }
144 | });
145 | }
146 |
147 | @SendEvent(category = "button", action = "click", label = "times", value = "%1$d")
148 | private void onButtonClicked(int count) {
149 | ...
150 | }
151 | }
152 | ```
153 |
154 | ### Customizing field values
155 |
156 | Use FieldBuilder to build each field value
157 |
158 | ```java
159 | public class MainActivity extends Activity {
160 |
161 | @Override
162 | @SendScreenView(screenName = "foo", screenNameBuilder = BundleValueFieldBuilder.class)
163 | protected void onCreate(Bundle savedInstanceState) {
164 | ...
165 | }
166 |
167 | public static class BundleValueFieldBuilder implements FieldBuilder {
168 | @Override
169 | public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
170 | // annotation's screenName value paths to fieldValue, in this case 'foo'
171 | return ((Bundle) arguments[0]).getString(fieldValue);
172 | }
173 | }
174 | }
175 | ```
176 |
177 |
178 | ### Interceptor
179 |
180 | Use HitInterceptor to send arbitrary values
181 |
182 | ```java
183 | public class MyApplication extends Application implements TrackerProvider, HitInterceptor.Provider {
184 |
185 | private Tracker mTracker;
186 |
187 | private HitInterceptor hitInterceptor = new HitInterceptor() {
188 | @Override
189 | public void onEvent(EventFacade eventFacade) {
190 | eventFacade.setCustomDimension(1, Build.MODEL);
191 | }
192 |
193 | @Override
194 | public void onScreenView(ScreenViewFacade screenViewFacade) {
195 | screenViewFacade.setCustomDimension(1, Build.MODEL);
196 | }
197 | };
198 |
199 | @Override
200 | public void onCreate() {
201 | super.onCreate();
202 | GoogleAnalytics ga = GoogleAnalytics.getInstance(this);
203 | ga.setLocalDispatchPeriod(1);
204 | mTracker = ga.newTracker("SET-YOUR-TRACKING-ID");
205 | }
206 |
207 | @Override
208 | public Tracker getByName(String trackerName) {
209 | return mTracker;
210 | }
211 |
212 | @Override
213 | public HitInterceptor getHitInterceptor(String trackerName) {
214 | return hitInterceptor;
215 | }
216 | }
217 | ```
218 |
219 |
220 | ### Supported types
221 |
222 | Following types or these subclasses are supported.
223 |
224 | - Application
225 | - Activity
226 | - Service
227 | - Fragment (android.app or android.support.v4.app)
228 | - View
229 | - Context
230 |
231 | or implements ContextProvider to orbitary classes.
232 |
233 | ```Java
234 | public class MyClass implements ContextProvider {
235 |
236 | private Context mContext;
237 |
238 | public MyClass(Context context) {
239 | mContext = context;
240 | }
241 |
242 | @Override
243 | public Context get() {
244 | return mContext;
245 | }
246 |
247 | @SendScreenView(screenName = "my-class")
248 | void foo() {
249 | }
250 | }
251 | ```
252 |
253 |
254 | ## Specify Google Play Services version
255 |
256 | ```
257 | configurations.all {
258 | resolutionStrategy {
259 | force "com.google.android.gms:play-services-analytics:${playservicesVersion}"
260 | }
261 | }
262 | ```
263 |
264 | See more details [ResolutionStrategy - Gradle DSL Version 2.8](https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html)
265 |
266 |
267 | ## Proguard
268 |
269 | Proguard configuration is automatically applied.
270 |
271 | # License
272 |
273 | Copyright 2014-2021 uPhyca Inc.
274 |
275 | Licensed under the Apache License, Version 2.0 (the "License");
276 | you may not use this file except in compliance with the License.
277 | You may obtain a copy of the License at
278 |
279 | http://www.apache.org/licenses/LICENSE-2.0
280 |
281 | Unless required by applicable law or agreed to in writing, software
282 | distributed under the License is distributed on an "AS IS" BASIS,
283 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
284 | See the License for the specific language governing permissions and
285 | limitations under the License.
286 |
--------------------------------------------------------------------------------
/library/src/main/java/com/uphyca/galette/GAlette.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import android.content.Context;
20 | import android.text.TextUtils;
21 | import android.util.Log;
22 | import com.google.android.gms.analytics.HitBuilders;
23 | import com.google.android.gms.analytics.Tracker;
24 |
25 | import java.lang.annotation.ElementType;
26 | import java.lang.annotation.Retention;
27 | import java.lang.annotation.Target;
28 | import java.lang.reflect.Method;
29 | import java.util.HashMap;
30 | import java.util.Map;
31 |
32 | import static java.lang.annotation.RetentionPolicy.CLASS;
33 |
34 | public class GAlette {
35 |
36 | private static GAlette INSTANCE = new GAlette();
37 |
38 | static GAlette getInsntance() {
39 | return INSTANCE;
40 | }
41 |
42 | public static void sendScreenView(Object target, Context appContext, Method method, Object[] arguments) {
43 | getInsntance().sendScreenView0(target, appContext, method, arguments);
44 | }
45 |
46 | public static void sendAppView(Object target, Context appContext, Method method, Object[] arguments) {
47 | getInsntance().sendAppView0(target, appContext, method, arguments);
48 | }
49 |
50 | public static void sendEvent(Object target, Context appContext, Method method, Object[] arguments) {
51 | getInsntance().sendEvent0(target, appContext, method, arguments);
52 | }
53 |
54 | private void sendScreenView0(Object target, Context appContext, Method method, Object[] arguments) {
55 | final SendScreenView analyticsAnnotation = method.getAnnotation(SendScreenView.class);
56 | final Tracker tracker = trackerFrom(appContext, analyticsAnnotation.trackerName());
57 | if (tracker == null) {
58 | return;
59 | }
60 |
61 | final HitBuilders.ScreenViewBuilder builder = new HitBuilders.ScreenViewBuilder();
62 | try {
63 | final FieldBuilder screenNameBuilder = createStringFieldBuilder(analyticsAnnotation.screenNameBuilder());
64 | final String screenName = screenNameBuilder.build(Fields.SCREEN_NAME, analyticsAnnotation.screenName(), target, method, arguments);
65 | tracker.setScreenName(screenName);
66 | HitInterceptor hitInterceptor = hitInterceptorFrom(appContext, analyticsAnnotation.trackerName());
67 | hitInterceptor.onScreenView(new ScreenViewBuilderDelegate(builder));
68 | } finally {
69 | tracker.send(builder.build());
70 | }
71 | }
72 |
73 | private void sendAppView0(Object target, Context appContext, Method method, Object[] arguments) {
74 | final SendAppView analyticsAnnotation = method.getAnnotation(SendAppView.class);
75 | final Tracker tracker = trackerFrom(appContext, analyticsAnnotation.trackerName());
76 | if (tracker == null) {
77 | return;
78 | }
79 |
80 | final HitBuilders.ScreenViewBuilder builder = new HitBuilders.ScreenViewBuilder();
81 | try {
82 | final FieldBuilder screenNameBuilder = createStringFieldBuilder(analyticsAnnotation.screenNameBuilder());
83 | final String screenName = screenNameBuilder.build(Fields.SCREEN_NAME, analyticsAnnotation.screenName(), target, method, arguments);
84 | tracker.setScreenName(screenName);
85 | HitInterceptor hitInterceptor = hitInterceptorFrom(appContext, analyticsAnnotation.trackerName());
86 | hitInterceptor.onScreenView(new ScreenViewBuilderDelegate(builder));
87 | } finally {
88 | tracker.send(builder.build());
89 | }
90 | }
91 |
92 | private void sendEvent0(Object target, Context appContext, Method method, Object[] arguments) {
93 | final SendEvent analyticsAnnotation = method.getAnnotation(SendEvent.class);
94 | final Tracker tracker = trackerFrom(appContext, analyticsAnnotation.trackerName());
95 | if (tracker == null) {
96 | return;
97 | }
98 |
99 | final HitBuilders.EventBuilder builder = new HitBuilders.EventBuilder();
100 | try {
101 | final FieldBuilder categoryBuilder = createStringFieldBuilder(analyticsAnnotation.categoryBuilder());
102 | final String category = categoryBuilder.build(Fields.CATEGORY, analyticsAnnotation.category(), target, method, arguments);
103 | builder.setCategory(category);
104 |
105 | final FieldBuilder actionBuilder = createStringFieldBuilder(analyticsAnnotation.actionBuilder());
106 | final String action = actionBuilder.build(Fields.ACTION, analyticsAnnotation.action(), target, method, arguments);
107 | builder.setAction(action);
108 |
109 | final FieldBuilder labelBuilder = createStringFieldBuilder(analyticsAnnotation.labelBuilder());
110 | final String label = labelBuilder.build(Fields.LABEL, analyticsAnnotation.label(), target, method, arguments);
111 | if (!TextUtils.isEmpty(label)) {
112 | builder.setLabel(label);
113 | }
114 |
115 | final FieldBuilder valueBuilder = createLongFieldBuilder(analyticsAnnotation.valueBuilder());
116 | final Long value = valueBuilder.build(Fields.VALUE, analyticsAnnotation.value(), target, method, arguments);
117 | if (value != null) {
118 | builder.setValue(value);
119 | }
120 | HitInterceptor hitInterceptor = hitInterceptorFrom(appContext, analyticsAnnotation.trackerName());
121 | hitInterceptor.onEvent(new EventBuilderDelegate(builder));
122 | } finally {
123 | tracker.send(builder.build());
124 | }
125 | }
126 |
127 | private static final FieldBuilder DEFAULT_STRING_FIELD_BUILDER = new StringFieldBuilder() {
128 | @Override
129 | public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
130 | if (arguments == null || arguments.length == 0) {
131 | return fieldValue;
132 | }
133 | return String.format(fieldValue, arguments);
134 | }
135 | };
136 |
137 | private static final FieldBuilder DEFAULT_LONG_FIELD_BUILDER = new LongFieldBuilder() {
138 | @Override
139 | public Long build(Fields fields, Long fieldValue, Object declaredObject, Method method, Object[] arguments) {
140 | return fieldValue.longValue() > Long.MIN_VALUE ? fieldValue : null;
141 | }
142 | };
143 |
144 | private final Map>, FieldBuilder> mFieldBuilderMap = new HashMap>, FieldBuilder>();
145 |
146 | private FieldBuilder createStringFieldBuilder(Class extends FieldBuilder> fieldBuilderClass) {
147 | if (fieldBuilderClass == StringFieldBuilder.class) {
148 | return DEFAULT_STRING_FIELD_BUILDER;
149 | }
150 | return createFieldBuilder(fieldBuilderClass);
151 | }
152 |
153 | private FieldBuilder createLongFieldBuilder(Class extends FieldBuilder> fieldBuilderClass) {
154 | if (fieldBuilderClass == LongFieldBuilder.class) {
155 | return DEFAULT_LONG_FIELD_BUILDER;
156 | }
157 | return createFieldBuilder(fieldBuilderClass);
158 | }
159 |
160 | private synchronized FieldBuilder createFieldBuilder(Class extends FieldBuilder> fieldBuilderClass) {
161 | if (!mFieldBuilderMap.containsKey(fieldBuilderClass)) {
162 | try {
163 | FieldBuilder fieldBuilder = fieldBuilderClass.newInstance();
164 | mFieldBuilderMap.put(fieldBuilderClass, fieldBuilder);
165 | } catch (InstantiationException e) {
166 | throw new IllegalArgumentException("Not found no-argument public constructor in " + fieldBuilderClass.getName(), e);
167 | } catch (IllegalAccessException e) {
168 | throw new IllegalArgumentException("Not found no-argument public constructor in " + fieldBuilderClass.getName(), e);
169 | }
170 | }
171 | return mFieldBuilderMap.get(fieldBuilderClass);
172 | }
173 |
174 | private static Tracker trackerFrom(Context appContext, String trackerName) {
175 | try {
176 | return ((TrackerProvider) appContext).getByName(!TextUtils.isEmpty(trackerName) ? trackerName : null);
177 | } catch (ClassCastException e) {
178 | Log.w("GAlette", "Application must be a type of " + "com.uphyca.galette.TrackerProvider");
179 | }
180 | return null;
181 | }
182 |
183 | private static HitInterceptor hitInterceptorFrom(Context appContext, String trackerName) {
184 | if(appContext instanceof HitInterceptor.Provider) {
185 | return ((HitInterceptor.Provider) appContext).getHitInterceptor(trackerName);
186 | }
187 | if(appContext instanceof HitInterceptor) {
188 | return (HitInterceptor) appContext;
189 | }
190 | return EMPTY_HIT_INTERCEPTOR;
191 | }
192 |
193 | @Target(ElementType.TYPE)
194 | @Retention(CLASS)
195 | private @interface Baked {
196 | }
197 |
198 | private static final HitInterceptor EMPTY_HIT_INTERCEPTOR = new HitInterceptor() {
199 | @Override
200 | public void onEvent(EventFacade event) {
201 | }
202 |
203 | @Override
204 | public void onScreenView(ScreenViewFacade screenView) {
205 | }
206 | };
207 | }
208 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/instrumentation/src/test/java/com/uphyca/galette/GAletteInstrumentationTest.java:
--------------------------------------------------------------------------------
1 | package com.uphyca.galette;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.app.Fragment;
6 | import android.app.Service;
7 | import android.content.Context;
8 | import android.content.ContextWrapper;
9 | import android.support.v4.app.FragmentActivity;
10 | import android.view.View;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.objectweb.asm.MethodVisitor;
14 |
15 | import java.io.File;
16 | import java.lang.reflect.Method;
17 | import java.net.URL;
18 | import java.net.URLClassLoader;
19 |
20 | import static org.mockito.Matchers.eq;
21 | import static org.mockito.Mockito.mock;
22 | import static org.mockito.Mockito.verify;
23 |
24 | public class GAletteInstrumentationTest {
25 |
26 | GAlette.IGAlette galette;
27 |
28 | @Before
29 | public void setUp() throws Exception {
30 | galette = mock(GAlette.IGAlette.class);
31 | GAlette.setIGAlette(galette);
32 | }
33 |
34 | private URL getJarFile(URL classPath) throws Exception {
35 | String classFile = classPath.getFile();
36 | return new URL(classFile.substring(0, classFile.indexOf('!')));
37 | }
38 |
39 | private URL getClassesDir(Class> clazz) throws Exception {
40 | String classFile = clazz.getResource(clazz.getSimpleName() + ".class").getFile();
41 | return new URL("file://" + classFile.substring(0, classFile.indexOf(clazz.getName().replace('.', '/'))));
42 | }
43 |
44 | private ClassLoader getIsolatedClassLoader() throws Exception {
45 | final URL asmJar = getJarFile(MethodVisitor.class.getResource("ClassReader.class"));
46 | return new URLClassLoader(new URL[]{asmJar, getClassesDir(GAletteInstrumentation.class), getClassesDir(getClass())}, Class.class.getClassLoader());
47 | }
48 |
49 | private File getClassFile(String className) {
50 | return new File(getClass().getResource("/" + className.replace('.', '/') + ".class").getFile());
51 | }
52 |
53 | private Class> processClass(String className) throws Exception {
54 | final ClassLoader classLoader = getIsolatedClassLoader();
55 | Class> inst = classLoader.loadClass(GAletteInstrumentation.class.getName());
56 | Method processFile = inst.getMethod("processFile", new Class[]{File.class});
57 | processFile.invoke(inst.newInstance(), new Object[]{getClassFile(className)});
58 | return classLoader.loadClass(className);
59 | }
60 |
61 | @Test
62 | public void weaveSendAppView() throws Exception {
63 | // Process classes.
64 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestClassForSendAppView");
65 |
66 | // Load instrumented classes.
67 | TestClassForSendAppView instrumentedObject = new TestClassForSendAppView();
68 | Application app = new Application();
69 | instrumentedObject.attach(app);
70 |
71 | // Invoke method.
72 | instrumentedObject.show();
73 |
74 | // Verify interactions.
75 | Method method = instrumentedObject.getClass().getDeclaredMethod("show", new Class[]{});
76 | verify(galette).sendAppView(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
77 | }
78 |
79 | static class TestClassForSendAppView extends Activity {
80 | @SendAppView
81 | void show() {
82 | }
83 | }
84 |
85 | @Test
86 | public void weaveSendScreenView() throws Exception {
87 | // Process classes.
88 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestClassForSendScreenView");
89 |
90 | // Load instrumented classes.
91 | TestClassForSendScreenView instrumentedObject = new TestClassForSendScreenView();
92 | Application app = new Application();
93 | instrumentedObject.attach(app);
94 |
95 | // Invoke method.
96 | instrumentedObject.show();
97 |
98 | // Verify interactions.
99 | Method method = instrumentedObject.getClass().getDeclaredMethod("show", new Class[]{});
100 | verify(galette).sendScreenView(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
101 | }
102 |
103 | static class TestClassForSendScreenView extends Activity {
104 | @SendScreenView
105 | void show() {
106 | }
107 | }
108 |
109 |
110 | @Test
111 | public void weaveSendEvent() throws Exception {
112 | // Process classes.
113 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestClassForSendEvent");
114 |
115 | // Load instrumented classes.
116 | TestClassForSendEvent instrumentedObject = new TestClassForSendEvent();
117 | Application app = new Application();
118 | instrumentedObject.attach(app);
119 |
120 | // Invoke method.
121 | instrumentedObject.click();
122 |
123 | // Verify interactions.
124 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
125 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
126 | }
127 |
128 | static class TestClassForSendEvent extends Activity {
129 | @SendEvent
130 | void click() {
131 | }
132 | }
133 |
134 | @Test
135 | public void weaveService() throws Exception {
136 | // Process classes.
137 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestServiceClass");
138 |
139 | // Load instrumented classes.
140 | TestServiceClass instrumentedObject = new TestServiceClass();
141 | Application app = new Application();
142 | instrumentedObject.setApplication(app);
143 |
144 | // Invoke method.
145 | instrumentedObject.click();
146 |
147 | // Verify interactions.
148 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
149 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
150 | }
151 |
152 | static class TestServiceClass extends Service {
153 | @SendEvent
154 | void click() {
155 | }
156 | }
157 |
158 | @Test
159 | public void weaveApplication() throws Exception {
160 | // Process classes.
161 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestApplicationClass");
162 |
163 | // Load instrumented classes.
164 | TestApplicationClass instrumentedObject = new TestApplicationClass();
165 |
166 | // Invoke method.
167 | instrumentedObject.click();
168 |
169 | // Verify interactions.
170 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
171 | verify(galette).sendEvent(eq(instrumentedObject), eq(instrumentedObject), eq(method), eq(new Object[]{}));
172 | }
173 |
174 | static class TestApplicationClass extends Application {
175 | @SendEvent
176 | void click() {
177 | }
178 | }
179 |
180 | @Test
181 | public void weaveView() throws Exception {
182 | // Process classes.
183 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestViewClass");
184 |
185 | // Load instrumented classes.
186 | final Application app = new Application();
187 | Context context = new ContextWrapper(app){
188 | @Override
189 | public Context getApplicationContext() {
190 | return app;
191 | }
192 | };
193 | TestViewClass instrumentedObject = new TestViewClass(context);
194 |
195 | // Invoke method.
196 | instrumentedObject.click();
197 |
198 | // Verify interactions.
199 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
200 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
201 | }
202 |
203 | static class TestViewClass extends View {
204 | TestViewClass(Context context) {
205 | super(context);
206 | }
207 |
208 | @SendEvent
209 | void click() {
210 | }
211 | }
212 |
213 | @Test
214 | public void weaveContextProvider() throws Exception {
215 | // Process classes.
216 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestContextProviderClass");
217 |
218 | // Load instrumented classes.
219 | final Application app = new Application();
220 | app.attach(new ContextWrapper(app) {
221 | @Override
222 | public Context getApplicationContext() {
223 | return app;
224 | }
225 | });
226 | TestContextProviderClass instrumentedObject = new TestContextProviderClass(app);
227 |
228 | // Invoke method.
229 | instrumentedObject.click();
230 |
231 | // Verify interactions.
232 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
233 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
234 | }
235 |
236 | static class TestContextProviderClass implements ContextProvider {
237 |
238 |
239 | private Context mContext;
240 |
241 | TestContextProviderClass(Context context) {
242 | mContext = context;
243 | }
244 |
245 | @SendEvent
246 | void click() {
247 | }
248 |
249 | @Override
250 | public Context get() {
251 | return mContext;
252 | }
253 | }
254 |
255 | @Test
256 | public void weaveContext() throws Exception {
257 | // Process classes.
258 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestContextClass");
259 |
260 | // Load instrumented classes.
261 | final Application app = new Application();
262 | app.attach(new ContextWrapper(app) {
263 | @Override
264 | public Context getApplicationContext() {
265 | return app;
266 | }
267 | });
268 | TestContextClass instrumentedObject = new TestContextClass(app);
269 |
270 | // Invoke method.
271 | instrumentedObject.click();
272 |
273 | // Verify interactions.
274 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
275 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
276 | }
277 |
278 | static class TestContextClass extends ContextWrapper {
279 |
280 | TestContextClass(Context context) {
281 | super(context);
282 | }
283 |
284 | @SendEvent
285 | void click() {
286 | }
287 | }
288 |
289 | @Test
290 | public void weaveFragment() throws Exception {
291 | // Process classes.
292 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestFragmentClass");
293 |
294 | // Load instrumented classes.
295 | TestFragmentClass instrumentedObject = new TestFragmentClass();
296 | Application app = new Application();
297 | Activity activity = new Activity();
298 | activity.attach(app);
299 | instrumentedObject.setActivity(activity);
300 |
301 | // Invoke method.
302 | instrumentedObject.click();
303 |
304 | // Verify interactions.
305 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
306 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
307 | }
308 |
309 | static class TestFragmentClass extends Fragment {
310 |
311 | @SendEvent
312 | void click() {
313 | }
314 | }
315 |
316 | @Test
317 | public void weaveSupportFragment() throws Exception {
318 | // Process classes.
319 | processClass("com.uphyca.galette.GAletteInstrumentationTest$TestSupportFragmentClass");
320 |
321 | // Load instrumented classes.
322 | TestSupportFragmentClass instrumentedObject = new TestSupportFragmentClass();
323 | Application app = new Application();
324 | FragmentActivity activity = new FragmentActivity();
325 | activity.attach(app);
326 | instrumentedObject.setActivity(activity);
327 |
328 | // Invoke method.
329 | instrumentedObject.click();
330 |
331 | // Verify interactions.
332 | Method method = instrumentedObject.getClass().getDeclaredMethod("click", new Class[]{});
333 | verify(galette).sendEvent(eq(instrumentedObject), eq(app), eq(method), eq(new Object[]{}));
334 | }
335 |
336 | static class TestSupportFragmentClass extends android.support.v4.app.Fragment {
337 |
338 | @SendEvent
339 | void click() {
340 | }
341 | }
342 |
343 | @Test
344 | public void weaveNestedClass() throws Exception {
345 | // Process classes.
346 | processClass("com.uphyca.galette.GAletteInstrumentationTest$NestedTestClass$InnerClass");
347 |
348 | // Load instrumented classes.
349 | NestedTestClass instrumentedObject = new NestedTestClass();
350 | Application app = new Application();
351 | instrumentedObject.attach(app);
352 |
353 | // Invoke method.
354 | instrumentedObject.click();
355 |
356 | // Verify interactions.
357 | Method method = instrumentedObject.getClass().getDeclaredClasses()[0].getDeclaredMethod("click", new Class[]{});
358 | verify(galette).sendEvent(eq(instrumentedObject.mInner), eq(app), eq(method), eq(new Object[]{}));
359 | }
360 |
361 | static class NestedTestClass extends Activity {
362 |
363 | class InnerClass {
364 |
365 | @SendEvent
366 | void click() {
367 |
368 | }
369 |
370 | class InnerInnerClass {
371 | @SendEvent
372 | void click() {
373 | }
374 | }
375 | }
376 |
377 | InnerClass mInner = new InnerClass();
378 |
379 | void click() {
380 | mInner.click();
381 | }
382 | }
383 |
384 | @Test(expected = Exception.class)
385 | public void weaveNestedNestedClass() throws Exception {
386 | // Process classes.
387 | processClass("com.uphyca.galette.GAletteInstrumentationTest$NestedNestedTestClass$InnerClass$InnerInnerClass");
388 |
389 | // // Load instrumented classes.
390 | // NestedNestedTestClass instrumentedObject = new NestedNestedTestClass();
391 | // Application app = new Application();
392 | // instrumentedObject.attach(app);
393 | //
394 | // // Invoke method.
395 | // instrumentedObject.click();
396 | //
397 | // // Verify interactions.
398 | // Method method = instrumentedObject.getClass().getDeclaredClasses()[0].getDeclaredClasses()[0].getDeclaredMethod("click", new Class[]{});
399 | // verify(galette).sendEvent(eq(instrumentedObject.mInner.mInnerInner), eq(app), eq(method), eq(new Object[]{}));
400 | }
401 |
402 | static class NestedNestedTestClass extends Activity {
403 |
404 | class InnerClass {
405 |
406 | InnerInnerClass mInnerInner = new InnerInnerClass();
407 |
408 | void click() {
409 | mInnerInner.click();
410 | }
411 |
412 | class InnerInnerClass {
413 | @SendEvent
414 | void click() {
415 | }
416 | }
417 | }
418 |
419 | InnerClass mInner = new InnerClass();
420 |
421 | void click() {
422 | mInner.click();
423 | }
424 | }
425 | }
426 |
--------------------------------------------------------------------------------
/instrumentation/src/main/java/com/uphyca/galette/GAletteInstrumentation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 uPhyca Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.uphyca.galette;
18 |
19 | import org.objectweb.asm.*;
20 | import org.objectweb.asm.commons.AdviceAdapter;
21 | import org.objectweb.asm.util.CheckClassAdapter;
22 |
23 | import java.io.*;
24 |
25 | public class GAletteInstrumentation {
26 |
27 | static class ModifierClassWriter extends ClassVisitor {
28 |
29 | private String name;
30 | private String syntheticFieldName;
31 | private String syntheticFieldType;
32 |
33 | private final boolean[] proceed = new boolean[1];
34 |
35 | ModifierClassWriter(int api, ClassVisitor cv) {
36 | super(api, cv);
37 | }
38 |
39 | /**
40 | * Returns true if the class transformed.
41 | */
42 | boolean isProceed() {
43 | return proceed[0];
44 | }
45 |
46 | @Override
47 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
48 | super.visit(version, access, name, signature, superName, interfaces);
49 | this.name = name;
50 | }
51 |
52 | @Override
53 | public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
54 | MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
55 | ModifierMethodWriter mvw = new ModifierMethodWriter(api, this.name, mv, access, name, desc, proceed, syntheticFieldName, syntheticFieldType);
56 | return mvw;
57 | }
58 |
59 | @Override
60 | public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
61 | if ((access & Opcodes.ACC_SYNTHETIC) == Opcodes.ACC_SYNTHETIC && (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL) {
62 | syntheticFieldName = name;
63 | syntheticFieldType = desc;
64 | }
65 | return super.visitField(access, name, desc, signature, value);
66 | }
67 |
68 | @Override
69 | public void visitEnd() {
70 | if (proceed[0]) {
71 | visitAnnotation("Lcom/uphyca/galette/GAlette$Baked;", false);
72 | }
73 | super.visitEnd();
74 | }
75 | }
76 |
77 |
78 | public static class ModifierMethodWriter extends AdviceAdapter {
79 |
80 | private final String className;
81 | private final String syntheticFieldName;
82 | private final String syntheticFieldType;
83 | private final String methodName;
84 | private final Type[] argumentTypes;
85 |
86 | ModifierMethodWriter(int api, String className, MethodVisitor mv, int access, String methodName, String desc, boolean[] proceed, String syntheticFieldName, String syntheticFieldType) {
87 | super(api, mv, access, methodName, desc);
88 | this.className = className;
89 | this.methodName = methodName;
90 | this.argumentTypes = Type.getArgumentTypes(desc);
91 | this.proceed = proceed;
92 | this.syntheticFieldName = syntheticFieldName;
93 | this.syntheticFieldType = syntheticFieldType;
94 | }
95 |
96 | private final boolean[] proceed;
97 | private String trackingMethodName;
98 |
99 | @Override
100 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
101 | AnnotationVisitor av = super.visitAnnotation(desc, visible);
102 | if (desc.equals("Lcom/uphyca/galette/SendAppView;")) {
103 | trackingMethodName = "sendAppView";
104 | } else if (desc.equals("Lcom/uphyca/galette/SendScreenView;")) {
105 | trackingMethodName = "sendScreenView";
106 | } else if (desc.equals("Lcom/uphyca/galette/SendEvent;")) {
107 | trackingMethodName = "sendEvent";
108 | }
109 | return av;
110 | }
111 |
112 | @Override
113 | protected void onMethodEnter() {
114 | if (trackingMethodName == null) {
115 | super.onMethodEnter();
116 | return;
117 | }
118 |
119 | proceed[0] = true;
120 |
121 | // The local variable to store argument types
122 | final int argumentTypesVariableId = newLocal(Type.getObjectType("[Ljava/lang/Class;"));
123 | // The local variable to store method
124 | final int methodVariableId = newLocal(Type.getObjectType("java/lang/reflect/Method"));
125 | // The local variable to store argument values
126 | final int argumentValuesVariableId = newLocal(Type.getObjectType("[Ljava/lang/Object;"));
127 | // The local variable to store context
128 | final int contextVariableId = newLocal(Type.getObjectType("[Landroid/content/Context;"));
129 |
130 | // Allocate the array to store argument values.
131 | // e.g. Object[] argumentValues = new Object[arguments.length]
132 | pushInt(argumentTypes.length);
133 | newArray(Type.getObjectType("java/lang/Object"));
134 | storeLocal(argumentValuesVariableId);
135 |
136 | // Store each argument values to the array
137 | // e.g. argumentValues[index] = arguments[index]
138 | for (int i = 0, size = argumentTypes.length; i < size; ++i) {
139 | loadLocal(argumentValuesVariableId);
140 | pushInt(i);
141 | loadArg(i);
142 | box(argumentTypes[i]);
143 | arrayStore(Type.getObjectType("java/lang/Object"));
144 | }
145 |
146 | // Allocate the array to store argument types
147 | // e.g. Class[] argumentTypes = new Class[arguments.length]
148 | pushInt(argumentTypes.length);
149 | newArray(Type.getObjectType("java/lang/Class"));
150 | storeLocal(argumentTypesVariableId);
151 |
152 | // Store each argument types to the array
153 | // e.g. argumentTypes[index] = arguments[index].getClass()
154 | for (int i = 0, size = argumentTypes.length; i < size; ++i) {
155 | loadLocal(argumentTypesVariableId);
156 | pushInt(i);
157 | pushClassObject(argumentTypes[i]);
158 | arrayStore(Type.getObjectType("java/lang/Class"));
159 | }
160 |
161 | // Get the method
162 | // e.g. Method method = Foo.class.getDeclaredMethod(methodName, argumentTypes)
163 | visitLdcInsn(Type.getObjectType(className));
164 | visitLdcInsn(methodName);
165 | loadLocal(argumentTypesVariableId);
166 | visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
167 | storeLocal(methodVariableId);
168 |
169 | // Invoke the tracking method
170 | // e.g. GAlette.sendScreenView(owner, method, argumentValues)
171 | try {
172 | final Class> type = Class.forName(className.replace('/', '.'));
173 | Instruction fallbackInstruction = new Instruction() {
174 | @Override
175 | void proceed() throws ClassNotFoundException {
176 | if (syntheticFieldType != null) {
177 | final Class> enclosingType = Class.forName(syntheticFieldType.substring(1, syntheticFieldType.length() - 1).replace('/', '.'));
178 | pushApplication(enclosingType, new Instruction() {
179 | @Override
180 | void proceed() throws ClassNotFoundException {
181 | loadThis();
182 | visitFieldInsn(GETFIELD, className, syntheticFieldName, syntheticFieldType);
183 | }
184 | }, new Instruction() {
185 | @Override
186 | void proceed() throws ClassNotFoundException {
187 | throw new IllegalStateException("Failed to get context from " + enclosingType.getName());
188 | }
189 | });
190 | } else {
191 | throw new IllegalStateException("Failed to get context from " + type.getName());
192 | }
193 | }
194 | };
195 | pushApplication(type, new Instruction() {
196 | @Override
197 | void proceed() throws ClassNotFoundException {
198 | loadThis();
199 | }
200 | }, fallbackInstruction);
201 | visitTypeInsn(CHECKCAST, "android/content/Context");
202 | storeLocal(contextVariableId);
203 | } catch (ClassNotFoundException e) {
204 | throw new IllegalStateException(e);
205 | }
206 | loadThis();
207 | loadLocal(contextVariableId);
208 | loadLocal(methodVariableId);
209 | loadLocal(argumentValuesVariableId);
210 | visitMethodInsn(Opcodes.INVOKESTATIC, "com/uphyca/galette/GAlette", trackingMethodName, "(Ljava/lang/Object;Landroid/content/Context;Ljava/lang/reflect/Method;[Ljava/lang/Object;)V", false);
211 |
212 | visitMaxs(0, 0);
213 | super.onMethodEnter();
214 | }
215 |
216 | private void pushApplication(Class> type, Instruction op, Instruction defaultOp) throws ClassNotFoundException {
217 | if (Class.forName("com.uphyca.galette.ContextProvider").isAssignableFrom(type)) {
218 | op.proceed();
219 | visitTypeInsn(CHECKCAST, "com/uphyca/galette/ContextProvider");
220 | visitMethodInsn(INVOKEINTERFACE, "com/uphyca/galette/ContextProvider", "get", "()Landroid/content/Context;", true);
221 | visitMethodInsn(INVOKEVIRTUAL, "android/content/Context", "getApplicationContext", "()Landroid/content/Context;", false);
222 | } else if (Class.forName("android.app.Application").isAssignableFrom(type)) {
223 | op.proceed();
224 | } else if (Class.forName("android.app.Activity").isAssignableFrom(type)) {
225 | op.proceed();
226 | visitTypeInsn(CHECKCAST, "android/app/Activity");
227 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/app/Activity", "getApplication", "()Landroid/app/Application;", false);
228 | } else if (Class.forName("android.app.Service").isAssignableFrom(type)) {
229 | op.proceed();
230 | visitTypeInsn(CHECKCAST, "android/app/Service");
231 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/app/Service", "getApplication", "()Landroid/app/Application;", false);
232 | } else if (Class.forName("android.view.View").isAssignableFrom(type)) {
233 | op.proceed();
234 | visitTypeInsn(CHECKCAST, "android/view/View");
235 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/view/View", "getContext", "()Landroid/content/Context;", false);
236 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/content/Context", "getApplicationContext", "()Landroid/content/Context;", false);
237 | } else if (Class.forName("android.app.Fragment").isAssignableFrom(type)) {
238 | op.proceed();
239 | visitTypeInsn(CHECKCAST, "android/app/Fragment");
240 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/app/Fragment", "getActivity", "()Landroid/app/Activity;", false);
241 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/app/Activity", "getApplication", "()Landroid/app/Application;", false);
242 | } else if (Class.forName("android.support.v4.app.Fragment").isAssignableFrom(type)) {
243 | op.proceed();
244 | visitTypeInsn(CHECKCAST, "android/support/v4/app/Fragment");
245 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/support/v4/app/Fragment", "getActivity", "()Landroid/support/v4/app/FragmentActivity;", false);
246 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/app/Activity", "getApplication", "()Landroid/app/Application;", false);
247 | } else if (Class.forName("android.content.Context").isAssignableFrom(type)) {
248 | op.proceed();
249 | visitTypeInsn(CHECKCAST, "android/content/Context");
250 | visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/content/Context", "getApplicationContext", "()Landroid/content/Context;", false);
251 | } else {
252 | defaultOp.proceed();
253 | }
254 | }
255 |
256 | static abstract class Instruction {
257 | abstract void proceed() throws ClassNotFoundException;
258 | }
259 |
260 | /**
261 | * Push type to stack
262 | */
263 | private void pushClassObject(Type type) {
264 | switch (type.getSort()) {
265 | case Type.BOOLEAN:
266 | visitTypeField("java/lang/Boolean");
267 | break;
268 | case Type.BYTE:
269 | visitTypeField("java/lang/Byte");
270 | break;
271 | case Type.CHAR:
272 | visitTypeField("java/lang/Character");
273 | break;
274 | case Type.SHORT:
275 | visitTypeField("java/lang/Short");
276 | break;
277 | case Type.INT:
278 | visitTypeField("java/lang/Integer");
279 | break;
280 | case Type.FLOAT:
281 | visitTypeField("java/lang/Float");
282 | break;
283 | case Type.LONG:
284 | visitTypeField("java/lang/Long");
285 | break;
286 | case Type.DOUBLE:
287 | visitTypeField("java/lang/Double");
288 | break;
289 | default:
290 | visitLdcInsn(type);
291 | break;
292 | }
293 | }
294 |
295 | /**
296 | * Visit TYPE field
297 | */
298 | private void visitTypeField(String internalClassName) {
299 | visitFieldInsn(GETSTATIC, internalClassName, "TYPE", "Ljava/lang/Class;");
300 | }
301 |
302 | /**
303 | * Push integer to stack
304 | */
305 | private void pushInt(int i) {
306 | if (i <= 5) {
307 | visitInsn(Opcodes.ICONST_0 + i);
308 | } else {
309 | visitIntInsn(Opcodes.BIPUSH, i);
310 | }
311 | }
312 | }
313 |
314 | static class BakedAnnotationReader extends ClassVisitor {
315 |
316 | private boolean weaved;
317 |
318 | BakedAnnotationReader(int api) {
319 | super(api);
320 | }
321 |
322 | @Override
323 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
324 | if (desc.equals("Lcom/uphyca/galette/GAlette$Baked;")) {
325 | weaved = true;
326 | visitEnd();
327 | }
328 | return super.visitAnnotation(desc, visible);
329 | }
330 | }
331 |
332 | /**
333 | * Process the file
334 | */
335 | public void processFile(File classFile) throws Exception {
336 | if (!classFile.isFile()) {
337 | throw new IllegalArgumentException(classFile + " is not a file");
338 | }
339 | InputStream in = null;
340 | try {
341 | in = new FileInputStream(classFile);
342 | final ClassReader classReader = new ClassReader(in);
343 | if (!hadWeaved(classReader)) {
344 | weaveIfNecessary(classReader, classFile);
345 | }
346 | } finally {
347 | closeQuietly(in);
348 | }
349 | }
350 |
351 | /**
352 | * Weave tracking method call if the annotations present.
353 | */
354 | private static void weaveIfNecessary(ClassReader classReader, File dest) throws IOException {
355 | ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
356 | ModifierClassWriter mcw = new ModifierClassWriter(Opcodes.ASM5, cw);
357 | classReader.accept(new CheckClassAdapter(mcw), ClassReader.EXPAND_FRAMES);
358 | if (!mcw.isProceed()) {
359 | return;
360 | }
361 |
362 | OutputStream out = null;
363 | try {
364 | out = new FileOutputStream(dest);
365 | out.write(cw.toByteArray());
366 | out.flush();
367 | } finally {
368 | closeQuietly(out);
369 | }
370 | }
371 |
372 | /**
373 | * Returns true if the class had weaved.
374 | */
375 | private static boolean hadWeaved(ClassReader classReader) {
376 | BakedAnnotationReader annotationReadClassVisitor = new BakedAnnotationReader(Opcodes.ASM5);
377 | classReader.accept(annotationReadClassVisitor, ClassReader.SKIP_CODE);
378 | return annotationReadClassVisitor.weaved;
379 | }
380 |
381 | /**
382 | * Close the resource without IOException
383 | */
384 | private static void closeQuietly(Closeable res) {
385 | if (res == null) {
386 | return;
387 | }
388 | try {
389 | res.close();
390 | } catch (IOException ignore) {
391 | }
392 | }
393 |
394 | public void processFiles(File fileOrDirectory) throws Exception {
395 | if (fileOrDirectory.isFile()) {
396 | if (fileOrDirectory.getName().endsWith(".class")) {
397 | processFile(fileOrDirectory);
398 | }
399 | } else {
400 | for (File each : fileOrDirectory.listFiles()) {
401 | processFiles(each);
402 | }
403 | }
404 | }
405 |
406 | public static void main(String[] args) throws Exception {
407 | final GAletteInstrumentation inst = new GAletteInstrumentation();
408 | File f = new File(args[0]);
409 | if (!f.exists()) {
410 | throw new IllegalArgumentException(f + " does not exist");
411 | }
412 | inst.processFiles(f);
413 | }
414 | }
415 |
--------------------------------------------------------------------------------