| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package robotester |
| |
| import ( |
| "archive/zip" |
| "fmt" |
| "io/ioutil" |
| "os" |
| "path/filepath" |
| |
| "android.googlesource.com/platform/tools/gpu/client/android/apk" |
| "android.googlesource.com/platform/tools/gpu/framework/id" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| "android.googlesource.com/platform/tools/gpu/tools/robotester/db" |
| ) |
| |
| // engineSignatures is used to identify the middleware engine used based on |
| // files found in the APK. |
| var engineSignatures = map[string]string{ |
| "libunity.so": "unity", |
| "libUnrealEngine3.so": "unreal3", |
| } |
| |
| // AddAPK parses the APK file, adds it to the specified directory, and returns |
| // the APK's information. |
| func AddAPK(ctx log.Context, apkData []byte, data string) (db.APK, error) { |
| files, err := apk.Read(ctx, apkData) |
| if err != nil { |
| return db.APK{}, err |
| } |
| m, err := apk.GetManifest(ctx, files) |
| if err != nil { |
| return db.APK{}, err |
| } |
| |
| id := id.OfBytes(apkData) |
| |
| activity, action, err := m.MainActivity(ctx) |
| if err != nil { |
| return db.APK{}, fmt.Errorf("Couldn't find launch activity: %v", err) |
| } |
| |
| path := fmt.Sprintf("%s/%s.apk", m.Package, id.String()) |
| fullpath := db.APKPath(data, db.APK{Path: path}) |
| if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil { |
| return db.APK{}, fmt.Errorf("Couldn't create directory '%s': %v", filepath.Dir(fullpath), err) |
| } |
| if err := ioutil.WriteFile(fullpath, apkData, 0666); err != nil { |
| return db.APK{}, fmt.Errorf("Couldn't store APK to '%s': %v", fullpath, err) |
| } |
| |
| return db.APK{ |
| ID: id, |
| Name: m.Package, // TODO |
| VersionCode: m.VersionCode, |
| VersionName: m.VersionName, |
| Path: path, |
| Package: m.Package, |
| Activity: activity, |
| Action: action, |
| Engine: engine(files), |
| ABIs: apk.GatherABIs(files), |
| }, nil |
| } |
| |
| func engine(files []*zip.File) string { |
| for _, file := range files { |
| _, name := filepath.Split(file.Name) |
| for sig, engine := range engineSignatures { |
| if name == sig { |
| return engine |
| } |
| } |
| } |
| return "<unknown>" |
| } |