Class: AppInfo::APK

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/app_info/apk.rb

Overview

Parse APK file

Defined Under Namespace

Modules: Device Classes: Certificate, Sign

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ APK

Returns a new instance of APK.



23
24
25
# File 'lib/app_info/apk.rb', line 23

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/app_info/apk.rb', line 13

def file
  @file
end

Instance Method Details

#activitiesObject



104
105
106
# File 'lib/app_info/apk.rb', line 104

def activities
  components.select { |c| c.type == 'activity' }
end

#apkObject



112
113
114
115
116
# File 'lib/app_info/apk.rb', line 112

def apk
  Zip.warn_invalid_date = false # fix invaild date format warnings

  @apk ||= ::Android::Apk.new(@file)
end

#certificatesObject



98
99
100
101
102
# File 'lib/app_info/apk.rb', line 98

def certificates
  apk.certificates.each_with_object([]) do |(path, certificate), obj|
    obj << Certificate.new(path, certificate)
  end
end

#clear!Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/app_info/apk.rb', line 138

def clear!
  return unless @contents

  FileUtils.rm_rf(@contents)

  @apk = nil
  @contents = nil
  @icons = nil
  @app_path = nil
  @info = nil
end

#contentsObject



150
151
152
# File 'lib/app_info/apk.rb', line 150

def contents
  @contents ||= File.join(Dir.mktmpdir, "AppInfo-android-#{SecureRandom.hex}")
end

#device_typeObject



54
55
56
57
58
59
60
61
62
# File 'lib/app_info/apk.rb', line 54

def device_type
  if wear?
    Device::WATCH
  elsif tv?
    Device::TV
  else
    Device::PHONE
  end
end

#iconsObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/app_info/apk.rb', line 118

def icons
  unless @icons
    @icons = apk.icon.each_with_object([]) do |(path, data), obj|
      icon_name = File.basename(path)
      icon_path = File.join(contents, File.dirname(path))
      icon_file = File.join(icon_path, icon_name)
      FileUtils.mkdir_p icon_path
      File.open(icon_file, 'wb') { |f| f.write(data) }

      obj << {
        name: icon_name,
        file: icon_file,
        dimensions: ImageSize.path(icon_file).size
      }
    end
  end

  @icons
end

#min_sdk_versionObject



77
78
79
# File 'lib/app_info/apk.rb', line 77

def min_sdk_version
  manifest.min_sdk_ver
end

#nameObject



50
51
52
# File 'lib/app_info/apk.rb', line 50

def name
  resource.find('@string/app_name')
end

#osObject Also known as: file_type



31
32
33
# File 'lib/app_info/apk.rb', line 31

def os
  AppInfo::Platform::ANDROID
end

#servicesObject



108
109
110
# File 'lib/app_info/apk.rb', line 108

def services
  components.select { |c| c.type == 'service' }
end

#signsObject



92
93
94
95
96
# File 'lib/app_info/apk.rb', line 92

def signs
  apk.signs.each_with_object([]) do |(path, sign), obj|
    obj << Sign.new(path, sign)
  end
end

#size(humanable = false) ⇒ Object



27
28
29
# File 'lib/app_info/apk.rb', line 27

def size(humanable = false)
  AppInfo::Util.file_size(@file, humanable)
end

#target_sdk_versionObject



81
82
83
84
85
86
# File 'lib/app_info/apk.rb', line 81

def target_sdk_version
  manifest.doc
          .elements['/manifest/uses-sdk']
          .attributes['targetSdkVersion']
          .to_i
end

#tv?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/app_info/apk.rb', line 73

def tv?
  use_features.include?('android.software.leanback')
end

#use_featuresObject



88
89
90
# File 'lib/app_info/apk.rb', line 88

def use_features
  manifest_values('/manifest/uses-feature')
end

#version_codeObject Also known as: build_version



45
46
47
# File 'lib/app_info/apk.rb', line 45

def version_code
  manifest.version_code.to_s
end

#wear?Boolean

TODO: find a way to detect def tablet?

resource

end

Returns:

  • (Boolean)


69
70
71
# File 'lib/app_info/apk.rb', line 69

def wear?
  use_features.include?('android.hardware.type.watch')
end