import exynos 7570 bsp
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos7580.git] / mobicore / TuiService / src / com / trustonic / tuiservice / TuiTlcWrapper.java
1 /*
2 * Copyright (c) 2013-2015 TRUSTONIC LIMITED
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 package com.trustonic.tuiservice;
33
34 import java.util.LinkedList;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 import com.trustonic.tuiservice.TuiActivity.PerformActionInBackground;
38 import com.trustonic.util.tLog;
39
40 import android.content.Context;
41 import android.content.Intent;
42 import android.graphics.Bitmap;
43 import android.graphics.BitmapFactory;
44 //import android.os.Build;
45 //import android.util.DisplayMetrics;
46 import android.os.PowerManager;
47 //import android.view.Surface;
48
49 public class TuiTlcWrapper {
50 private static final String TAG = TuiTlcWrapper.class.getSimpleName();
51
52 // public static final int INIT_SESSION = 1;
53 public static final int CLOSE_SESSION = 2;
54
55 private static Context context = null;
56 private static PerformActionInBackground handler = null;
57 private static final Object sessionSignal = new Object();
58 private static final Object startSignal = new Object();
59 private static final Object finishSignal = new Object();
60 private static boolean sessionOpened = false;
61 private static AtomicBoolean isActityAlive = new AtomicBoolean();
62 private static AtomicBoolean isActivityCreated = new AtomicBoolean();
63
64 private static PowerManager pm;
65 private static PowerManager.WakeLock wl;
66
67
68 public static boolean isSessionOpened() {
69 synchronized (sessionSignal) {
70 return sessionOpened;
71 }
72 }
73 public static void setSessionOpened(boolean sessionOpened) {
74 synchronized (sessionSignal) {
75 TuiTlcWrapper.sessionOpened = sessionOpened;
76 }
77 }
78
79 public static Object getStartSignal() {
80 return startSignal;
81 }
82 public static Object getFinishSignal() {
83 return finishSignal;
84 }
85
86 public static PerformActionInBackground getHandler() {
87 return handler;
88 }
89 public static void setHandler(PerformActionInBackground handler) {
90 TuiTlcWrapper.handler = handler;
91 }
92
93 public static void setIsActityAlive(boolean status) {
94 TuiTlcWrapper.isActityAlive.set(status);
95 }
96
97 public static void setIsActivityCreated(boolean status) {
98 TuiTlcWrapper.isActivityCreated.set(status);
99 }
100
101 public static boolean startTuiSession(){
102
103 try {
104 synchronized (startSignal) {
105 /* create activities */
106 Intent myIntent = new Intent(context, TuiActivity.class);
107 myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
108 | Intent.FLAG_DEBUG_LOG_RESOLUTION
109 | Intent.FLAG_ACTIVITY_NO_ANIMATION);
110 context.startActivity(myIntent);
111 /* Wait activity created*/
112 startSignal.wait(5000);
113 if( ! TuiTlcWrapper.isActityAlive.get()) {
114 tLog.d(TAG, "ERROR ACTIVITY timout");
115 finishTuiSession();
116 return false;
117 }
118 }
119 } catch (InterruptedException e) {
120 // TODO Auto-generated catch block
121 e.printStackTrace();
122 }
123
124 /* Enable cancel events catching */
125 synchronized (sessionSignal) {
126 TuiTlcWrapper.sessionOpened = true;
127 }
128
129 return true;
130 }
131
132 public static void acquireWakeLock() {
133 /* Ensure that CPU is still running */
134 try {
135 wl.acquire();
136 } catch (Exception e1) {
137 // TODO Auto-generated catch block
138 e1.printStackTrace();
139 }
140 }
141
142 public static void finishTuiSession(){
143 tLog.d(TAG, "finishTuiSession!");
144 if(TuiTlcWrapper.isActivityCreated.get()) {
145 /* Disable cancel events catching */
146 synchronized (sessionSignal) {
147 TuiTlcWrapper.sessionOpened = false;
148 }
149
150 try{
151 synchronized (finishSignal) {
152 /* Send a message to the activity UI thread */
153 handler.sendMessage(handler.obtainMessage(CLOSE_SESSION));
154 /* Wait activity closed */
155 finishSignal.wait(5000);
156 if(TuiTlcWrapper.isActivityCreated.get()) {
157 tLog.d(TAG, "ERROR ACTIVITY timout");
158 }
159 }
160 }catch (Exception e) {
161 // TODO: handle exception
162 e.printStackTrace();
163 }
164 }
165 try {
166 if (wl.isHeld()) {
167 wl.release();
168 }
169 } catch (Exception e2) {
170 // TODO Auto-generated catch block
171 e2.printStackTrace();
172 }
173 }
174
175
176 /* Initialize static members */
177 public static void init(Context ctxt) {
178 /* Save context */
179 context = ctxt;
180
181 pm = (PowerManager) ctxt.getSystemService(Context.POWER_SERVICE);
182 wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TuiService");
183
184 /* Start the TlcTui native thread */
185 startTlcTui();
186 }
187
188 /* Native functions */
189 public static native int startTlcTui();
190 public static native boolean notifyEvent(int eventType);
191
192 /**
193 * this is used to load the library on application startup. The
194 * library has already been unpacked to the app specific folder
195 * at installation time by the package manager.
196 */
197 static {
198 System.loadLibrary("Tui");
199 }
200 }