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