<?php
$platforms = $this->getParam('platforms');
$versions = [];

foreach($platforms['client']['sdks'] as $lang) {
    $key = $lang['key'] ?? '';
    $version = $lang['version'] ?? '';

    if(!empty($key)) {
        $versions[$key] = $version;
    }
}

$androidVersion = (isset($versions['android'])) ? $versions['android'] : '';
?>
<p>Appwrite is a development platform that provides a powerful API and management console to get your next project up and running quickly.</p>

<p><a href="https://cloud.appwrite.io/" target="_blank" rel="noopener">Create your first project now</a> and start building on Appwrite Cloud.</p>

<h2><a href="/docs/getting-started-for-android#addProject" id="addProject">Add your Android Platform</a></h2>

<p>To init your SDK and start interacting with Appwrite services, you need to add a new Android platform to your project. To add a new platform, go to your Appwrite Console, choose the project you created in the step before, and click the 'Add Platform' button. Only API requests initiated from platforms added to your Appwrite project will be accepted. This prevents unauthorized apps from accessing your Appwrite project.</p>

<p>From the options, choose to add a new <b>Android</b> platform and add add your app <u>name</u> and <u>package name</u>, your package name is generally the <b>applicationId</b> in your app-level <a href="https://github.com/appwrite/playground-for-android/blob/master/app/build.gradle#L12" target="_blank" rel="noopener">build.gradle</a> file. By registering your new app platform, you are allowing your app to communicate with the Appwrite API.</p>

<h2><a href="/docs/getting-started-for-android#getSDK" id="getSDK">Get Appwrite Android SDK</a></h2>

<p>First, add this to your root level build.gradle file: </p>
<div class="ide" data-lang="groovy" data-lang-label="Groovy">
  <pre class="line-numbers"><code class="prism language-groovy" data-prism><?php echo $this->escape('repositories {
    mavenCentral()
}');?></code></pre>
</div>

<p>Then add this to your project's build.gradle file:</p>

<div class="ide" data-lang="groovy" data-lang-label="Groovy">
  <pre class="line-numbers"><code class="prism language-groovy" data-prism>implementation("io.appwrite:sdk-for-android:<?php echo $this->escape($androidVersion); ?>")</code></pre>
</div>

<h3><a href="/docs/getting-started-for-android#OAuthCallback" id="OAuthCallback">OAuth Callback</a></h3>

<p>In order to capture the Appwrite OAuth callback url, the following activity needs to be added inside the <code>&lt;application&gt;</code> tag, along side the existing <code>&lt;activity&gt;</code> tags in your <a href="https://github.com/appwrite/playground-for-android/blob/master/app/src/main/AndroidManifest.xml" target="_blank" rel="noopener">AndroidManifest.xml</a>. Be sure to replace the <code>[PROJECT_ID]</code> string with your actual Appwrite project ID. You can find your Appwrite project ID in your project settings screen in your Appwrite Console.</p>

<div class="ide" data-lang="html" data-lang-label="XML">
    <pre class="line-numbers"><code class="prism language-xml" data-prism><?php echo $this->escape('<manifest ...>
  ...
  <application ...>
    ...
    <!-- Add this inside the `<application>` tag, along side the existing `<activity>` tags -->
    <activity android:name="io.appwrite.views.CallbackActivity" android:exported="true">
      <intent-filter android:label="android_web_auth">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="appwrite-callback-[PROJECT_ID]" />
      </intent-filter>
    </activity>
  </application>
</manifest>'); ?>
</code></pre>
</div>

<h2><a href="/docs/getting-started-for-android#initSDK" id="initSDK">Init your SDK</a></h2>

<p>Initialize your SDK code with your project ID, which can be found in your project settings page.</p>

<div class="ide" data-lang="android" data-lang-label="Android SDK">
    <pre class="line-numbers"><code class="prism language-kotlin" data-prism>import io.appwrite.Client
import io.appwrite.services.Account

val client = Client(context)
    .setEndpoint("https://cloud.appwrite.io/v1") // Your Appwrite Endpoint
    .setProject("[PROJECT_ID]")                // Your project ID</code></pre>
</div>

<p>Before sending any API calls to your new Appwrite project, make sure your Android device or emulator has network access to your Appwrite project's hostname or IP address.</p>

<h2><a href="/docs/getting-started-for-android#makeRequest" id="makeRequest">Make Your First Request</a></h2>

<p>After your SDK configuration is set, access any of the Appwrite services and choose any request to send. Full documentation for any service method you would like to use can be found in the SDK documentation or in the API References section.</p>

<div class="ide" data-lang="android" data-lang-label="Android SDK">
                    <pre class="line-numbers"><code class="prism language-kotlin" data-prism>// Register User
val account = Account(client)

val user = account.create(
    userId = ID.unique(),
    email = "email@example.com",
    password = "password"
)</code></pre>
</div>

<h2><a href="/docs/getting-started-for-android#listenToChanges" id="listenToChanges">Listen to Changes</a></h2>

<p>To listen to changes in realtime from Appwrite, subscribe to a variety of channels and receive updates within milliseconds. Full documentation for Realtime can be found <a href="/docs/realtime">here</a>.</p>

<div class="ide" data-lang="android" data-lang-label="Android SDK">
  <pre class="line-numbers"><code class="prism language-kotlin" data-prism>// Subscribe to files channel
val realtime = Realtime(client)

val subscription = realtime.subscribe("files") {
    if (it.events.contains("buckets.*.files.*.create")) {
        // Log when a new file is uploaded
        print(it.payload.toString());
    }
})
</code></pre>
</div>

<h2><a href="/docs/getting-started-for-android#fullExample" id="fullExample">Full Example</a></h2>
<div class="ide" data-lang="android" data-lang-label="Android SDK">
                    <pre class="line-numbers"><code class="prism language-kotlin" data-prism>import io.appwrite.Client
import io.appwrite.services.Account

val client = Client(context)
    .setEndpoint("https://cloud.appwrite.io/v1") // Your Appwrite Endpoint
    .setProject("[PROJECT_ID]")                // Your project ID

val account = Account(client)

val user = account.create(
    userId = ID.unique(),
    email = "email@example.com",
    password = "password"
)

// Subscribe to files channel
val realtime = Realtime(client)

val subscription = realtime.subscribe("files") {
    if (it.events.contains("buckets.*.files.*.create")) {
        // Log when a new file is uploaded
        print(it.payload.toString());
    }
}</code></pre>
</div>

<h2><a href="/docs/getting-started-for-android#nextSteps" id="nextSteps">Next Steps</a></h2>

<p>Appwrite has many services and tools to help improve your app and speed up your development. The best way to learn how you can take advantage of them is to explore the different API references docs.</p>
