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
26
27
28
# File 'lib/app_info/apk.rb', line 23

def initialize(file)
  @file = file

  Zip.warn_invalid_date = false # fix invaild date format warnings
  @apk = ::Android::Apk.new(file)
end

Instance Attribute Details

#apkObject (readonly)

Returns the value of attribute apk.



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

def apk
  @apk
end

#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



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

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

#certificatesObject



101
102
103
104
105
# File 'lib/app_info/apk.rb', line 101

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

#device_typeObject



57
58
59
60
61
62
63
64
65
# File 'lib/app_info/apk.rb', line 57

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

#iconsObject



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

def icons
  unless @icons
    tmp_path = File.join(Dir.mktmpdir, "AppInfo-android-#{SecureRandom.hex}")

    @icons = @apk.icon.each_with_object([]) do |(path, data), obj|
      icon_name = File.basename(path)
      icon_path = File.join(tmp_path, File.dirname(path))
      icon_file = File.join(icon_path, icon_name)
      FileUtils.mkdir_p icon_path
      File.open(icon_file, 'w') do |f|
        f.write(data.force_encoding('BINARY'))
      end

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

  @icons
end

#min_sdk_versionObject



80
81
82
# File 'lib/app_info/apk.rb', line 80

def min_sdk_version
  manifest.min_sdk_ver
end

#nameObject



53
54
55
# File 'lib/app_info/apk.rb', line 53

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

#osObject Also known as: file_type



34
35
36
# File 'lib/app_info/apk.rb', line 34

def os
  AppInfo::Platform::ANDROID
end

#servicesObject



111
112
113
# File 'lib/app_info/apk.rb', line 111

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

#signsObject



95
96
97
98
99
# File 'lib/app_info/apk.rb', line 95

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

#size(humanable = false) ⇒ Object



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

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

#target_sdk_versionObject



84
85
86
87
88
89
# File 'lib/app_info/apk.rb', line 84

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

#tv?Boolean

Returns:

  • (Boolean)


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

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

#use_featuresObject



91
92
93
# File 'lib/app_info/apk.rb', line 91

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

#version_codeObject Also known as: build_version



48
49
50
# File 'lib/app_info/apk.rb', line 48

def version_code
  manifest.version_code.to_s
end

#wear?Boolean

TODO: find a way to detect def tablet?

resource

end

Returns:

  • (Boolean)


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

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