Skip to content

Commit

Permalink
Merge pull request #4 from Unloc/master
Browse files Browse the repository at this point in the history
Add getNumberType
  • Loading branch information
emostar authored Dec 17, 2019
2 parents 312fc90 + 955a8f5 commit 408311a
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 1 deletion.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ android {
dependencies {
api 'com.googlecode.libphonenumber:libphonenumber:8.10.2'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void onMethodCall(MethodCall call, Result result) {
case "getRegionInfo":
handleGetRegionInfo(call, result);
break;
case "getNumberType":
handleGetNumberType(call, result);
case "formatAsYouType":
formatAsYouType(call, result);
break;
Expand Down Expand Up @@ -92,6 +94,57 @@ private void handleGetRegionInfo(MethodCall call, Result result) {
}
}

private void handleGetNumberType(MethodCall call, Result result) {
final String phoneNumber = call.argument("phone_number");
final String isoCode = call.argument("iso_code");

try {
Phonenumber.PhoneNumber p = phoneUtil.parse(phoneNumber, isoCode.toUpperCase());
PhoneNumberUtil.PhoneNumberType t = phoneUtil.getNumberType(p);

switch (t) {
case FIXED_LINE:
result.success(0);
break;
case MOBILE:
result.success(1);
break;
case FIXED_LINE_OR_MOBILE:
result.success(2);
break;
case TOLL_FREE:
result.success(3);
break;
case PREMIUM_RATE:
result.success(4);
break;
case SHARED_COST:
result.success(5);
break;
case VOIP:
result.success(6);
break;
case PERSONAL_NUMBER:
result.success(7);
break;
case PAGER:
result.success(8);
break;
case UAN:
result.success(9);
break;
case VOICEMAIL:
result.success(10);
break;
case UNKNOWN:
result.success(-1);
break;
}
} catch (NumberParseException e) {
result.error("NumberParseException", e.getMessage(), null);
}
}

private void formatAsYouType(MethodCall call, Result result) {
final String phoneNumber = call.argument("phone_number");
final String isoCode = call.argument("iso_code");
Expand Down
1 change: 1 addition & 0 deletions example/ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions example/ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
69 changes: 69 additions & 0 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def parse_KV_file(file, separator='=')
file_abs_path = File.expand_path(file)
if !File.exists? file_abs_path
return [];
end
pods_ary = []
skip_line_start_symbols = ["#", "/"]
File.foreach(file_abs_path) { |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
pods_ary.push({:name => podname, :path => podpath});
else
puts "Invalid plugin specification: #{line}"
end
}
return pods_ary
end

target 'Runner' do
# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
# referring to absolute paths on developers' machines.
system('rm -rf .symlinks')
system('mkdir -p .symlinks/plugins')

# Flutter Pods
generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig')
if generated_xcode_build_settings.empty?
puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first."
end
generated_xcode_build_settings.map { |p|
if p[:name] == 'FLUTTER_FRAMEWORK_DIR'
symlink = File.join('.symlinks', 'flutter')
File.symlink(File.dirname(p[:path]), symlink)
pod 'Flutter', :path => File.join(symlink, File.basename(p[:path]))
end
}

# Plugin Pods
plugin_pods = parse_KV_file('../.flutter-plugins')
plugin_pods.map { |p|
symlink = File.join('.symlinks', 'plugins', p[:name])
File.symlink(p[:path], symlink)
pod p[:name], :path => File.join(symlink, 'ios')
}
end

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
3 changes: 3 additions & 0 deletions ios/Classes/LibphonenumberPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
@"regionCode": countryCode == nil ? @"" : [countryCode stringValue],
@"formattedPhoneNumber": formattedNumber == nil ? @"" : formattedNumber,
});
} else if ([@"getNumberType" isEqualToString:call.method]) {
NSNumber *numberType = [NSNumber numberWithInteger:[self.phoneUtil getNumberType:number]];
result(numberType);
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
29 changes: 29 additions & 0 deletions lib/libphonenumber.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ class RegionInfo {
}
}

enum PhoneNumberType {
fixedLine,
mobile,
fixedLineOrMobile,
tollFree,
premiumRate,
sharedCost,
voip,
personalNumber,
pager,
uan,
voicemail,
unknown
}

class PhoneNumberUtil {
static const MethodChannel _channel = const MethodChannel('codeheadlabs.com/libphonenumber');

Expand Down Expand Up @@ -55,6 +70,20 @@ class PhoneNumberUtil {
);
}

static Future<PhoneNumberType> getNumberType({
@required String phoneNumber,
@required String isoCode,
}) async {
int result = await _channel.invokeMethod('getNumberType', {
'phone_number': phoneNumber,
'iso_code': isoCode,
});
if (result == -1) {
return PhoneNumberType.unknown;
}
return PhoneNumberType.values[result];
}

static Future<String> formatAsYouType({
@required String phoneNumber,
@required String isoCode,
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ homepage: https://github.com/emostar/flutter-libphonenumber
dependencies:
flutter:
sdk: flutter
built_value:

flutter:
plugin:
Expand Down

0 comments on commit 408311a

Please sign in to comment.