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