Home | History | Annotate | Download | only in channelsprograms
      1 /*
      2  * Copyright (c) 2017 Google Inc.
      3  *
      4  *  Licensed under the Apache License, Version 2.0 (the "License");
      5  *  you may not use this file except in compliance with the License.
      6  *  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  *  Unless required by applicable law or agreed to in writing, software
     11  *  distributed under the License is distributed on an "AS IS" BASIS,
     12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  *  See the License for the specific language governing permissions and
     14  *  limitations under the License.
     15  */
     16 package com.example.android.tv.channelsprograms;
     17 
     18 import android.app.Activity;
     19 import android.content.Intent;
     20 import android.net.Uri;
     21 import android.os.Bundle;
     22 import android.util.Log;
     23 import android.widget.Toast;
     24 
     25 import com.example.android.tv.channelsprograms.model.MockDatabase;
     26 import com.example.android.tv.channelsprograms.model.Movie;
     27 import com.example.android.tv.channelsprograms.model.Subscription;
     28 import com.example.android.tv.channelsprograms.playback.PlaybackActivity;
     29 import com.example.android.tv.channelsprograms.util.AppLinkHelper;
     30 
     31 /**
     32  * Delegates to the correct activity based on how the user entered the app.
     33  *
     34  * <p>Supports two options: view and play. The view option will open the channel for the user to be
     35  * able to view more programs. The play option will load the channel/program,
     36  * subscriptions/mediaContent start playing the movie.
     37  */
     38 public class AppLinkActivity extends Activity {
     39 
     40     private static final String TAG = "AppLinkActivity";
     41 
     42     @Override
     43     protected void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         Intent intent = getIntent();
     47         Uri uri = intent.getData();
     48 
     49         Log.v(TAG, uri.toString());
     50 
     51         if (uri.getPathSegments().isEmpty()) {
     52             Log.e(TAG, "Invalid uri " + uri);
     53             finish();
     54             return;
     55         }
     56 
     57         AppLinkHelper.AppLinkAction action = AppLinkHelper.extractAction(uri);
     58         switch (action.getAction()) {
     59             case AppLinkHelper.PLAYBACK:
     60                 play((AppLinkHelper.PlaybackAction) action);
     61                 break;
     62             case AppLinkHelper.BROWSE:
     63                 browse((AppLinkHelper.BrowseAction) action);
     64                 break;
     65             default:
     66                 throw new IllegalArgumentException("Invalid Action " + action);
     67         }
     68     }
     69 
     70     private void browse(AppLinkHelper.BrowseAction action) {
     71         Subscription subscription =
     72                 MockDatabase.findSubscriptionByName(this, action.getSubscriptionName());
     73         if (subscription == null) {
     74             Log.e(TAG, "Invalid subscription " + action.getSubscriptionName());
     75         } else {
     76             // TODO: Open an activity that has the movies for the subscription.
     77             Toast.makeText(this, action.getSubscriptionName(), Toast.LENGTH_LONG).show();
     78         }
     79         finish();
     80     }
     81 
     82     private void play(AppLinkHelper.PlaybackAction action) {
     83         if (action.getPosition() == AppLinkHelper.DEFAULT_POSITION) {
     84             Log.d(
     85                     TAG,
     86                     "Playing program "
     87                             + action.getMovieId()
     88                             + " from channel "
     89                             + action.getChannelId());
     90         } else {
     91             Log.d(
     92                     TAG,
     93                     "Continuing program "
     94                             + action.getMovieId()
     95                             + " from channel "
     96                             + action.getChannelId()
     97                             + " at time "
     98                             + action.getPosition());
     99         }
    100 
    101         Movie movie = MockDatabase.findMovieById(this, action.getChannelId(), action.getMovieId());
    102         if (movie == null) {
    103             Log.e(TAG, "Invalid program " + action.getMovieId());
    104         } else {
    105             startPlaying(action.getChannelId(), movie, action.getPosition());
    106         }
    107         finish();
    108     }
    109 
    110     private void startPlaying(long channelId, Movie movie, long position) {
    111         Intent playMovieIntent = new Intent(this, PlaybackActivity.class);
    112         playMovieIntent.putExtra(PlaybackActivity.EXTRA_MOVIE, movie);
    113         playMovieIntent.putExtra(PlaybackActivity.EXTRA_CHANNEL_ID, channelId);
    114         playMovieIntent.putExtra(PlaybackActivity.EXTRA_POSITION, position);
    115         startActivity(playMovieIntent);
    116     }
    117 }
    118