/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.facebook.buck.ide.intellij.lang.android;

import com.facebook.buck.android.AndroidLibraryDescription;
import com.facebook.buck.ide.intellij.ModuleBuildContext;
import com.facebook.buck.ide.intellij.lang.java.JavaLibraryRuleHelper;
import com.facebook.buck.ide.intellij.model.IjModuleAndroidFacet;
import com.facebook.buck.ide.intellij.model.IjModuleFactoryResolver;
import com.facebook.buck.ide.intellij.model.IjModuleType;
import com.facebook.buck.ide.intellij.model.IjProjectConfig;
import com.facebook.buck.io.ProjectFilesystem;
import com.facebook.buck.rules.Description;
import com.facebook.buck.rules.TargetNode;
import java.nio.file.Path;
import java.util.Optional;

public class AndroidLibraryModuleRule extends AndroidModuleRule<AndroidLibraryDescription.Arg> {

  public AndroidLibraryModuleRule(
      ProjectFilesystem projectFilesystem,
      IjModuleFactoryResolver moduleFactoryResolver,
      IjProjectConfig projectConfig) {
    super(projectFilesystem, moduleFactoryResolver, projectConfig, true);
  }

  @Override
  public Class<? extends Description<?>> getDescriptionClass() {
    return AndroidLibraryDescription.class;
  }

  @Override
  public void apply(
      TargetNode<AndroidLibraryDescription.Arg, ?> target, ModuleBuildContext context) {
    super.apply(target, context);
    addDepsAndSources(target, true /* wantsPackagePrefix */, context);
    JavaLibraryRuleHelper.addCompiledShadowIfNeeded(projectConfig, target, context);
    Optional<Path> dummyRDotJavaClassPath = moduleFactoryResolver.getDummyRDotJavaPath(target);
    if (dummyRDotJavaClassPath.isPresent()) {
      context.addExtraClassPathDependency(dummyRDotJavaClassPath.get());
    }

    IjModuleAndroidFacet.Builder builder = context.getOrCreateAndroidFacetBuilder();
    Optional<Path> manifestPath = moduleFactoryResolver.getLibraryAndroidManifestPath(target);
    if (manifestPath.isPresent()) {
      builder.setManifestPath(manifestPath.get());
    }
  }

  @Override
  public IjModuleType detectModuleType(TargetNode<AndroidLibraryDescription.Arg, ?> targetNode) {
    return IjModuleType.ANDROID_MODULE;
  }
}

@@ -771,16 +771,153 @@
 ontext;%0A
+import com.facebook.buck.ide.intellij.aggregation.AggregationContext;%0Aimport com.facebook.buck.ide.intellij.aggregation.AggregationKeys;%0A
 import c
@@ -975,24 +975,24 @@
 RuleHelper;%0A
-
 import com.f
@@ -1367,16 +1367,17 @@
 etNode;%0A
+%0A
 import j
@@ -2430,16 +2430,114 @@
 %0A    %7D%0A%0A
+    context.setJavaLanguageLevel(JavaLibraryRuleHelper.getLanguageLevel(projectConfig, target));%0A%0A
     IjMo
@@ -2740,24 +2740,24 @@
 resent()) %7B%0A
-
       builde
@@ -2802,24 +2802,498 @@
 %0A    %7D%0A  %7D%0A%0A
+  @Override%0A  public void applyDuringAggregation(%0A      AggregationContext context, TargetNode%3CAndroidLibraryDescription.Arg, ?%3E targetNode) %7B%0A    super.applyDuringAggregation(context, targetNode);%0A%0A    Optional%3CString%3E languageLevel = JavaLibraryRuleHelper.getLanguageLevel(%0A        projectConfig,%0A        targetNode);%0A    if (languageLevel.isPresent()) %7B%0A      context.addAggregationKey(%0A          AggregationKeys.JAVA_LANGUAGE_LEVEL,%0A          languageLevel);%0A    %7D%0A  %7D%0A%0A
   @Override%0A

Fix language level for android library modules in intellij.