// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.packageinfo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** PackageInfoPlugin */
public class PackageInfoPlugin implements MethodCallHandler {
  private final Context context;

  /** Plugin registration. */
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel =
        new MethodChannel(registrar.messenger(), "plugins.flutter.io/package_info");
    channel.setMethodCallHandler(new PackageInfoPlugin(registrar.activity()));
  }

  private PackageInfoPlugin(Activity activity) {
    this.context = activity;
  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    try {
      PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
      switch (call.method) {
        case "getVersion":
          result.success(info.versionName);
          break;
        case "getBuildNumber":
          result.success(String.valueOf(info.versionCode));
          break;
        default:
          result.notImplemented();
      }
    } catch (PackageManager.NameNotFoundException ex) {
      result.error("Name not found", ex.getMessage(), null);
    }
  }
}

@@ -204,37 +204,8 @@
 o;%0A%0A
-import android.app.Activity;%0A
 impo
@@ -974,24 +974,23 @@
 gistrar.
-activity
+context
 ()));%0A  
@@ -1024,25 +1024,23 @@
 gin(
-Activity activity
+Context context
 ) %7B%0A
@@ -1062,16 +1062,15 @@
 t = 
-activity
+context
 ;%0A  

Work on removiing foreground Activity assumption (#304)