1 /* 2 * Copyright 2018 The Android Open Source Project 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 17 package com.android.pump.provider; 18 19 import android.net.Uri; 20 21 import androidx.annotation.AnyThread; 22 import androidx.annotation.NonNull; 23 24 import com.android.pump.util.Clog; 25 26 import java.util.regex.MatchResult; 27 import java.util.regex.Matcher; 28 import java.util.regex.Pattern; 29 30 @AnyThread 31 public final class Query { 32 private static final String TAG = Clog.tag(Query.class); 33 34 private static final Pattern PATTERN_EPISODE = Pattern.compile( 35 "([^\\\\/]+?)(?:\\(((?:19|20)\\d{2})\\))?\\s*" + 36 "[Ss](\\d{1,2})[._x]?[Ee](\\d{1,2})(?!.*[Ss]\\d{1,2}[._x]?[Ee]\\d{1,2})"); 37 private static final Pattern PATTERN_MOVIE = Pattern.compile( 38 "([^\\\\/]+)\\(((?:19|20)\\d{2})\\)(?!.*\\((?:19|20)\\d{2}\\))"); 39 private static final Pattern PATTERN_CLEANUP = Pattern.compile("\\s+"); 40 41 private final String mName; 42 private final int mYear; 43 private final int mSeason; 44 private final int mEpisode; 45 Query(String name)46 private Query(String name) { 47 //Clog.i(TAG, "Query(" + name + ")"); 48 mName = name; 49 mYear = -1; 50 mSeason = -1; 51 mEpisode = -1; 52 } 53 Query(String name, int year)54 private Query(String name, int year) { 55 //Clog.i(TAG, "Query(" + name + ", " + year + ")"); 56 mName = name; 57 mYear = year; 58 mSeason = -1; 59 mEpisode = -1; 60 } 61 Query(String name, int season, int episode)62 private Query(String name, int season, int episode) { 63 //Clog.i(TAG, "Query(" + name + ", " + season + ", " + episode + ")"); 64 mName = name; 65 mYear = -1; 66 mSeason = season; 67 mEpisode = episode; 68 } 69 Query(String name, int year, int season, int episode)70 private Query(String name, int year, int season, int episode) { 71 //Clog.i(TAG, "Query(" + name + ", " + year + ", " + season + ", " + episode + ")"); 72 mName = name; 73 mYear = year; 74 mSeason = season; 75 mEpisode = episode; 76 } 77 isMovie()78 public boolean isMovie() { 79 return hasYear() && !isEpisode(); 80 } 81 isEpisode()82 public boolean isEpisode() { 83 return mSeason >= 0 && mEpisode >= 0; 84 } 85 getName()86 public @NonNull String getName() { 87 return mName; 88 } 89 hasYear()90 public boolean hasYear() { 91 return mYear >= 0; 92 } 93 getYear()94 public int getYear() { 95 return mYear; 96 } 97 getSeason()98 public int getSeason() { 99 return mSeason; 100 } 101 getEpisode()102 public int getEpisode() { 103 return mEpisode; 104 } 105 parse(@onNull Uri uri)106 public static @NonNull Query parse(@NonNull Uri uri) { 107 //Clog.i(TAG, "parse(" + uri + ")"); 108 String filePath = uri.getPath(); 109 Query query; 110 query = parseEpisode(filePath); 111 if (query == null) { 112 query = parseMovie(filePath); 113 } 114 if (query == null) { 115 query = new Query(uri.getLastPathSegment()); 116 } 117 return query; 118 } 119 parseEpisode(String filePath)120 private static Query parseEpisode(String filePath) { 121 //Clog.i(TAG, "parseEpisode(" + filePath + ")"); 122 Matcher matcher = PATTERN_EPISODE.matcher(filePath); 123 if (matcher.find()) { 124 MatchResult matchResult = matcher.toMatchResult(); 125 if (matchResult.groupCount() == 4) { 126 String name = cleanup(matchResult.group(1)); 127 int year = matchResult.group(2) == null ? 0 : Integer.valueOf(matchResult.group(2)); 128 int season = Integer.valueOf(matchResult.group(3)); 129 int episode = Integer.valueOf(matchResult.group(4)); 130 //Clog.i(TAG, "name = " + name); 131 //if (year > 0) { 132 // Clog.i(TAG, "year = " + year); 133 //} 134 //Clog.i(TAG, "season = " + season); 135 //Clog.i(TAG, "episode = " + episode); 136 if (year > 0) { 137 return new Query(name, year, season, episode); 138 } else { 139 return new Query(name, season, episode); 140 } 141 } 142 } 143 return null; 144 } 145 parseMovie(String filePath)146 private static Query parseMovie(String filePath) { 147 //Clog.i(TAG, "parseMovie(" + filePath + ")"); 148 Matcher matcher = PATTERN_MOVIE.matcher(filePath); 149 if (matcher.find()) { 150 MatchResult matchResult = matcher.toMatchResult(); 151 if (matchResult.groupCount() == 2) { 152 String name = cleanup(matchResult.group(1)); 153 int year = Integer.valueOf(matchResult.group(2)); 154 //Clog.i(TAG, "name = " + name); 155 //Clog.i(TAG, "year = " + year); 156 return new Query(name, year); 157 } 158 } 159 return null; 160 } 161 cleanup(String string)162 private static String cleanup(String string) { 163 return PATTERN_CLEANUP.matcher(string).replaceAll(" ").trim(); 164 } 165 } 166