1 #!/usr/bin/python 2 3 # Copyright (C) 2017 The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import os 18 import sys 19 import re 20 21 print "Generate framework fragment related code for leanback" 22 23 cls = ['Base', 'BaseRow', 'Browse', 'Details', 'Error', 'Headers', 24 'Playback', 'Rows', 'Search', 'VerticalGrid', 'Branded', 25 'GuidedStep', 'Onboarding', 'Video'] 26 27 for w in cls: 28 print "copy {}SupportFragment to {}Fragment".format(w, w) 29 30 file = open('src/android/support/v17/leanback/app/{}SupportFragment.java'.format(w), 'r') 31 content = "// CHECKSTYLE:OFF Generated code\n" 32 content = content + "/* This file is auto-generated from {}SupportFragment.java. DO NOT MODIFY. */\n\n".format(w) 33 34 for line in file: 35 line = line.replace('IS_FRAMEWORK_FRAGMENT = false', 'IS_FRAMEWORK_FRAGMENT = true'); 36 for w2 in cls: 37 line = line.replace('{}SupportFragment'.format(w2), '{}Fragment'.format(w2)) 38 line = line.replace('androidx.fragment.app.FragmentActivity', 'android.app.Activity') 39 line = line.replace('androidx.fragment.app.Fragment', 'android.app.Fragment') 40 line = line.replace('activity.getSupportFragmentManager()', 'activity.getFragmentManager()') 41 line = line.replace('FragmentActivity activity', 'Activity activity') 42 line = line.replace('(FragmentActivity', '(Activity') 43 # replace getContext() with FragmentUtil.getContext(XXXFragment.this), but dont match the case "view.getContext()" 44 line = re.sub(r'([^\.])getContext\(\)', r'\1FragmentUtil.getContext({}Fragment.this)'.format(w), line); 45 content = content + line 46 file.close() 47 # add deprecated tag to fragment class and inner classes/interfaces 48 content = re.sub(r'\*\/\n(@.*\n|)(public |abstract public |abstract |)class', '* @deprecated use {@link ' + w + 'SupportFragment}\n */\n@Deprecated\n\\1\\2class', content) 49 content = re.sub(r'\*\/\n public (static class|interface|final static class|abstract static class)', '* @deprecated use {@link ' + w + 'SupportFragment}\n */\n @Deprecated\n public \\1', content) 50 outfile = open('src/android/support/v17/leanback/app/{}Fragment.java'.format(w), 'w') 51 outfile.write(content) 52 outfile.close() 53 54 55 56 print "copy VideoSupportFragmentGlueHost to VideoFragmentGlueHost" 57 file = open('src/android/support/v17/leanback/app/VideoSupportFragmentGlueHost.java', 'r') 58 content = "// CHECKSTYLE:OFF Generated code\n" 59 content = content + "/* This file is auto-generated from VideoSupportFragmentGlueHost.java. DO NOT MODIFY. */\n\n" 60 for line in file: 61 line = line.replace('androidx.fragment.app.Fragment', 'android.app.Fragment') 62 line = line.replace('VideoSupportFragment', 'VideoFragment') 63 line = line.replace('PlaybackSupportFragment', 'PlaybackFragment') 64 content = content + line 65 file.close() 66 # add deprecated tag to class 67 content = re.sub(r'\*\/\npublic class', '* @deprecated use {@link VideoSupportFragmentGlueHost}\n */\n@Deprecated\npublic class', content) 68 outfile = open('src/android/support/v17/leanback/app/VideoFragmentGlueHost.java', 'w') 69 outfile.write(content) 70 outfile.close() 71 72 73 74 print "copy PlaybackSupportFragmentGlueHost to PlaybackFragmentGlueHost" 75 file = open('src/android/support/v17/leanback/app/PlaybackSupportFragmentGlueHost.java', 'r') 76 content = "// CHECKSTYLE:OFF Generated code\n" 77 content = content + "/* This file is auto-generated from {}PlaybackSupportFragmentGlueHost.java. DO NOT MODIFY. */\n\n" 78 for line in file: 79 line = line.replace('VideoSupportFragment', 'VideoFragment') 80 line = line.replace('PlaybackSupportFragment', 'PlaybackFragment') 81 line = line.replace('androidx.fragment.app.Fragment', 'android.app.Fragment') 82 content = content + line 83 file.close() 84 # add deprecated tag to class 85 content = re.sub(r'\*\/\npublic class', '* @deprecated use {@link PlaybackSupportFragmentGlueHost}\n */\n@Deprecated\npublic class', content) 86 outfile = open('src/android/support/v17/leanback/app/PlaybackFragmentGlueHost.java', 'w') 87 outfile.write(content) 88 outfile.close() 89 90 91 92 print "copy DetailsSupportFragmentBackgroundController to DetailsFragmentBackgroundController" 93 file = open('src/android/support/v17/leanback/app/DetailsSupportFragmentBackgroundController.java', 'r') 94 content = "// CHECKSTYLE:OFF Generated code\n" 95 content = content + "/* This file is auto-generated from {}DetailsSupportFragmentBackgroundController.java. DO NOT MODIFY. */\n\n" 96 for line in file: 97 line = line.replace('VideoSupportFragment', 'VideoFragment') 98 line = line.replace('DetailsSupportFragment', 'DetailsFragment') 99 line = line.replace('RowsSupportFragment', 'RowsFragment') 100 line = line.replace('androidx.fragment.app.Fragment', 'android.app.Fragment') 101 line = line.replace('mFragment.getContext()', 'FragmentUtil.getContext(mFragment)') 102 content = content + line 103 file.close() 104 # add deprecated tag to class 105 content = re.sub(r'\*\/\npublic class', '* @deprecated use {@link DetailsSupportFragmentBackgroundController}\n */\n@Deprecated\npublic class', content) 106 outfile = open('src/android/support/v17/leanback/app/DetailsFragmentBackgroundController.java', 'w') 107 outfile.write(content) 108 outfile.close() 109