1 /*
2  * Copyright (C) 2014 The Android Open Source Project
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.android.framework.multidexlegacytestservices;
18 
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.IBinder;
23 import androidx.multidex.MultiDex;
24 import android.util.Log;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.RandomAccessFile;
29 
30 /**
31  * Empty service for testing legacy multidex. Access more than 64k methods but some are required at
32  * init, some only at verification and others during execution.
33  */
34 public abstract class AbstractService extends Service implements Runnable {
35     private final String TAG = "MultidexLegacyTestService" + getId();
36 
37     private int instanceFieldNotInited;
38     private int instanceFieldInited =
39             new com.android.framework.multidexlegacytestservices.manymethods.Big043().get43();
40     private static int staticField =
41             new com.android.framework.multidexlegacytestservices.manymethods.Big044().get44();
42 
AbstractService()43     public AbstractService() {
44         instanceFieldNotInited = new com.android.framework.multidexlegacytestservices.manymethods.Big042().get42();
45     }
46 
47     @Override
onCreate()48     public void onCreate() {
49         Log.i(TAG, "onCreate");
50         new Thread(this).start();
51 
52     }
53 
54     @Override
run()55     public void run() {
56         Context applicationContext = getApplicationContext();
57         File resultFile = new File(applicationContext.getFilesDir(), getId());
58         try {
59             // Append a constant value in result file, if services crashed and is relaunched, size
60             // of the result file will be too big.
61             RandomAccessFile raf = new RandomAccessFile(resultFile, "rw");
62             raf.seek(raf.length());
63             if (raf.length() == 0) {
64                 Log.i(TAG, "Writing 0x42434445 at " + raf.length() + " in " + resultFile.getPath());
65                 raf.writeInt(0x42434445);
66             } else {
67                 Log.w(TAG, "Service was restarted appending 0x42434445 twice at " + raf.length()
68                         + " in " + resultFile.getPath());
69                 raf.writeInt(0x42434445);
70                 raf.writeInt(0x42434445);
71             }
72             raf.close();
73             MultiDex.install(applicationContext);
74             Log.i(TAG, "Multi dex installation done.");
75 
76             int value = getValue();
77             Log.i(TAG, "Saving the result (" + value + ") to " + resultFile.getPath());
78             // Append the check value in result file, keeping the constant values already written.
79             raf = new RandomAccessFile(resultFile, "rw");
80             raf.seek(raf.length());
81             Log.i(TAG, "Writing result at " + raf.length() + " in " + resultFile.getPath());
82             raf.writeInt(value);
83             raf.close();
84         } catch (IOException e) {
85             throw new AssertionError(e);
86         } finally {
87             try {
88                 // Writing end of processing flags, the existence of the file is the criteria
89                 RandomAccessFile raf = new RandomAccessFile(
90                         new File(applicationContext.getFilesDir(), getId() + ".complete"), "rw");
91                 Log.i(TAG, "creating complete file " + resultFile.getPath());
92                 raf.writeInt(0x32333435);
93                 raf.close();
94             } catch (IOException e) {
95                 e.printStackTrace();
96             }
97         }
98     }
99 
100     @Override
onStartCommand(Intent intent, int flags, int startId)101     public int onStartCommand(Intent intent, int flags, int startId) {
102         Log.i("Service" + getId(), "Received start id " + startId + ": " + intent);
103         // We want this service to continue running until it is explicitly
104         // stopped, so return sticky.
105         return START_STICKY;
106     }
107 
getId()108     private String getId() {
109         return this.getClass().getSimpleName();
110     }
111 
112     @Override
onDestroy()113     public void onDestroy() {
114     }
115 
116     @Override
onBind(Intent intent)117     public IBinder onBind(Intent intent) {
118         return null;
119     }
120 
getValue()121     public int getValue() {
122         int intermediate = -1;
123         try {
124             intermediate = ReflectIntermediateClass.get(45, 80, 20 /* 5 seems enough on a nakasi,
125                 using 20 to get some margin */);
126         } catch (Exception e) {
127             throw new AssertionError(e);
128         }
129         int value =
130                 new com.android.framework.multidexlegacytestservices.manymethods.Big001().get1() +
131                 new com.android.framework.multidexlegacytestservices.manymethods.Big002().get2() +
132                 new com.android.framework.multidexlegacytestservices.manymethods.Big003().get3() +
133                 new com.android.framework.multidexlegacytestservices.manymethods.Big004().get4() +
134                 new com.android.framework.multidexlegacytestservices.manymethods.Big005().get5() +
135                 new com.android.framework.multidexlegacytestservices.manymethods.Big006().get6() +
136                 new com.android.framework.multidexlegacytestservices.manymethods.Big007().get7() +
137                 new com.android.framework.multidexlegacytestservices.manymethods.Big008().get8() +
138                 new com.android.framework.multidexlegacytestservices.manymethods.Big009().get9() +
139                 new com.android.framework.multidexlegacytestservices.manymethods.Big010().get10() +
140                 new com.android.framework.multidexlegacytestservices.manymethods.Big011().get11() +
141                 new com.android.framework.multidexlegacytestservices.manymethods.Big012().get12() +
142                 new com.android.framework.multidexlegacytestservices.manymethods.Big013().get13() +
143                 new com.android.framework.multidexlegacytestservices.manymethods.Big014().get14() +
144                 new com.android.framework.multidexlegacytestservices.manymethods.Big015().get15() +
145                 new com.android.framework.multidexlegacytestservices.manymethods.Big016().get16() +
146                 new com.android.framework.multidexlegacytestservices.manymethods.Big017().get17() +
147                 new com.android.framework.multidexlegacytestservices.manymethods.Big018().get18() +
148                 new com.android.framework.multidexlegacytestservices.manymethods.Big019().get19() +
149                 new com.android.framework.multidexlegacytestservices.manymethods.Big020().get20() +
150                 new com.android.framework.multidexlegacytestservices.manymethods.Big021().get21() +
151                 new com.android.framework.multidexlegacytestservices.manymethods.Big022().get22() +
152                 new com.android.framework.multidexlegacytestservices.manymethods.Big023().get23() +
153                 new com.android.framework.multidexlegacytestservices.manymethods.Big024().get24() +
154                 new com.android.framework.multidexlegacytestservices.manymethods.Big025().get25() +
155                 new com.android.framework.multidexlegacytestservices.manymethods.Big026().get26() +
156                 new com.android.framework.multidexlegacytestservices.manymethods.Big027().get27() +
157                 new com.android.framework.multidexlegacytestservices.manymethods.Big028().get28() +
158                 new com.android.framework.multidexlegacytestservices.manymethods.Big029().get29() +
159                 new com.android.framework.multidexlegacytestservices.manymethods.Big030().get30() +
160                 new com.android.framework.multidexlegacytestservices.manymethods.Big031().get31() +
161                 new com.android.framework.multidexlegacytestservices.manymethods.Big032().get32() +
162                 new com.android.framework.multidexlegacytestservices.manymethods.Big033().get33() +
163                 new com.android.framework.multidexlegacytestservices.manymethods.Big034().get34() +
164                 new com.android.framework.multidexlegacytestservices.manymethods.Big035().get35() +
165                 new com.android.framework.multidexlegacytestservices.manymethods.Big036().get36() +
166                 new com.android.framework.multidexlegacytestservices.manymethods.Big037().get37() +
167                 new com.android.framework.multidexlegacytestservices.manymethods.Big038().get38() +
168                 new com.android.framework.multidexlegacytestservices.manymethods.Big039().get39() +
169                 new com.android.framework.multidexlegacytestservices.manymethods.Big040().get40() +
170                 new com.android.framework.multidexlegacytestservices.manymethods.Big041().get41() +
171                 instanceFieldNotInited +
172                 instanceFieldInited +
173                 staticField +
174                 intermediate;
175         return value;
176     }
177 
178 }
179