Class: MakeIcon::SocialAction

Inherits:
Object
  • Object
show all
Defined in:
lib/makeicon.rb

Class Method Summary collapse

Class Method Details

.authorsObject



131
132
133
# File 'lib/makeicon.rb', line 131

def self.authors
  ["@adrum"]
end

.available_optionsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/makeicon.rb', line 135

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :appicon_image_file,
                            env_name: "APPICON_IMAGE_FILE",
                         description: "Path to a square image file, at least 512x512",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_devices,
                            env_name: "APPICON_DEVICES",
                       default_value: [:phone],
                         description: "Array of device types to generate icons for",
                            optional: true,
                                type: Array),
    FastlaneCore::ConfigItem.new(key: :appicon_path,
                            env_name: "APPICON_PATH",
                       default_value: 'app/res/mipmap/',
                         description: "Path to res subfolder",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_filename,
                            env_name: "APPICON_FILENAME",
                       default_value: 'ic_launcher',
                         description: "The output filename of each image",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_notification_icon_path,
                            env_name: "APPICON_NOTIFICATION_ICON_PATH",
                       default_value: 'app/res/drawable/',
                         description: "Path to res subfolder",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_notification_icon_filename,
                            env_name: "APPICON_NOTIFICATION_ICON_FILENAME",
                       default_value: 'ic_stat_onesignal_default',
                         description: "File name for notification icons",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_custom_sizes,
                         description: "Hash of custom sizes - {'path/icon.png' => '256x256'}",
                            optional: true,
                                type: Hash)
  ]
end

.descriptionObject



127
128
129
# File 'lib/makeicon.rb', line 127

def self.description
  "Generate required icon sizes from a master application icon"
end

.get_custom_sizes(image, custom_sizes) ⇒ Object



123
124
125
# File 'lib/makeicon.rb', line 123

def self.get_custom_sizes(image, custom_sizes)
  
end

.get_needed_icons(device, needed_icons, is_android = false, custom_sizes = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/makeicon.rb', line 18

def self.get_needed_icons(device, needed_icons, is_android = false, custom_sizes = {})
  icons = []
  puts device
  needed_icons[device].each do |scale, sizes|
    sizes.each do |size|
      if size.kind_of?(Array)
        size, role, subtype = size
      end

      if is_android
        width, height = size.split('x').map { |v| v.to_f }
      else
        width, height = size.split('x').map { |v| v.to_f * scale.to_i }
      end

      icons << {
        'width' => width,
        'height' => height,
        'size' => size,
        'device' => device.to_s.gsub('_', '-'),
        'scale' => scale,
        'role' => role,
        'subtype' => subtype
      }
    end
  end
  
  # Add custom icon sizes (probably for notifications) 
  custom_sizes.each do |path, size|
    path = path.to_s
    width, height = size.split('x').map { |v| v.to_f }

    icons << {
      'width' => width,
      'height' => height,
      'size' => size,
      'basepath' => File.dirname(path),
      'filename' => File.basename(path)
    }
  end
  
  # Sort from the largest to the smallest needed icon
  icons = icons.sort_by {|value| value['width']} .reverse
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/makeicon.rb', line 179

def self.is_supported?(platform)
  [:android].include?(platform)
end

.needed_iconsObject



63
64
65
66
67
68
69
70
71
# File 'lib/makeicon.rb', line 63

def self.needed_icons
  {
    social: {
      'weixin' => ['28x28','108x108'],
      'weibo' => ['16x16','80x80','120x120'],
      'qq' => ['16x16','512x512'],
    },
  }
end

.notification_icons(path, filename) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/makeicon.rb', line 73

def self.notification_icons(path, filename)
  return {} if !path || !filename
  
  {
    "#{path}-ldpi/#{filename}" => '36x36',
    "#{path}-mdpi/#{filename}" => '24x24',
    "#{path}-hdpi/#{filename}" => '36x36',
    "#{path}-xhdpi/#{filename}" => '48x48',
    "#{path}-xxhdpi/#{filename}" => '72x72',
    "#{path}-xxxhdpi/#{filename}" => '96x96',
  }
end

.run(params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/makeicon.rb', line 86

def self.run(params)
  fname = params[:appicon_image_file]

  require 'mini_magick'
  image = MiniMagick::Image.open(fname)
  fpname = Pathname.new(fname)
  basedir  = fpname.dirname

  Helper::AppiconHelper.check_input_image_size(image, 512)

  # Convert image to png
  image.format 'png'
  
  # Merge notification icons into customer sizes as they are handled thes same way

  icons = get_needed_icons(:social, self.needed_icons, true)
  icons.each do |icon|
    width = icon['width']
    height = icon['height']

    # Custom icons will have basepath and filename already defined
    if icon.has_key?('basepath') && icon.has_key?('filename')
      basepath = Pathname.new(icon['basepath'])
      filename = icon['filename']  
    else
      basepath = basedir + Pathname.new("#{params[:appicon_path]}-#{icon['scale']}")
      filename = "#{params[:appicon_filename]}_#{width}x#{height}.png"
    end
    FileUtils.mkdir_p(basepath)
    puts basepath + filename
    image.resize "#{width}x#{height}"
    image.write basepath + filename
  end

  puts "Successfully stored launcher icons at '#{params[:basepath]}'"
end